fs: update validateOffsetLengthRead in utils.js

PR-URL: https://github.com/nodejs/node/pull/32896
Fixes: https://github.com/nodejs/node/issues/32871
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
This commit is contained in:
daemon1024 2020-04-17 12:13:53 +05:30 committed by Andrey Pechkurov
parent 6a07eca49c
commit 1cc1ca4297
2 changed files with 24 additions and 9 deletions

View File

@ -539,13 +539,15 @@ function toUnixTimestamp(time, name = 'time') {
const validateOffsetLengthRead = hideStackFrames(
(offset, length, bufferLength) => {
if (offset < 0 || offset >= bufferLength) {
throw new ERR_OUT_OF_RANGE('offset',
`>= 0 && <= ${bufferLength}`, offset);
if (offset < 0) {
throw new ERR_OUT_OF_RANGE('offset', '>= 0', offset);
}
if (length < 0 || offset + length > bufferLength) {
if (length < 0) {
throw new ERR_OUT_OF_RANGE('length', '>= 0', length);
}
if (offset + length > bufferLength) {
throw new ERR_OUT_OF_RANGE('length',
`>= 0 && <= ${bufferLength - offset}`, length);
`<= ${bufferLength - offset}`, length);
}
}
);

View File

@ -44,7 +44,7 @@ assert.throws(() => {
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "offset" is out of range. It must be >= 0 && <= 4. ' +
message: 'The value of "offset" is out of range. It must be >= 0. ' +
'Received -1'
});
@ -73,7 +73,7 @@ assert.throws(() => {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "length" is out of range. ' +
'It must be >= 0 && <= 4. Received -1'
'It must be >= 0. Received -1'
});
@ -110,7 +110,7 @@ assert.throws(() => {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "offset" is out of range. ' +
'It must be >= 0 && <= 4. Received -1'
'It must be >= 0. Received -1'
});
assert.throws(() => {
@ -136,5 +136,18 @@ assert.throws(() => {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "length" is out of range. ' +
'It must be >= 0 && <= 4. Received -1'
'It must be >= 0. Received -1'
});
assert.throws(() => {
fs.readSync(fd,
Buffer.allocUnsafe(expected.length),
0,
expected.length + 1,
0);
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "length" is out of range. ' +
'It must be <= 4. Received 5'
});