fs: fix reads with pos > 4GB

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Mathias Buus 2018-05-28 19:00:42 +02:00
parent 9f4bf4ca43
commit 1dae526348
2 changed files with 17 additions and 2 deletions

View File

@ -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 = {};

View File

@ -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);
}));
}