From 8cb080c4867bb7b8eefbb2abf34bd97c31f647a1 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 26 Nov 2017 13:38:58 -0800 Subject: [PATCH] fs: move type checking for fs.sync to js PR-URL: https://github.com/nodejs/node/pull/17334 Reviewed-By: Refael Ackermann Reviewed-By: Timothy Gu Reviewed-By: Jon Moss Reviewed-By: Anna Henningsen Reviewed-By: Joyee Cheung Reviewed-By: Matteo Collina --- lib/fs.js | 4 ++-- src/node_file.cc | 5 +---- test/parallel/test-fs-fsync.js | 20 ++++++++++++++++++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 70e0d3d98ca2a7..9d9ba70612790c 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -925,7 +925,7 @@ fs.fdatasyncSync = function(fd) { }; fs.fsync = 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'); @@ -935,7 +935,7 @@ fs.fsync = function(fd, callback) { }; fs.fsyncSync = 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 7acf6296fc9164..13155bedb101e6 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -806,10 +806,7 @@ static void Fdatasync(const FunctionCallbackInfo& args) { static void Fsync(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 1a9d1d97f8fee1..e1ec8ddcc0fe14 100644 --- a/test/parallel/test-fs-fsync.js +++ b/test/parallel/test-fs-fsync.js @@ -66,11 +66,27 @@ fs.open(fileFixture, 'a', 0o777, common.mustCall(function(err, fd) { message: 'The "fd" argument must be of type number' } ); + common.expectsError( + () => fs.fsync(i), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "fd" argument must be of type number' + } + ); + common.expectsError( + () => fs.fsyncSync(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), + () => fs.fsync(i), { code: 'ERR_OUT_OF_RANGE', type: RangeError, @@ -78,7 +94,7 @@ fs.open(fileFixture, 'a', 0o777, common.mustCall(function(err, fd) { } ); common.expectsError( - () => fs.fdatasyncSync(i), + () => fs.fsyncSync(i), { code: 'ERR_OUT_OF_RANGE', type: RangeError,