Skip to content

Commit 9fee62e

Browse files
authored
Merge pull request #108 from dweitzman/write-non-strings
Catch non-strings in BinaryEncoder.writeString
2 parents 143ba02 + 73dd25f commit 9fee62e

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

binary/decoder_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,16 @@ describe('binaryDecoderTest', function() {
376376
assertEquals(utf8_four_bytes, decoder.readString(utf8_four_bytes.length));
377377
});
378378

379+
/**
380+
* Verifies that passing a non-string to writeString raises an error.
381+
*/
382+
it('testBadString', function() {
383+
var encoder = new jspb.BinaryEncoder();
384+
385+
assertThrows(function() {encoder.writeString(42)});
386+
assertThrows(function() {encoder.writeString(null)});
387+
});
388+
379389
/**
380390
* Verifies that misuse of the decoder class triggers assertions.
381391
*/

binary/encoder.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@ jspb.BinaryEncoder.prototype.writeFixedHash64 = function(hash) {
473473
jspb.BinaryEncoder.prototype.writeString = function(value) {
474474
var oldLength = this.buffer_.length;
475475

476+
// Protect against non-string values being silently ignored.
477+
goog.asserts.assertString(value);
478+
476479
for (var i = 0; i < value.length; i++) {
477480

478481
var c = value.charCodeAt(i);

0 commit comments

Comments
 (0)