From e25671cddb369ffd6c47072ae12554a674508741 Mon Sep 17 00:00:00 2001 From: Shinho Ahn Date: Fri, 10 Dec 2021 19:26:37 +0900 Subject: [PATCH] fs: fix `length` option being ignored during `read()` Currently, `length` in an options object is ignored. PR-URL: https://github.com/nodejs/node/pull/40906 Reviewed-By: Robert Nagy Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca --- 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 b92f189c54c1aa..6a0ce4d05c6b87 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; } diff --git a/test/parallel/test-fs-promises-file-handle-read.js b/test/parallel/test-fs-promises-file-handle-read.js index b79d65e9484a1f..34a70c61d7e445 100644 --- a/test/parallel/test-fs-promises-file-handle-read.js +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -87,6 +87,15 @@ async function validateReadWithPositionZero() { } } +async function validateReadLength(len) { + 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 +107,6 @@ async function validateReadWithPositionZero() { await validateLargeRead({ useConf: true }); await validateReadNoParams(); await validateReadWithPositionZero(); + await validateReadLength(0); + await validateReadLength(1); })().then(common.mustCall());