From: Feross Aboukhadijeh Date: Thu, 18 Aug 2016 03:20:34 +0000 (-0700) Subject: throw on negative .allocUnsafe() argument X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=80c1d498e2d54caa733b6c39f232693cd94d6801;p=buffer.git throw on negative .allocUnsafe() argument To match new behavior in node v6.4.0 https://github.com/nodejs/node/commit/8f90dcc1b8e4ac3d8597ea2ee3927f325c c980d3 --- diff --git a/index.js b/index.js index b870886..0fcd80a 100644 --- a/index.js +++ b/index.js @@ -165,6 +165,8 @@ if (Buffer.TYPED_ARRAY_SUPPORT) { function assertSize (size) { if (typeof size !== 'number') { throw new TypeError('"size" argument must be a number') + } else if (size < 0) { + throw new RangeError('"size" argument must not be negative') } } @@ -241,7 +243,7 @@ function fromString (that, string, encoding) { } function fromArrayLike (that, array) { - var length = checked(array.length) | 0 + var length = array.length < 0 ? 0 : checked(array.length) | 0 that = createBuffer(that, length) for (var i = 0; i < length; i += 1) { that[i] = array[i] & 255 @@ -310,7 +312,7 @@ function fromObject (that, obj) { } function checked (length) { - // Note: cannot use `length < kMaxLength` here because that fails when + // Note: cannot use `length < kMaxLength()` here because that fails when // length is NaN (which is otherwise coerced to zero.) if (length >= kMaxLength()) { throw new RangeError('Attempt to allocate Buffer larger than maximum ' +