Skip to content

Commit 1dae526

Browse files
committed
fs: fix reads with pos > 4GB
PR-URL: #21003 Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 9f4bf4c commit 1dae526

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ function read(fd, buffer, offset, length, position, callback) {
459459

460460
validateOffsetLengthRead(offset, length, buffer.length);
461461

462-
if (!isUint32(position))
462+
if (!Number.isSafeInteger(position))
463463
position = -1;
464464

465465
function wrapper(err, bytesRead) {
@@ -489,7 +489,7 @@ function readSync(fd, buffer, offset, length, position) {
489489

490490
validateOffsetLengthRead(offset, length, buffer.length);
491491

492-
if (!isUint32(position))
492+
if (!Number.isSafeInteger(position))
493493
position = -1;
494494

495495
const ctx = {};

test/parallel/test-fs-read.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,18 @@ test(Buffer.allocUnsafe(expected.length),
5353
test(new Uint8Array(expected.length),
5454
new Uint8Array(expected.length),
5555
Uint8Array.from(expected));
56+
57+
{
58+
// Reading beyond file length (3 in this case) should return no data.
59+
// This is a test for a bug where reads > uint32 would return data
60+
// from the current position in the file.
61+
const fd = fs.openSync(filepath, 'r');
62+
const pos = 0xffffffff + 1; // max-uint32 + 1
63+
const nRead = fs.readSync(fd, Buffer.alloc(1), 0, 1, pos);
64+
assert.strictEqual(nRead, 0);
65+
66+
fs.read(fd, Buffer.alloc(1), 0, 1, pos, common.mustCall((err, nRead) => {
67+
assert.ifError(err);
68+
assert.strictEqual(nRead, 0);
69+
}));
70+
}

0 commit comments

Comments
 (0)