Skip to content

Commit a5a1691

Browse files
mihilmytargos
authored andcommitted
fs: nullish coalescing to respect zero positional reads
When the file read position is moved passing zero is not respected and `null` is used instead. PR fixes the issues by using nullish coalescing which will return the rhs only when the lhs is `null` or `undefined`; respecting the zero. Fixes: #40715 PR-URL: #40716 Fixes: #40699 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent bddb4c6 commit a5a1691

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/internal/fs/promises.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ async function read(handle, bufferOrOptions, offset, length, position) {
521521
}
522522
offset = bufferOrOptions.offset || 0;
523523
length = buffer.byteLength;
524-
position = bufferOrOptions.position || null;
524+
position = bufferOrOptions.position ?? null;
525525
}
526526

527527
if (offset == null) {

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

+20
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ async function validateReadNoParams() {
6868
await fileHandle.read();
6969
}
7070

71+
// Validates that the zero position is respected after the position has been
72+
// moved. The test iterates over the xyz chars twice making sure that the values
73+
// are read from the correct position.
74+
async function validateReadWithPositionZero() {
75+
const opts = { useConf: true };
76+
const filePath = fixtures.path('x.txt');
77+
const fileHandle = await open(filePath, 'r');
78+
const expectedSequence = ['x', 'y', 'z'];
79+
80+
for (let i = 0; i < expectedSequence.length * 2; i++) {
81+
const len = 1;
82+
const pos = i % 3;
83+
const buf = Buffer.alloc(len);
84+
const { bytesRead } = await read(fileHandle, buf, 0, len, pos, opts);
85+
assert.strictEqual(bytesRead, len);
86+
assert.strictEqual(buf.toString(), expectedSequence[pos]);
87+
}
88+
}
89+
7190

7291
(async function() {
7392
tmpdir.refresh();
@@ -78,4 +97,5 @@ async function validateReadNoParams() {
7897
await validateLargeRead({ useConf: false });
7998
await validateLargeRead({ useConf: true });
8099
await validateReadNoParams();
100+
await validateReadWithPositionZero();
81101
})().then(common.mustCall());

0 commit comments

Comments
 (0)