Skip to content

Commit

Permalink
buffer: harden SlowBuffer creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ZYSzys committed Mar 10, 2019
1 parent 6e81a95 commit c9cf8b1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 0 additions & 2 deletions lib/buffer.js
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
23 changes: 13 additions & 10 deletions test/parallel/test-buffer-slow.js
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
// 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);

0 comments on commit c9cf8b1

Please sign in to comment.