From bfe6dc369d9a2ffd093b988a8f56b31a77571264 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Mon, 28 May 2018 19:00:42 +0200 Subject: [PATCH] fs: fix reads with pos > 4GB PR-URL: https://github.com/nodejs/node/pull/21003 Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum Reviewed-By: Trivikram Kamat Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater --- lib/fs.js | 4 ++-- test/parallel/test-fs-read.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index bf6f93ebb7bf05..646772a813e127 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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) { @@ -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 = {}; 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); + })); +}