Skip to content
Open
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
1 change: 1 addition & 0 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,7 @@ async function read(handle, bufferOrParams, offset, length, position) {
}

length ??= buffer.byteLength - offset;
length |= 0;

if (position == null) {
position = -1;
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-fs-promises-file-handle-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ async function validateReadLength(len) {
}
}

async function validateReadLengthCoercedFromString() {
// Align with fs.read / fs.readSync (`length |= 0`). A non-number length
// must not reach node::fs::Read (CHECK args[3]->IsInt32()).
const buf = Buffer.alloc(4);
const filePath = fixtures.path('x.txt');
const fileHandle = await open(filePath, 'r');
try {
const { bytesRead } = await fileHandle.read(buf, 0, '1', 0);
assert.strictEqual(bytesRead, 1);
const { bytesRead: bytesReadOptions } = await fileHandle.read({
buffer: buf,
offset: 0,
length: '1',
position: 0,
});
assert.strictEqual(bytesReadOptions, 1);
} finally {
await fileHandle.close();
}
}

async function validateReadWithNoOptions(byte) {
const buf = Buffer.alloc(byte);
const filePath = fixtures.path('x.txt');
Expand Down Expand Up @@ -144,6 +165,7 @@ async function validateReadWithNoOptions(byte) {
await validateReadWithPositionZero();
await validateReadLength(0);
await validateReadLength(1);
await validateReadLengthCoercedFromString();
await validateReadWithNoOptions(0);
await validateReadWithNoOptions(1);
})().then(common.mustCall());
Loading