Commit 378daa69 authored by Ken Rockot's avatar Ken Rockot Committed by Commit Bot

Mojom JS: Fix string serialization

Corrects string payload size computation to use the actual UTF8-encoded
byte length rather than string length.

Bug: 935332
Change-Id: I8be84c165312bda1755762ff1876825021f885ae
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1494314Reviewed-by: default avatarcalamity <calamity@chromium.org>
Commit-Queue: Ken Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#637916}
parent 806e82b0
......@@ -400,13 +400,11 @@ mojo.internal.Encoder = class {
}
encodeString(offset, value) {
if (!mojo.internal.Message.textEncoder)
mojo.internal.Message.textEncoder = new TextEncoder('utf-8');
if (typeof value !== 'string')
throw new Error('Unxpected non-string value for string field.');
this.encodeArray(
{elementType: mojo.internal.Uint8}, offset,
mojo.internal.Message.textEncoder.encode(value));
mojo.internal.Encoder.stringToUtf8Bytes(value));
}
encodeOffset(offset, absoluteOffset) {
......@@ -556,10 +554,20 @@ mojo.internal.Encoder = class {
field['type'].$.encode(
value[tag], unionEncoder, offset + 8, 0, field['nullable']);
}
/**
* @param {string} value
* @return {!Uint8Array}
*/
static stringToUtf8Bytes(value) {
if (!mojo.internal.Encoder.textEncoder)
mojo.internal.Encoder.textEncoder = new TextEncoder('utf-8');
return mojo.internal.Encoder.textEncoder.encode(value);
}
};
/** @type {TextEncoder} */
mojo.internal.Message.textEncoder = null;
mojo.internal.Encoder.textEncoder = null;
/**
* Helps decode incoming messages. Decoders may be created recursively to
......@@ -1161,7 +1169,8 @@ mojo.internal.String = {
},
computePayloadSize: function(value, nullable) {
return mojo.internal.computeTotalArraySize(
{elementType: mojo.internal.Uint8}, value);
{elementType: mojo.internal.Uint8},
mojo.internal.Encoder.stringToUtf8Bytes(value));
},
arrayElementSize: nullable => 8,
isValidObjectKeyType: true,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment