From 80c1d498e2d54caa733b6c39f232693cd94d6801 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 17 Aug 2016 20:20:34 -0700 Subject: [PATCH] throw on negative .allocUnsafe() argument To match new behavior in node v6.4.0 https://github.com/nodejs/node/commit/8f90dcc1b8e4ac3d8597ea2ee3927f325c c980d3 --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 ' + -- 2.34.1