Skip to content

Commit 956f97b

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

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
@@ -907,7 +907,7 @@ fs.rmdirSync = function(path) {
907907
};
908908

909909
fs.fdatasync = function(fd, callback) {
910-
if (typeof fd !== 'number')
910+
if (!Number.isInteger(fd))
911911
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
912912
if (fd < 0 || fd > 0xFFFFFFFF)
913913
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
@@ -917,7 +917,7 @@ fs.fdatasync = function(fd, callback) {
917917
};
918918

919919
fs.fdatasyncSync = function(fd) {
920-
if (typeof fd !== 'number')
920+
if (!Number.isInteger(fd))
921921
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
922922
if (fd < 0 || fd > 0xFFFFFFFF)
923923
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
@@ -792,10 +792,7 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) {
792792
static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
793793
Environment* env = Environment::GetCurrent(args);
794794

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

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

test/parallel/test-fs-fsync.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,41 @@ fs.open(fileFixture, 'a', 0o777, common.mustCall(function(err, fd) {
4848
}));
4949
}));
5050
}));
51+
52+
['', false, null, undefined, {}, []].forEach((i) => {
53+
common.expectsError(
54+
() => fs.fdatasync(i),
55+
{
56+
code: 'ERR_INVALID_ARG_TYPE',
57+
type: TypeError,
58+
message: 'The "fd" argument must be of type number'
59+
}
60+
);
61+
common.expectsError(
62+
() => fs.fdatasyncSync(i),
63+
{
64+
code: 'ERR_INVALID_ARG_TYPE',
65+
type: TypeError,
66+
message: 'The "fd" argument must be of type number'
67+
}
68+
);
69+
});
70+
71+
[-1, 0xFFFFFFFF + 1].forEach((i) => {
72+
common.expectsError(
73+
() => fs.fdatasync(i),
74+
{
75+
code: 'ERR_OUT_OF_RANGE',
76+
type: RangeError,
77+
message: 'The "fd" argument is out of range'
78+
}
79+
);
80+
common.expectsError(
81+
() => fs.fdatasyncSync(i),
82+
{
83+
code: 'ERR_OUT_OF_RANGE',
84+
type: RangeError,
85+
message: 'The "fd" argument is out of range'
86+
}
87+
);
88+
});

0 commit comments

Comments
 (0)