Skip to content

Commit

Permalink
fs: fix reads with pos > 4GB
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mafintosh authored and MylesBorins committed May 29, 2018
1 parent d09bec8 commit bfe6dc3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ fs.read = function(fd, buffer, offset, length, position, callback) {

validateOffsetLengthRead(offset, length, buffer.length);

if (!isUint32(position))
if (!Number.isSafeInteger(position))
position = -1;

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

validateOffsetLengthRead(offset, length, buffer.length);

if (!isUint32(position))
if (!Number.isSafeInteger(position))
position = -1;

const ctx = {};
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-fs-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,18 @@ test(Buffer.allocUnsafe(expected.length),
test(new Uint8Array(expected.length),
new Uint8Array(expected.length),
Uint8Array.from(expected));

{
// Reading beyond file length (3 in this case) should return no data.
// This is a test for a bug where reads > uint32 would return data
// from the current position in the file.
const fd = fs.openSync(filepath, 'r');
const pos = 0xffffffff + 1; // max-uint32 + 1
const nRead = fs.readSync(fd, Buffer.alloc(1), 0, 1, pos);
assert.strictEqual(nRead, 0);

fs.read(fd, Buffer.alloc(1), 0, 1, pos, common.mustCall((err, nRead) => {
assert.ifError(err);
assert.strictEqual(nRead, 0);
}));
}

0 comments on commit bfe6dc3

Please sign in to comment.