From b1b8d3ff17201396bd4d66d6598402910e8f4fe8 Mon Sep 17 00:00:00 2001 From: wafuwafu13 Date: Sat, 24 Sep 2022 21:50:18 +0900 Subject: [PATCH 1/3] fs: add validateBuffer to improve error --- lib/fs.js | 1 + test/parallel/test-fs-read.js | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/fs.js b/lib/fs.js index 4862b8cd698018..f402513b0b03cf 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -619,6 +619,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) { // This is fs.read(fd, params, callback) params = buffer; ({ buffer = Buffer.alloc(16384) } = params ?? kEmptyObject); + validateBuffer(buffer); } callback = offsetOrOptions; } else { diff --git a/test/parallel/test-fs-read.js b/test/parallel/test-fs-read.js index 2b3dab4f243b90..294f8ce3e048c3 100644 --- a/test/parallel/test-fs-read.js +++ b/test/parallel/test-fs-read.js @@ -79,7 +79,11 @@ assert.throws( assert.throws( () => fs.read(fd, { buffer: null }, common.mustNotCall()), - /TypeError: Cannot read properties of null \(reading 'byteLength'\)/, + { + name: 'TypeError', + message: 'The "buffer" argument must be an instance of Buffer, ' + + 'TypedArray, or DataView. Received null', + }, 'throws when options.buffer is null' ); From 9c62ea804e4afb496def9890a0b0647196428683 Mon Sep 17 00:00:00 2001 From: wafuwafu13 Date: Sat, 24 Sep 2022 22:03:19 +0900 Subject: [PATCH 2/3] test: not validate the message, but rather the error code --- test/parallel/test-fs-read.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/parallel/test-fs-read.js b/test/parallel/test-fs-read.js index 294f8ce3e048c3..966185c5138d63 100644 --- a/test/parallel/test-fs-read.js +++ b/test/parallel/test-fs-read.js @@ -79,11 +79,7 @@ assert.throws( assert.throws( () => fs.read(fd, { buffer: null }, common.mustNotCall()), - { - name: 'TypeError', - message: 'The "buffer" argument must be an instance of Buffer, ' + - 'TypedArray, or DataView. Received null', - }, + { code: 'ERR_INVALID_ARG_TYPE' }, 'throws when options.buffer is null' ); From 888beffafe292c228c0e31d4231d6e4413d97569 Mon Sep 17 00:00:00 2001 From: wafuwafu13 Date: Sun, 25 Sep 2022 17:17:33 +0900 Subject: [PATCH 3/3] fix: use optional chaining instead of validateBuffer --- lib/fs.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index f402513b0b03cf..fc9b630e81208c 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -619,7 +619,6 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) { // This is fs.read(fd, params, callback) params = buffer; ({ buffer = Buffer.alloc(16384) } = params ?? kEmptyObject); - validateBuffer(buffer); } callback = offsetOrOptions; } else { @@ -633,7 +632,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) { } ({ offset = 0, - length = buffer.byteLength - offset, + length = buffer?.byteLength - offset, position = null, } = params ?? kEmptyObject); }