From c9cf8b1d71655354236f3d6f9d9b79cfce06dfa8 Mon Sep 17 00:00:00 2001 From: ZYSzys Date: Mon, 4 Mar 2019 11:02:53 +0800 Subject: [PATCH 1/2] buffer: harden SlowBuffer creation --- lib/buffer.js | 2 -- test/parallel/test-buffer-slow.js | 23 +++++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 9684eaa6baeff0..c5497e18aa66d3 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -297,8 +297,6 @@ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) { // If --zero-fill-buffers command line argument is set, a zero-filled // buffer is returned. function SlowBuffer(length) { - if (typeof length !== 'number') - length = +length; assertSize(length); return createUnsafeBuffer(length); } diff --git a/test/parallel/test-buffer-slow.js b/test/parallel/test-buffer-slow.js index 8317d8c5eece7f..5c5f9bff130310 100644 --- a/test/parallel/test-buffer-slow.js +++ b/test/parallel/test-buffer-slow.js @@ -39,21 +39,24 @@ try { assert.strictEqual(e.name, 'RangeError'); } -// Should work with number-coercible values -assert.strictEqual(SlowBuffer('6').length, 6); -assert.strictEqual(SlowBuffer(true).length, 1); - -// Should throw with invalid length +// Should throw with invalid length type +const bufferInvalidTypeMsg = common.expectsError({ + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: /^The "size" argument must be of type number/, +}, 4); +assert.throws(() => SlowBuffer(), bufferInvalidTypeMsg); +assert.throws(() => SlowBuffer({}), bufferInvalidTypeMsg); +assert.throws(() => SlowBuffer('6'), bufferInvalidTypeMsg); +assert.throws(() => SlowBuffer(true), bufferInvalidTypeMsg); + +// Should throw with invalid length value const bufferMaxSizeMsg = common.expectsError({ code: 'ERR_INVALID_OPT_VALUE', type: RangeError, message: /^The value "[^"]*" is invalid for option "size"$/ -}, 7); - -assert.throws(() => SlowBuffer(), bufferMaxSizeMsg); +}, 4); assert.throws(() => SlowBuffer(NaN), bufferMaxSizeMsg); -assert.throws(() => SlowBuffer({}), bufferMaxSizeMsg); -assert.throws(() => SlowBuffer('string'), bufferMaxSizeMsg); assert.throws(() => SlowBuffer(Infinity), bufferMaxSizeMsg); assert.throws(() => SlowBuffer(-1), bufferMaxSizeMsg); assert.throws(() => SlowBuffer(buffer.kMaxLength + 1), bufferMaxSizeMsg); From 78cbc3a23a96940df59b31e8334f7734b994c22f Mon Sep 17 00:00:00 2001 From: ZYSzys Date: Wed, 6 Mar 2019 08:32:40 +0800 Subject: [PATCH 2/2] fixup! buffer: harden SlowBuffer creation --- test/parallel/test-buffer-slow.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/parallel/test-buffer-slow.js b/test/parallel/test-buffer-slow.js index 5c5f9bff130310..c9f5f17841b5db 100644 --- a/test/parallel/test-buffer-slow.js +++ b/test/parallel/test-buffer-slow.js @@ -1,6 +1,6 @@ 'use strict'; -const common = require('../common'); +require('../common'); const assert = require('assert'); const buffer = require('buffer'); const SlowBuffer = buffer.SlowBuffer; @@ -40,22 +40,22 @@ try { } // Should throw with invalid length type -const bufferInvalidTypeMsg = common.expectsError({ +const bufferInvalidTypeMsg = { code: 'ERR_INVALID_ARG_TYPE', - type: TypeError, + name: 'TypeError [ERR_INVALID_ARG_TYPE]', message: /^The "size" argument must be of type number/, -}, 4); +}; assert.throws(() => SlowBuffer(), bufferInvalidTypeMsg); assert.throws(() => SlowBuffer({}), bufferInvalidTypeMsg); assert.throws(() => SlowBuffer('6'), bufferInvalidTypeMsg); assert.throws(() => SlowBuffer(true), bufferInvalidTypeMsg); // Should throw with invalid length value -const bufferMaxSizeMsg = common.expectsError({ +const bufferMaxSizeMsg = { code: 'ERR_INVALID_OPT_VALUE', - type: RangeError, + name: 'RangeError [ERR_INVALID_OPT_VALUE]', message: /^The value "[^"]*" is invalid for option "size"$/ -}, 4); +}; assert.throws(() => SlowBuffer(NaN), bufferMaxSizeMsg); assert.throws(() => SlowBuffer(Infinity), bufferMaxSizeMsg); assert.throws(() => SlowBuffer(-1), bufferMaxSizeMsg);