From 614645bb0389c8059a89dff80ed7ee308ff8a730 Mon Sep 17 00:00:00 2001 From: Chris Dickinson Date: Mon, 28 Sep 2020 10:06:51 -0700 Subject: [PATCH] Revert "add bounds checking to encode and decode" This reverts commit fcebf3acdc04831238278dd54ffef3863b0142bf. --- decode.js | 2 +- encode.js | 4 ---- test.js | 23 +++-------------------- 3 files changed, 4 insertions(+), 25 deletions(-) diff --git a/decode.js b/decode.js index 80d49f2..4d274c2 100644 --- a/decode.js +++ b/decode.js @@ -12,7 +12,7 @@ function read(buf, offset) { , l = buf.length do { - if (counter >= l || shift > 49) { + if (counter >= l) { read.bytes = 0 throw new RangeError('Could not decode varint') } diff --git a/encode.js b/encode.js index f4ecb8d..d395723 100644 --- a/encode.js +++ b/encode.js @@ -6,10 +6,6 @@ var MSB = 0x80 , INT = Math.pow(2, 31) function encode(num, out, offset) { - if (num > Number.MAX_SAFE_INTEGER) { - encode.bytes = 0 - throw new RangeError('Could not encode varint') - } out = out || [] offset = offset || 0 var oldOffset = offset diff --git a/test.js b/test.js index a55c03c..0138ff4 100644 --- a/test.js +++ b/test.js @@ -77,6 +77,7 @@ test('big integers', function (assert) { var bigs = [] for(var i = 32; i <= 53; i++) (function (i) { bigs.push(Math.pow(2, i) - 1) + bigs.push(Math.pow(2, i)) })(i) bigs.forEach(function (n) { @@ -92,7 +93,7 @@ test('fuzz test - big', function(assert) { var expect , encoded - var MAX_INTD = Number.MAX_SAFE_INTEGER + var MAX_INTD = Math.pow(2, 55) var MAX_INT = Math.pow(2, 31) for(var i = 0, len = 100; i < len; ++i) { @@ -109,7 +110,7 @@ test('fuzz test - big', function(assert) { test('encodingLength', function (assert) { for(var i = 0; i <= 53; i++) { - var n = Math.pow(2, i) - 1 + var n = Math.pow(2, i) assert.equal(encode(n).length, encodingLength(n)) } @@ -133,24 +134,6 @@ test('buffer too short', function (assert) { assert.end() }) -test('buffer too long', function (assert) { - - var buffer = Uint8Array.from( - Array.from({length: 150}, function () { return 0xff }) - .concat(Array.from({length: 1}, function () { return 0x1 })) - ) - - try { - var val = decode(buffer) - encode(val) - assert.fail('expected an error received value instead: ' + val) - } catch (err) { - assert.equal(err.constructor, RangeError) - assert.equal(decode.bytes, 0) - } - assert.end() -}) - function randint(range) { return Math.floor(Math.random() * range) }