Skip to content

Commit 6390968

Browse files
committed
fs: move type checking on fs.fstat to js
PR-URL: #17334 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 8974df1 commit 6390968

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

lib/fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ fs.readdirSync = function(path, options) {
981981
};
982982

983983
fs.fstat = function(fd, callback) {
984-
if (typeof fd !== 'number')
984+
if (!Number.isInteger(fd))
985985
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
986986
if (fd < 0 || fd > 0xFFFFFFFF)
987987
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
@@ -1011,7 +1011,7 @@ fs.stat = function(path, callback) {
10111011
};
10121012

10131013
fs.fstatSync = function(fd) {
1014-
if (typeof fd !== 'number')
1014+
if (!Number.isInteger(fd))
10151015
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
10161016
if (fd < 0 || fd > 0xFFFFFFFF)
10171017
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');

src/node_file.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,7 @@ static void LStat(const FunctionCallbackInfo<Value>& args) {
634634
static void FStat(const FunctionCallbackInfo<Value>& args) {
635635
Environment* env = Environment::GetCurrent(args);
636636

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

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

test/parallel/test-fs-stat.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,41 @@ fs.stat(__filename, common.mustCall(function(err, s) {
130130
assert.strictEqual(typeof parsed[k], 'string', `${k} should be a string`);
131131
});
132132
}));
133+
134+
['', false, null, undefined, {}, []].forEach((i) => {
135+
common.expectsError(
136+
() => fs.fstat(i),
137+
{
138+
code: 'ERR_INVALID_ARG_TYPE',
139+
type: TypeError,
140+
message: 'The "fd" argument must be of type number'
141+
}
142+
);
143+
common.expectsError(
144+
() => fs.fstatSync(i),
145+
{
146+
code: 'ERR_INVALID_ARG_TYPE',
147+
type: TypeError,
148+
message: 'The "fd" argument must be of type number'
149+
}
150+
);
151+
});
152+
153+
[-1, 0xFFFFFFFF + 1].forEach((i) => {
154+
common.expectsError(
155+
() => fs.fstat(i),
156+
{
157+
code: 'ERR_OUT_OF_RANGE',
158+
type: RangeError,
159+
message: 'The "fd" argument is out of range'
160+
}
161+
);
162+
common.expectsError(
163+
() => fs.fstatSync(i),
164+
{
165+
code: 'ERR_OUT_OF_RANGE',
166+
type: RangeError,
167+
message: 'The "fd" argument is out of range'
168+
}
169+
);
170+
});

0 commit comments

Comments
 (0)