diff --git a/lib/fs.js b/lib/fs.js index b804336bcf6193..da1ff5036932ae 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -459,7 +459,7 @@ function read(fd, buffer, offset, length, position, callback) { validateOffsetLengthRead(offset, length, buffer.length); - if (!isUint32(position)) + if (!Number.isSafeInteger(position)) position = -1; function wrapper(err, bytesRead) { @@ -489,7 +489,7 @@ function readSync(fd, buffer, offset, length, position) { validateOffsetLengthRead(offset, length, buffer.length); - if (!isUint32(position)) + if (!Number.isSafeInteger(position)) position = -1; const ctx = {}; diff --git a/test/parallel/test-fs-read.js b/test/parallel/test-fs-read.js index f69937b3be5174..864876537c22c6 100644 --- a/test/parallel/test-fs-read.js +++ b/test/parallel/test-fs-read.js @@ -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); + })); +}