buffer: use min/max of `validateNumber`

Instead of additional `if` statement, use min/max
of `validateNumber`.

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Deokjin Kim 2023-01-18 19:36:27 +09:00 committed by GitHub
parent 32254988ba
commit 671ffd7825
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 7 additions and 14 deletions

View File

@ -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) =>
@ -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);
});
/**

View File

@ -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.

View File

@ -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);

View File

@ -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);

View File

@ -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);