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
6 changes: 6 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ function read(fd, buffer, offset, length, position, callback) {

validateOffsetLengthRead(offset, length, buffer.byteLength);

if (typeof position === 'bigint')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a check that can be performed early instead of being this late? Before validateBuffer would be a good place I think.

Copy link
Author

@darvesh darvesh Nov 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to have arguments validation in order?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern was mostly about the if (length === 0) above which means if the length is 0 you'd be able to get away with these checks..but thinking again it was already the case for a few other cases above, so it's probably not a bit deal.

throw new ERR_INVALID_ARG_TYPE('position', 'number', position);

if (!NumberIsSafeInteger(position))
position = -1;

Expand Down Expand Up @@ -595,6 +598,9 @@ function readSync(fd, buffer, offset, length, position) {

validateOffsetLengthRead(offset, length, buffer.byteLength);

if (typeof position === 'bigint')
throw new ERR_INVALID_ARG_TYPE('position', 'number', position);

if (!NumberIsSafeInteger(position))
position = -1;

Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-fs-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,10 @@ assert.throws(
code: 'ERR_INVALID_ARG_TYPE',
}
);

assert.throws(() => fs.read(fd, Buffer.alloc(1), 0, 1, 1n, () => {}), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message:
'The "position" argument must be of type number. Received type bigint (1n)',
});