Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit c86e3d3

Browse files
committed
fix: fs.readSync with large position value
Backport of nodejs/node#21003; fixed after 10.3.0 so this does not also need to be applied to the 10.6.0 upgrade
1 parent ed9e26b commit c86e3d3

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
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: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,40 @@ const expected = Buffer.from('xyz\n');
3131

3232
function test(bufferAsync, bufferSync, expected) {
3333
fs.read(fd,
34-
bufferAsync,
35-
0,
36-
expected.length,
37-
0,
38-
common.mustCall((err, bytesRead) => {
39-
assert.ifError(err);
40-
assert.strictEqual(bytesRead, expected.length);
41-
assert.deepStrictEqual(bufferAsync, expected);
42-
}));
34+
bufferAsync,
35+
0,
36+
expected.length,
37+
0,
38+
common.mustCall((err, bytesRead) => {
39+
assert.ifError(err);
40+
assert.strictEqual(bytesRead, expected.length);
41+
assert.deepStrictEqual(bufferAsync, expected);
42+
}));
4343

4444
const r = fs.readSync(fd, bufferSync, 0, expected.length, 0);
4545
assert.deepStrictEqual(bufferSync, expected);
4646
assert.strictEqual(r, expected.length);
4747
}
4848

4949
test(Buffer.allocUnsafe(expected.length),
50-
Buffer.allocUnsafe(expected.length),
51-
expected);
50+
Buffer.allocUnsafe(expected.length),
51+
expected);
5252

5353
test(new Uint8Array(expected.length),
54-
new Uint8Array(expected.length),
55-
Uint8Array.from(expected));
54+
new Uint8Array(expected.length),
55+
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)