Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand All @@ -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();
Expand Down
9 changes: 8 additions & 1 deletion test/parallel/test-fs-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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',
}
);