Skip to content

Commit

Permalink
buffer: use min/max of validateNumber
Browse files Browse the repository at this point in the history
Instead of additional `if` statement, use min/max
of `validateNumber`.

PR-URL: #45796
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
deokjinkim authored Jan 18, 2023
1 parent 3225498 commit 671ffd7
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 14 deletions.
9 changes: 3 additions & 6 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,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 @@ -349,10 +349,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

0 comments on commit 671ffd7

Please sign in to comment.