-
Notifications
You must be signed in to change notification settings - Fork 18k
x/mobile/bind/seq: panic when returning a nil error or "" string from Go to Java through parameter serialization #9271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
After re-reading the gobind doc, perhaps the single return value of type What is the intended way to handle if err == nil {
out.WriteUTF16("")
} else {
out.WriteUTF16(err.Error())
} And the Java serialization side receives: String _err = _out.readUTF16();
if (_err != null) {
throw new Exception(_err);
} When Go sends |
Two bugs here. The first is that WriteUTF16 doesn't pre-allocate enough space for the length header, which appears when writing empty strings: E/Go (21191): panic: runtime error: slice bounds out of range The second is that Java_go_seq_readUTF16 allocates an empty Java string for an empty Go string, instead of returning NULL, which trips up the the error->exception logic. I have a test that exercises both. Unfortunately, you couldn't see that panic in your adb logcat output because I didn't get the CL to redirect panic output in until after the Go 1.4 release. If you use tip, you'll see the message. Thanks the bug report. |
Fixes golang/go#9271. Change-Id: I57dcd11ae5afdc23bc9b1a0945d0789c9feeefb1 Reviewed-on: https://go-review.googlesource.com/1423 Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
error
. Used Java/Go binding and from Java invoked the Go function which returnsnil
for error value. Under the hood, this is the same as returning a""
string.The parameter serialization function
Buffer.WriteUTF16("")
is called in the case where parameter serialization is returning anil
error. As a test, I changed my code to return a""
string instead of a nil error and that also results in the same process termination.I suspect the problem is here: https://github.com/golang/mobile/blob/2861ce3b898909384ed7b647e0a167254238c036/bind/seq/utf16.go#L39. In the case where
len(s)
is0
, the buffer is not grown. But the subsequent linedata := b.Data[b.Offset+4:]
assumes the buffer is at least4
bytes and will panic with the default empty buffer.By modifying
WriteUTF16
to skip the string writing entirely in the0
length case, the serialization works: Java receives the""
ornil
error and the process does not terminate.Incidentally, the
b.grow(4 * len(s))
may also be insufficient in alen(s) > 0
case because it only grows the buffer by the maximum possible string length, but the buffer is accessed as if it were4 + max possible string length
bytes due to how the not-yet-allocated initial length prefix is skipped over. In the "everything is surrogate pair" case, could that result in an access past the end of the allocated buffer? To fix this, perhaps the buffer needs to grow by4 + 4*len(s)
just in case.The text was updated successfully, but these errors were encountered: