Bug 819164 - reply_len is only valid before we call readString because of UTF-8 interpretation so do our string fixup based on that. r=gal

This commit is contained in:
Blake Kaplan 2012-12-11 18:04:17 -08:00
parent c2999d8be0
commit 8cb191fab1

View File

@ -45,11 +45,22 @@ self.onmessage = function(e) {
var ret = libhardware_legacy.command(data.request, cbuf, len.address());
var reply = "";
if (!ret) {
// The return value from libhardware_legacy.command is not guaranteed to
// be null-terminated. At the same time we want to make sure that we
// don't return a response with a trailing newline, so handle both cases
// here. Note that if we wrote 4096 characters to cbuf, we don't have to
// null-terminate the buffer, as ctypes has the maximum size already.
var reply_len = len.value;
var str = cbuf.readString();
if (str[reply_len-1] == "\n")
--reply_len;
reply = str.substr(0, reply_len);
if (reply_len !== 0) {
if (cbuf[reply_len - 1] === 10)
cbuf[--reply_len] = 0;
else if (reply_len !== 4096)
cbuf[reply_len] = 0;
reply = cbuf.readString();
}
// Else if reply_len was 0, use the empty reply, set above.
}
postMessage({ id: id, status: ret, reply: reply });
break;