Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: fix length option being ignored during read() #40906

Merged
merged 3 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());