Skip to content

Commit

Permalink
fs: move type checking on fs.fstat to js
Browse files Browse the repository at this point in the history
PR-URL: #17334
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
jasnell committed Dec 13, 2017
1 parent 8974df1 commit 6390968
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ fs.readdirSync = function(path, options) {
};

fs.fstat = function(fd, callback) {
if (typeof fd !== 'number')
if (!Number.isInteger(fd))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
if (fd < 0 || fd > 0xFFFFFFFF)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
Expand Down Expand Up @@ -1011,7 +1011,7 @@ fs.stat = function(path, callback) {
};

fs.fstatSync = function(fd) {
if (typeof fd !== 'number')
if (!Number.isInteger(fd))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
if (fd < 0 || fd > 0xFFFFFFFF)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
Expand Down
5 changes: 1 addition & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,7 @@ static void LStat(const FunctionCallbackInfo<Value>& args) {
static void FStat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 1)
return TYPE_ERROR("fd is required");
if (!args[0]->IsInt32())
return TYPE_ERROR("fd must be a file descriptor");
CHECK(args[0]->IsInt32());

int fd = args[0]->Int32Value();

Expand Down
38 changes: 38 additions & 0 deletions test/parallel/test-fs-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,41 @@ fs.stat(__filename, common.mustCall(function(err, s) {
assert.strictEqual(typeof parsed[k], 'string', `${k} should be a string`);
});
}));

['', false, null, undefined, {}, []].forEach((i) => {
common.expectsError(
() => fs.fstat(i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type number'
}
);
common.expectsError(
() => fs.fstatSync(i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type number'
}
);
});

[-1, 0xFFFFFFFF + 1].forEach((i) => {
common.expectsError(
() => fs.fstat(i),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The "fd" argument is out of range'
}
);
common.expectsError(
() => fs.fstatSync(i),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The "fd" argument is out of range'
}
);
});

0 comments on commit 6390968

Please sign in to comment.