X Tutup
The Wayback Machine - https://web.archive.org/web/20230219195829/https://github.com/nodejs/node/commit/fc328f1ab0
Skip to content
Permalink
Browse files
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 <ronagy@icloud.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Evan Lucas <evanlucas@me.com>
  • Loading branch information
mihilmy authored and richardlau committed Jan 25, 2022
1 parent 0231ffa commit fc328f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
@@ -399,7 +399,7 @@ async function read(handle, bufferOrOptions, offset, length, position) {
}
offset = bufferOrOptions.offset || 0;
length = buffer.byteLength;
position = bufferOrOptions.position || null;
position = bufferOrOptions.position ?? null;
}

if (offset == null) {
@@ -68,6 +68,24 @@ async function validateReadNoParams() {
}

let useConf = false;
// Validates that the zero position is respected after the position has been
// moved. The test iterates over the xyz chars twice making sure that the values
// are read from the correct position.
async function validateReadWithPositionZero() {
const opts = { useConf: true };
const filePath = fixtures.path('x.txt');
const fileHandle = await open(filePath, 'r');
const expectedSequence = ['x', 'y', 'z'];

for (let i = 0; i < expectedSequence.length * 2; i++) {
const len = 1;
const pos = i % 3;
const buf = Buffer.alloc(len);
const { bytesRead } = await read(fileHandle, buf, 0, len, pos, opts);
assert.strictEqual(bytesRead, len);
assert.strictEqual(buf.toString(), expectedSequence[pos]);
}
}

(async function() {
for (const value of [false, true]) {
@@ -80,4 +98,5 @@ let useConf = false;
.then(common.mustCall());
}
await validateReadNoParams();
await validateReadWithPositionZero();
})().then(common.mustCall());

0 comments on commit fc328f1

Please sign in to comment.
X Tutup