From e8daf89486b5a11f3dfa3b00d9cd6b2ebaa13a61 Mon Sep 17 00:00:00 2001 From: himself65 Date: Wed, 25 Mar 2020 16:36:35 +0800 Subject: [PATCH] lib: fix fs.read when passing null value --- lib/fs.js | 10 ++++++---- test/parallel/test-fs-read.js | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 7a3480a0eca9a8..c3aee1c8171421 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -484,10 +484,12 @@ function read(fd, buffer, offset, length, position, callback) { callback = offset; } - buffer = options.buffer || Buffer.alloc(16384); - offset = options.offset || 0; - length = options.length || buffer.length; - position = options.position; + ({ + buffer = Buffer.alloc(16384), + offset = 0, + length = buffer.length, + position + } = options); } validateBuffer(buffer); diff --git a/test/parallel/test-fs-read.js b/test/parallel/test-fs-read.js index 03ddf7f36d1f98..2b665d8eb96ebb 100644 --- a/test/parallel/test-fs-read.js +++ b/test/parallel/test-fs-read.js @@ -80,6 +80,14 @@ assert.throws( } ); +['buffer', 'offset', 'length'].forEach((option) => + assert.throws( + () => fs.read(fd, { + [option]: null + }), + `not throws when options.${option} is null` + )); + assert.throws( () => fs.read(null, Buffer.alloc(1), 0, 1, 0), {