diff --git a/lib/fs.js b/lib/fs.js index 6badc808d2b476..7517f275c82ff3 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -445,13 +445,14 @@ function openSync(path, flags, mode) { function read(fd, buffer, offset, length, position, callback) { validateUint32(fd, 'fd'); validateBuffer(buffer); + callback = maybeCallback(callback); offset |= 0; length |= 0; if (length === 0) { return process.nextTick(function tick() { - callback && callback(null, 0, buffer); + callback(null, 0, buffer); }); } @@ -467,7 +468,7 @@ function read(fd, buffer, offset, length, position, callback) { function wrapper(err, bytesRead) { // Retain a reference to buffer so that it can't be GC'ed too soon. - callback && callback(err, bytesRead || 0, buffer); + callback(err, bytesRead || 0, buffer); } const req = new FSReqCallback(); diff --git a/test/parallel/test-fs-read.js b/test/parallel/test-fs-read.js index 864876537c22c6..5843cdc8365024 100644 --- a/test/parallel/test-fs-read.js +++ b/test/parallel/test-fs-read.js @@ -58,7 +58,6 @@ test(new Uint8Array(expected.length), // 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); @@ -68,3 +67,11 @@ test(new Uint8Array(expected.length), assert.strictEqual(nRead, 0); })); } + +assert.throws( + () => fs.read(fd, Buffer.alloc(1), 0, 1, 0), + { + message: 'Callback must be a function', + code: 'ERR_INVALID_CALLBACK', + } +);