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: use min/max of validateNumber #45796

Merged
merged 1 commit into from
Jan 18, 2023
Merged
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
9 changes: 3 additions & 6 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ const {
const {
validateArray,
validateBuffer,
validateNumber,
validateInteger,
validateString
validateNumber,
validateString,
} = require('internal/validators');
// Provide validateInteger() but with kMaxLength as the default maximum value.
const validateOffset = (value, name, min = 0, max = kMaxLength) =>
Expand Down Expand Up @@ -356,10 +356,7 @@ ObjectSetPrototypeOf(Buffer, Uint8Array);
// occurs. This is done simply to keep the internal details of the
// implementation from bleeding out to users.
const assertSize = hideStackFrames((size) => {
validateNumber(size, 'size');
if (!(size >= 0 && size <= kMaxLength)) {
throw new ERR_INVALID_ARG_VALUE.RangeError('size', size);
}
validateNumber(size, 'size', 0, kMaxLength);
});

/**
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-buffer-no-negative-allocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ const assert = require('assert');
const { SlowBuffer } = require('buffer');

const msg = {
code: 'ERR_INVALID_ARG_VALUE',
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /^The argument 'size' is invalid\. Received [^"]*$/
};

// Test that negative Buffer length inputs throw errors.
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-buffer-over-max-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ const SlowBuffer = buffer.SlowBuffer;

const kMaxLength = buffer.kMaxLength;
const bufferMaxSizeMsg = {
code: 'ERR_INVALID_ARG_VALUE',
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /^The argument 'size' is invalid\. Received [^"]*$/
};

assert.throws(() => Buffer((-1 >>> 0) + 2), bufferMaxSizeMsg);
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-buffer-slow.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ assert.throws(() => SlowBuffer(true), bufferInvalidTypeMsg);

// Should throw with invalid length value
const bufferMaxSizeMsg = {
code: 'ERR_INVALID_ARG_VALUE',
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /^The argument 'size' is invalid\. Received [^"]*$/
};
assert.throws(() => SlowBuffer(NaN), bufferMaxSizeMsg);
assert.throws(() => SlowBuffer(Infinity), bufferMaxSizeMsg);
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-buffer-tostring-rangeerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ const SlowBuffer = require('buffer').SlowBuffer;

const len = 1422561062959;
const message = {
code: 'ERR_INVALID_ARG_VALUE',
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /^The argument 'size' is invalid\. Received [^"]*$/
};
assert.throws(() => Buffer(len).toString('utf8'), message);
assert.throws(() => SlowBuffer(len).toString('utf8'), message);
Expand Down