From 4948c56b75d55bd25fb4a3e35d8b6b2449a92768 Mon Sep 17 00:00:00 2001 From: Shinho Ahn Date: Sun, 21 Nov 2021 20:40:13 +0900 Subject: [PATCH 1/3] fs: fix `length` option being ignored during `read()` Currently, `length` in an options object is ignored. --- lib/internal/fs/promises.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 5b3482b99b26ee..e75b49eaa508be 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -520,7 +520,7 @@ async function read(handle, bufferOrOptions, offset, length, position) { buffer = Buffer.alloc(16384); } offset = bufferOrOptions.offset || 0; - length = buffer.byteLength; + length = bufferOrOptions.length || buffer.byteLength; position = bufferOrOptions.position ?? null; } From 8fdb45bf948bc9e9e933859281d8bccec510466a Mon Sep 17 00:00:00 2001 From: Shinho Ahn Date: Mon, 22 Nov 2021 18:50:19 +0900 Subject: [PATCH 2/3] fs: use nullish coalescing to respect zero-length reads Co-authored-by: Robert Nagy --- lib/internal/fs/promises.js | 2 +- test/parallel/test-fs-promises-file-handle-read.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index e75b49eaa508be..b28bfe918a4e59 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -520,7 +520,7 @@ async function read(handle, bufferOrOptions, offset, length, position) { buffer = Buffer.alloc(16384); } offset = bufferOrOptions.offset || 0; - length = bufferOrOptions.length || buffer.byteLength; + length = bufferOrOptions.length ?? buffer.byteLength; position = bufferOrOptions.position ?? null; } diff --git a/test/parallel/test-fs-promises-file-handle-read.js b/test/parallel/test-fs-promises-file-handle-read.js index b79d65e9484a1f..fb309b56ddc4c4 100644 --- a/test/parallel/test-fs-promises-file-handle-read.js +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -87,6 +87,16 @@ async function validateReadWithPositionZero() { } } +async function validateReadLength() { + const len = 1; + const buf = Buffer.alloc(4); + const opts = { useConf: true }; + const filePath = fixtures.path('x.txt'); + const fileHandle = await open(filePath, 'r'); + const { bytesRead } = await read(fileHandle, buf, 0, len, 0, opts); + assert.strictEqual(bytesRead, len); +} + (async function() { tmpdir.refresh(); @@ -98,4 +108,5 @@ async function validateReadWithPositionZero() { await validateLargeRead({ useConf: true }); await validateReadNoParams(); await validateReadWithPositionZero(); + await validateReadLength(); })().then(common.mustCall()); From 3d496879b539bca167236048a5101fac725f9f1c Mon Sep 17 00:00:00 2001 From: Shinho Ahn Date: Tue, 23 Nov 2021 09:50:54 +0900 Subject: [PATCH 3/3] fs: add test for zero-length read Co-authored-by: Antoine du Hamel --- test/parallel/test-fs-promises-file-handle-read.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-fs-promises-file-handle-read.js b/test/parallel/test-fs-promises-file-handle-read.js index fb309b56ddc4c4..34a70c61d7e445 100644 --- a/test/parallel/test-fs-promises-file-handle-read.js +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -87,8 +87,7 @@ async function validateReadWithPositionZero() { } } -async function validateReadLength() { - const len = 1; +async function validateReadLength(len) { const buf = Buffer.alloc(4); const opts = { useConf: true }; const filePath = fixtures.path('x.txt'); @@ -108,5 +107,6 @@ async function validateReadLength() { await validateLargeRead({ useConf: true }); await validateReadNoParams(); await validateReadWithPositionZero(); - await validateReadLength(); + await validateReadLength(0); + await validateReadLength(1); })().then(common.mustCall());