From b8b8e82591392b658f5ba2e1ce7ac7fdd5825274 Mon Sep 17 00:00:00 2001 From: himself65 Date: Wed, 25 Mar 2020 16:36:35 +0800 Subject: [PATCH] fs: fix fs.read when passing null value PR-URL: https://github.com/nodejs/node/pull/32479 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Evan Lucas --- 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 5896c9484b3509..09e6cba80446da 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -485,10 +485,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), {