From 639096855ed52958a5bf7ccc0a83cee6a4a24166 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 26 Nov 2017 13:18:54 -0800 Subject: fs: move type checking on fs.fstat 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-stat.js | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 67c96a7524..ac0d42c76a 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -981,7 +981,7 @@ fs.readdirSync = function(path, options) { }; fs.fstat = 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'); @@ -1011,7 +1011,7 @@ fs.stat = function(path, callback) { }; fs.fstatSync = 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 1eda2104b7..72995ca1be 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -634,10 +634,7 @@ static void LStat(const FunctionCallbackInfo& args) { static void FStat(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-stat.js b/test/parallel/test-fs-stat.js index 332a26e9bf..57fccc1586 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -130,3 +130,41 @@ fs.stat(__filename, common.mustCall(function(err, s) { assert.strictEqual(typeof parsed[k], 'string', `${k} should be a string`); }); })); + +['', false, null, undefined, {}, []].forEach((i) => { + common.expectsError( + () => fs.fstat(i), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "fd" argument must be of type number' + } + ); + common.expectsError( + () => fs.fstatSync(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.fstat(i), + { + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The "fd" argument is out of range' + } + ); + common.expectsError( + () => fs.fstatSync(i), + { + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The "fd" argument is out of range' + } + ); +}); -- cgit v1.2.3