diff --git a/lib/fs.js b/lib/fs.js index ac0d42c76a2..70e0d3d98ca 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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'); @@ -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'); diff --git a/src/node_file.cc b/src/node_file.cc index 72995ca1be7..7acf6296fc9 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -792,10 +792,7 @@ static void FTruncate(const FunctionCallbackInfo& args) { static void Fdatasync(const FunctionCallbackInfo& 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(); diff --git a/test/parallel/test-fs-fsync.js b/test/parallel/test-fs-fsync.js index 3f43dd75450..1a9d1d97f8f 100644 --- a/test/parallel/test-fs-fsync.js +++ b/test/parallel/test-fs-fsync.js @@ -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' + } + ); +});