Skip to content

Commit 8cb080c

Browse files
committed
fs: move type checking for fs.sync 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 956f97b commit 8cb080c

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

lib/fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ fs.fdatasyncSync = function(fd) {
925925
};
926926

927927
fs.fsync = function(fd, callback) {
928-
if (typeof fd !== 'number')
928+
if (!Number.isInteger(fd))
929929
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
930930
if (fd < 0 || fd > 0xFFFFFFFF)
931931
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
@@ -935,7 +935,7 @@ fs.fsync = function(fd, callback) {
935935
};
936936

937937
fs.fsyncSync = function(fd) {
938-
if (typeof fd !== 'number')
938+
if (!Number.isInteger(fd))
939939
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
940940
if (fd < 0 || fd > 0xFFFFFFFF)
941941
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
@@ -806,10 +806,7 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
806806
static void Fsync(const FunctionCallbackInfo<Value>& args) {
807807
Environment* env = Environment::GetCurrent(args);
808808

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

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

test/parallel/test-fs-fsync.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,35 @@ fs.open(fileFixture, 'a', 0o777, common.mustCall(function(err, fd) {
6666
message: 'The "fd" argument must be of type number'
6767
}
6868
);
69+
common.expectsError(
70+
() => fs.fsync(i),
71+
{
72+
code: 'ERR_INVALID_ARG_TYPE',
73+
type: TypeError,
74+
message: 'The "fd" argument must be of type number'
75+
}
76+
);
77+
common.expectsError(
78+
() => fs.fsyncSync(i),
79+
{
80+
code: 'ERR_INVALID_ARG_TYPE',
81+
type: TypeError,
82+
message: 'The "fd" argument must be of type number'
83+
}
84+
);
6985
});
7086

7187
[-1, 0xFFFFFFFF + 1].forEach((i) => {
7288
common.expectsError(
73-
() => fs.fdatasync(i),
89+
() => fs.fsync(i),
7490
{
7591
code: 'ERR_OUT_OF_RANGE',
7692
type: RangeError,
7793
message: 'The "fd" argument is out of range'
7894
}
7995
);
8096
common.expectsError(
81-
() => fs.fdatasyncSync(i),
97+
() => fs.fsyncSync(i),
8298
{
8399
code: 'ERR_OUT_OF_RANGE',
84100
type: RangeError,

0 commit comments

Comments
 (0)