Revert "add bounds checking to encode and decode"

This reverts commit fcebf3acdc.
master
Chris Dickinson 4 years ago
parent 79d2daa0cc
commit 614645bb03

@ -12,7 +12,7 @@ function read(buf, offset) {
, l = buf.length , l = buf.length
do { do {
if (counter >= l || shift > 49) { if (counter >= l) {
read.bytes = 0 read.bytes = 0
throw new RangeError('Could not decode varint') throw new RangeError('Could not decode varint')
} }

@ -6,10 +6,6 @@ var MSB = 0x80
, INT = Math.pow(2, 31) , INT = Math.pow(2, 31)
function encode(num, out, offset) { function encode(num, out, offset) {
if (num > Number.MAX_SAFE_INTEGER) {
encode.bytes = 0
throw new RangeError('Could not encode varint')
}
out = out || [] out = out || []
offset = offset || 0 offset = offset || 0
var oldOffset = offset var oldOffset = offset

@ -77,6 +77,7 @@ test('big integers', function (assert) {
var bigs = [] var bigs = []
for(var i = 32; i <= 53; i++) (function (i) { for(var i = 32; i <= 53; i++) (function (i) {
bigs.push(Math.pow(2, i) - 1) bigs.push(Math.pow(2, i) - 1)
bigs.push(Math.pow(2, i))
})(i) })(i)
bigs.forEach(function (n) { bigs.forEach(function (n) {
@ -92,7 +93,7 @@ test('fuzz test - big', function(assert) {
var expect var expect
, encoded , encoded
var MAX_INTD = Number.MAX_SAFE_INTEGER var MAX_INTD = Math.pow(2, 55)
var MAX_INT = Math.pow(2, 31) var MAX_INT = Math.pow(2, 31)
for(var i = 0, len = 100; i < len; ++i) { for(var i = 0, len = 100; i < len; ++i) {
@ -109,7 +110,7 @@ test('fuzz test - big', function(assert) {
test('encodingLength', function (assert) { test('encodingLength', function (assert) {
for(var i = 0; i <= 53; i++) { 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)) assert.equal(encode(n).length, encodingLength(n))
} }
@ -133,24 +134,6 @@ test('buffer too short', function (assert) {
assert.end() 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) { function randint(range) {
return Math.floor(Math.random() * range) return Math.floor(Math.random() * range)
} }

Loading…
Cancel
Save