Skip to content

Commit

Permalink
buffer: harden SlowBuffer creation
Browse files Browse the repository at this point in the history
PR-URL: #26272
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
ZYSzys authored and danbev committed Mar 11, 2019
1 parent 560466c commit 6113ba9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
2 changes: 0 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
29 changes: 16 additions & 13 deletions test/parallel/test-buffer-slow.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const common = require('../common');
require('../common');
const assert = require('assert');
const buffer = require('buffer');
const SlowBuffer = buffer.SlowBuffer;
Expand Down Expand Up @@ -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
const bufferMaxSizeMsg = common.expectsError({
// Should throw with invalid length type
const bufferInvalidTypeMsg = {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: /^The "size" argument must be of type number/,
};
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 = {
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError,
name: 'RangeError [ERR_INVALID_OPT_VALUE]',
message: /^The value "[^"]*" is invalid for option "size"$/
}, 7);

assert.throws(() => SlowBuffer(), bufferMaxSizeMsg);
};
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);

0 comments on commit 6113ba9

Please sign in to comment.