From: Jesse Tane Date: Tue, 16 Dec 2014 00:01:35 +0000 (-0500) Subject: update bounds checks and throw RangeErrors instead of TypeErrors for failures X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=75dc592e2283012f5b53af88f62719ecb0c64709;p=buffer.git update bounds checks and throw RangeErrors instead of TypeErrors for failures --- diff --git a/index.js b/index.js index baeb4ff..98c8b87 100644 --- a/index.js +++ b/index.js @@ -382,6 +382,10 @@ Buffer.prototype.write = function (string, offset, length, encoding) { } offset = Number(offset) || 0 + + if (length < 0 || offset < 0 || offset > this.length) + throw new RangeError('attempt to write outside buffer bounds'); + var remaining = this.length - offset if (!length) { length = remaining @@ -648,8 +652,8 @@ Buffer.prototype.readDoubleBE = function (offset, noAssert) { function checkInt (buf, value, offset, ext, max, min) { if (!Buffer.isBuffer(buf)) throw new TypeError('buffer must be a Buffer instance') - if (value > max || value < min) throw new TypeError('value is out of bounds') - if (offset + ext > buf.length) throw new TypeError('index out of range') + if (value > max || value < min) throw new RangeError('value is out of bounds') + if (offset + ext > buf.length) throw new RangeError('index out of range') } Buffer.prototype.writeUInt8 = function (value, offset, noAssert) { @@ -794,8 +798,9 @@ Buffer.prototype.writeInt32BE = function (value, offset, noAssert) { } function checkIEEE754 (buf, value, offset, ext, max, min) { - if (value > max || value < min) throw new TypeError('value is out of bounds') - if (offset + ext > buf.length) throw new TypeError('index out of range') + if (value > max || value < min) throw new RangeError('value is out of bounds') + if (offset + ext > buf.length) throw new RangeError('index out of range') + if (offset < 0) throw new RangeError('index out of range') } function writeFloat (buf, value, offset, littleEndian, noAssert) { @@ -829,23 +834,24 @@ Buffer.prototype.writeDoubleBE = function (value, offset, noAssert) { } // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) -Buffer.prototype.copy = function (target, target_start, start, end) { +Buffer.prototype.copy = function(target, target_start, start, end) { var source = this if (!start) start = 0 if (!end && end !== 0) end = this.length + if (target_start >= target.length) target_start = target.length if (!target_start) target_start = 0 + if (end > 0 && end < start) end = start // Copy 0 bytes; we're done - if (end === start) return - if (target.length === 0 || source.length === 0) return + if (end === start) return 0 + if (target.length === 0 || source.length === 0) return 0 // Fatal error conditions - if (end < start) throw new TypeError('sourceEnd < sourceStart') - if (target_start < 0 || target_start >= target.length) - throw new TypeError('targetStart out of bounds') - if (start < 0 || start >= source.length) throw new TypeError('sourceStart out of bounds') - if (end < 0 || end > source.length) throw new TypeError('sourceEnd out of bounds') + if (target_start < 0) + throw new RangeError('targetStart out of bounds') + if (start < 0 || start >= source.length) throw new RangeError('sourceStart out of bounds') + if (end < 0) throw new RangeError('sourceEnd out of bounds') // Are we oob? if (end > this.length) @@ -870,14 +876,14 @@ Buffer.prototype.fill = function (value, start, end) { if (!start) start = 0 if (!end) end = this.length - if (end < start) throw new TypeError('end < start') + if (end < start) throw new RangeError('end < start') // Fill 0 bytes; we're done if (end === start) return if (this.length === 0) return - if (start < 0 || start >= this.length) throw new TypeError('start out of bounds') - if (end < 0 || end > this.length) throw new TypeError('end out of bounds') + if (start < 0 || start >= this.length) throw new RangeError('start out of bounds') + if (end < 0 || end > this.length) throw new RangeError('end out of bounds') var i if (typeof value === 'number') {