Skip to content

Commit 8fdb45b

Browse files
fracsinusronag
andcommitted
fs: use nullish coalescing to respect zero-length reads
Co-authored-by: Robert Nagy <ronagy@icloud.com>
1 parent 4948c56 commit 8fdb45b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/internal/fs/promises.js

Lines changed: 1 addition & 1 deletion
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 = bufferOrOptions.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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ async function validateReadWithPositionZero() {
8787
}
8888
}
8989

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

91101
(async function() {
92102
tmpdir.refresh();
@@ -98,4 +108,5 @@ async function validateReadWithPositionZero() {
98108
await validateLargeRead({ useConf: true });
99109
await validateReadNoParams();
100110
await validateReadWithPositionZero();
111+
await validateReadLength();
101112
})().then(common.mustCall());

0 commit comments

Comments
 (0)