From 2093f13333c5d2537435e2552dec6e60e8c9ae2d Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Fri, 17 Apr 2020 12:13:53 +0530 Subject: [PATCH] fs: update validateOffsetLengthRead in utils.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/32896 Fixes: https://github.com/nodejs/node/issues/32871 Reviewed-By: Anna Henningsen Reviewed-By: Zeyu Yang Reviewed-By: Juan José Arboleda Reviewed-By: Andrey Pechkurov --- lib/internal/fs/utils.js | 12 +++++++----- test/parallel/test-fs-read-type.js | 21 +++++++++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index e143f985a4da99..6f096336f20e10 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -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); } } ); diff --git a/test/parallel/test-fs-read-type.js b/test/parallel/test-fs-read-type.js index 850715c2ce0952..d95c88c47e7c26 100644 --- a/test/parallel/test-fs-read-type.js +++ b/test/parallel/test-fs-read-type.js @@ -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' }); @@ -59,7 +59,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' }); @@ -96,7 +96,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(() => { @@ -109,5 +109,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' });