Skip to content

Commit ec868f3

Browse files
authored
fix: Consistently reject truncated 64-bit varints (#2322)
1 parent 3359e64 commit ec868f3

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/reader.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,16 @@ function readLongVarint() {
224224
return bits;
225225
i = 0;
226226
} else {
227-
for (; i < 3; ++i) {
227+
for (; i < 4; ++i) {
228228
/* istanbul ignore if */
229229
if (this.pos >= this.len)
230230
throw indexOutOfRange(this);
231-
// 1st..3th
231+
// 1st..4th
232232
bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
233233
if (this.buf[this.pos++] < 128)
234234
return bits;
235235
}
236-
// 4th
237-
bits.lo = (bits.lo | (this.buf[this.pos++] & 127) << i * 7) >>> 0;
238-
return bits;
236+
throw indexOutOfRange(this);
239237
}
240238
if (this.len - this.pos > 4) { // fast route (hi)
241239
for (; i < 5; ++i) {

tests/api_writer-reader.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ tape.test("writer & reader", function(test) {
8484
var zzBaseVal = longVal.shru(1).xor(longVal.and(1).negate());
8585
test.ok(expect("sint64", zzBaseVal, val[1]), "should write " + zzBaseVal + " as a signed zig-zag encoded varint of length " + val[1].length + " and read it back equally");
8686
});
87+
test.throws(function() {
88+
Reader.create([ 128, 128, 128 ]).uint64();
89+
}, /index out of range/, "should throw on truncated 64 bit varints with 3 bytes");
90+
test.throws(function() {
91+
Reader.create([ 128, 128, 128, 128 ]).uint64();
92+
}, /index out of range/, "should throw on truncated 64 bit varints with 4 bytes");
8793

8894
// fixed64, sfixed64 -> see also see comp_fixed/sfixed64 (grpc)
8995

0 commit comments

Comments
 (0)