Skip to content

Commit e25671c

Browse files
fracsinusdanielleadams
authored andcommitted
fs: fix length option being ignored during read()
Currently, `length` in an options object is ignored. PR-URL: #40906 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 93f5bd3 commit e25671c

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/internal/fs/promises.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ async function read(handle, bufferOrOptions, offset, length, position) {
520520
buffer = Buffer.alloc(16384);
521521
}
522522
offset = bufferOrOptions.offset || 0;
523-
length = buffer.byteLength;
523+
length = bufferOrOptions.length ?? buffer.byteLength;
524524
position = bufferOrOptions.position ?? null;
525525
}
526526

test/parallel/test-fs-promises-file-handle-read.js

+11
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ async function validateReadWithPositionZero() {
8787
}
8888
}
8989

90+
async function validateReadLength(len) {
91+
const buf = Buffer.alloc(4);
92+
const opts = { useConf: true };
93+
const filePath = fixtures.path('x.txt');
94+
const fileHandle = await open(filePath, 'r');
95+
const { bytesRead } = await read(fileHandle, buf, 0, len, 0, opts);
96+
assert.strictEqual(bytesRead, len);
97+
}
98+
9099

91100
(async function() {
92101
tmpdir.refresh();
@@ -98,4 +107,6 @@ async function validateReadWithPositionZero() {
98107
await validateLargeRead({ useConf: true });
99108
await validateReadNoParams();
100109
await validateReadWithPositionZero();
110+
await validateReadLength(0);
111+
await validateReadLength(1);
101112
})().then(common.mustCall());

0 commit comments

Comments
 (0)