Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions binary/decoder_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,16 @@ describe('binaryDecoderTest', function() {
assertEquals(utf8_four_bytes, decoder.readString(utf8_four_bytes.length));
});

/**
* Verifies that passing a non-string to writeString raises an error.
*/
it('testBadString', function() {
var encoder = new jspb.BinaryEncoder();

assertThrows(function() {encoder.writeString(42)});
assertThrows(function() {encoder.writeString(null)});
});

/**
* Verifies that misuse of the decoder class triggers assertions.
*/
Expand Down
3 changes: 3 additions & 0 deletions binary/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,9 @@ jspb.BinaryEncoder.prototype.writeFixedHash64 = function(hash) {
jspb.BinaryEncoder.prototype.writeString = function(value) {
var oldLength = this.buffer_.length;

// Protect against non-string values being silently ignored.
goog.asserts.assertString(value);

for (var i = 0; i < value.length; i++) {

var c = value.charCodeAt(i);
Expand Down
14 changes: 9 additions & 5 deletions binary/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,11 +1051,6 @@ jspb.utils.byteSourceToUint8Array = function(data) {
return /** @type {!Uint8Array} */(new Uint8Array(data));
}

if (typeof Buffer != 'undefined' && data.constructor === Buffer) {
return /** @type {!Uint8Array} */ (
new Uint8Array(/** @type {?} */ (data)));
}

if (data.constructor === Array) {
data = /** @type {!Array<number>} */(data);
return /** @type {!Uint8Array} */(new Uint8Array(data));
Expand All @@ -1066,6 +1061,15 @@ jspb.utils.byteSourceToUint8Array = function(data) {
return goog.crypt.base64.decodeStringToUint8Array(data);
}

if (data instanceof Uint8Array) {
// Support types like nodejs Buffer and other subclasses of Uint8Array.
data = /** @type {!Uint8Array} */ (data);
// Make a shallow copy to ensure we only ever deal with Uint8Array
// exactly to ensure monomorphism.
return /** @type {!Uint8Array} */ (
new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
}

goog.asserts.fail('Type not convertible to Uint8Array.');
return /** @type {!Uint8Array} */(new Uint8Array(0));
};
1 change: 0 additions & 1 deletion binary/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,6 @@ describe('binaryUtilsTest', function() {
var sourceBytes = new Uint8Array(sourceData);
var sourceBuffer = sourceBytes.buffer;
var sourceBase64 = goog.crypt.base64.encodeByteArray(sourceData);
var sourceString = goog.crypt.byteArrayToString(sourceData);

function check(result) {
expect(result.constructor).toEqual(Uint8Array);
Expand Down
Loading