Skip to content

Commit 70dcbf2

Browse files
author
Daniel Wirtz
committed
Make sure to fully read any varint, fixes #3
1 parent 4951dc6 commit 70dcbf2

File tree

7 files changed

+44
-38
lines changed

7 files changed

+44
-38
lines changed

ByteBuffer.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,14 +1042,11 @@
10421042
b;
10431043
var value = 0 >>> 0;
10441044
do {
1045-
if (count == ByteBuffer.MAX_VARINT32_BYTES) {
1046-
throw(new Error("Cannot read Varint32 from "+this+"@"+offset+": Number of bytes is larger than "+ByteBuffer.MAX_VARINT32_BYTES));
1047-
}
10481045
b = src[offset+count];
10491046
value |= ((b&0x7F)<<(7*count)) >>> 0;
10501047
++count;
10511048
} while (b & 0x80);
1052-
value = value | 0;
1049+
value = value | 0; // Make sure to discard the higher order bits
10531050
if (advance) {
10541051
this.offset += count;
10551052
return value;

ByteBuffer.min.js

Lines changed: 24 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ByteBuffer.min.map

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

ByteBuffer.noexpose.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -976,14 +976,11 @@
976976
b;
977977
var value = 0 >>> 0;
978978
do {
979-
if (count == ByteBuffer.MAX_VARINT32_BYTES) {
980-
throw(new Error("Cannot read Varint32 from "+this+"@"+offset+": Number of bytes is larger than "+ByteBuffer.MAX_VARINT32_BYTES));
981-
}
982979
b = src[offset+count];
983980
value |= ((b&0x7F)<<(7*count)) >>> 0;
984981
++count;
985982
} while (b & 0x80);
986-
value = value | 0;
983+
value = value | 0; // Make sure to discard the higher order bits
987984
if (advance) {
988985
this.offset += count;
989986
return value;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bytebuffer",
3-
"version": "1.3.4",
3+
"version": "1.3.5",
44
"author": "Daniel Wirtz <[email protected]>",
55
"description": "ByteBuffer.js: A Java-like, Netty-inspired ByteBuffer implementation using typed arrays.",
66
"main": "ByteBuffer.js",

src/ByteBuffer.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,14 +1042,11 @@
10421042
b;
10431043
var value = 0 >>> 0;
10441044
do {
1045-
if (count == ByteBuffer.MAX_VARINT32_BYTES) {
1046-
throw(new Error("Cannot read Varint32 from "+this+"@"+offset+": Number of bytes is larger than "+ByteBuffer.MAX_VARINT32_BYTES));
1047-
}
10481045
b = src[offset+count];
10491046
value |= ((b&0x7F)<<(7*count)) >>> 0;
10501047
++count;
10511048
} while (b & 0x80);
1052-
value = value | 0;
1049+
value = value | 0; // Make sure to discard the higher order bits
10531050
if (advance) {
10541051
this.offset += count;
10551052
return value;

tests/suite.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ var FILE = "ByteBuffer.min.js";
3131
*/
3232
var ByteBuffer = require(__dirname+"/../"+FILE);
3333

34+
/**
35+
* Long.
36+
* @type {Long}
37+
*/
38+
var Long = ByteBuffer.Long;
39+
3440
/**
3541
* Constructs a new Sandbox for module loaders and shim testing.
3642
* @param {Object.<string,*>} properties Additional properties to set
@@ -434,6 +440,15 @@ var suite = {
434440
test.done();
435441
},
436442

443+
"writeVarint64/readVarint32": function(test) {
444+
var bb = new ByteBuffer();
445+
bb.writeVarint64(Long.fromNumber(-1));
446+
bb.flip();
447+
var n = bb.readVarint32();
448+
test.equal(n, -1);
449+
test.done();
450+
},
451+
437452
"LE/BE": function(test) {
438453
var bb = new ByteBuffer(8).LE().writeInt(1).BE().writeInt(2).flip();
439454
test.equal(bb.toHex(), "<01 00 00 00 00 00 00 02>");

0 commit comments

Comments
 (0)