Skip to content

Commit

Permalink
fs: update validateOffsetLengthRead in utils.js
Browse files Browse the repository at this point in the history
PR-URL: #32896
Fixes: #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>
  • Loading branch information
daemon1024 authored and BridgeAR committed Apr 28, 2020
1 parent a673c8f commit 27837fe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
12 changes: 7 additions & 5 deletions lib/internal/fs/utils.js
Expand Up @@ -506,13 +506,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);
}
}
);
Expand Down
21 changes: 17 additions & 4 deletions test/parallel/test-fs-read-type.js
Expand Up @@ -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'
});

Expand Down Expand Up @@ -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'
});


Expand Down Expand Up @@ -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(() => {
Expand All @@ -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'
});

0 comments on commit 27837fe

Please sign in to comment.