Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: harden SlowBuffer creation #26272

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
ZYSzys marked this conversation as resolved.
Show resolved Hide resolved
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);