Skip to content

Commit bfe6dc3

Browse files
mafintoshMylesBorins
authored andcommitted
fs: fix reads with pos > 4GB
PR-URL: #21003 Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent d09bec8 commit bfe6dc3

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
@@ -593,7 +593,7 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
593593

594594
validateOffsetLengthRead(offset, length, buffer.length);
595595

596-
if (!isUint32(position))
596+
if (!Number.isSafeInteger(position))
597597
position = -1;
598598

599599
function wrapper(err, bytesRead) {
@@ -623,7 +623,7 @@ fs.readSync = function(fd, buffer, offset, length, position) {
623623

624624
validateOffsetLengthRead(offset, length, buffer.length);
625625

626-
if (!isUint32(position))
626+
if (!Number.isSafeInteger(position))
627627
position = -1;
628628

629629
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)