Skip to content
Prev Previous commit
Next Next commit
fs: move type checking for fs.fdatasync to js
  • Loading branch information
jasnell committed Nov 26, 2017
commit 19de31525a9a5a65c2f75261666eef403d317c02
4 changes: 2 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ fs.rmdirSync = function(path) {
};

fs.fdatasync = 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 All @@ -917,7 +917,7 @@ fs.fdatasync = function(fd, callback) {
};

fs.fdatasyncSync = 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 @@ -792,10 +792,7 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) {
static void Fdatasync(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-fsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,41 @@ fs.open(fileFixture, 'a', 0o777, common.mustCall(function(err, fd) {
}));
}));
}));

['', false, null, undefined, {}, []].forEach((i) => {
common.expectsError(
() => fs.fdatasync(i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type number'
}
);
common.expectsError(
() => fs.fdatasyncSync(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.fdatasync(i),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The "fd" argument is out of range'
}
);
common.expectsError(
() => fs.fdatasyncSync(i),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The "fd" argument is out of range'
}
);
});