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: #40716Fixes: #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>
1 parent bddb4c6 commit a5a1691

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎lib/internal/fs/promises.js‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ async function validateReadNoParams() {
6868
awaitfileHandle.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+
asyncfunctionvalidateReadWithPositionZero(){
75+
constopts={useConf: true};
76+
constfilePath=fixtures.path('x.txt');
77+
constfileHandle=awaitopen(filePath,'r');
78+
constexpectedSequence=['x','y','z'];
79+
80+
for(leti=0;i<expectedSequence.length*2;i++){
81+
constlen=1;
82+
constpos=i%3;
83+
constbuf=Buffer.alloc(len);
84+
const{ bytesRead }=awaitread(fileHandle,buf,0,len,pos,opts);
85+
assert.strictEqual(bytesRead,len);
86+
assert.strictEqual(buf.toString(),expectedSequence[pos]);
87+
}
88+
}
89+
7190

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

0 commit comments

Comments
 (0)