Skip to content

Commit

Permalink
fs: fix length option being ignored during read()
Browse files Browse the repository at this point in the history
Currently, `length` in an options object is ignored.

PR-URL: #40906
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
fracsinus authored and danielleadams committed Dec 13, 2021
1 parent 93f5bd3 commit e25671c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-fs-promises-file-handle-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -98,4 +107,6 @@ async function validateReadWithPositionZero() {
await validateLargeRead({ useConf: true });
await validateReadNoParams();
await validateReadWithPositionZero();
await validateReadLength(0);
await validateReadLength(1);
})().then(common.mustCall());

0 comments on commit e25671c

Please sign in to comment.