From bb4891d8d465c951e0ab1c10346251f61f60e8c4 Mon Sep 17 00:00:00 2001 From: Hirotaka Tagawa / wafuwafu13 Date: Wed, 28 Sep 2022 17:44:39 +0900 Subject: [PATCH] fs: add validateBuffer to improve error PR-URL: https://github.com/nodejs/node/pull/44769 Reviewed-By: LiviaMedeiros Reviewed-By: Antoine du Hamel Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca --- lib/fs.js | 2 +- test/parallel/test-fs-read.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 4862b8cd698018..fc9b630e81208c 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -632,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); } diff --git a/test/parallel/test-fs-read.js b/test/parallel/test-fs-read.js index 2b3dab4f243b90..966185c5138d63 100644 --- a/test/parallel/test-fs-read.js +++ b/test/parallel/test-fs-read.js @@ -79,7 +79,7 @@ assert.throws( assert.throws( () => fs.read(fd, { buffer: null }, common.mustNotCall()), - /TypeError: Cannot read properties of null \(reading 'byteLength'\)/, + { code: 'ERR_INVALID_ARG_TYPE' }, 'throws when options.buffer is null' );