diff options
author | James M Snell <jasnell@gmail.com> | 2017-11-26 13:18:54 -0800 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2017-12-13 13:26:28 -0800 |
commit | 639096855ed52958a5bf7ccc0a83cee6a4a24166 (patch) | |
tree | a23172610276817b721eeff2e303a70bfabe15d4 | |
parent | 8974df15a973e97a74cf9fb0ccb45c11baa7b54a (diff) | |
download | android-node-v8-639096855ed52958a5bf7ccc0a83cee6a4a24166.tar.gz android-node-v8-639096855ed52958a5bf7ccc0a83cee6a4a24166.tar.bz2 android-node-v8-639096855ed52958a5bf7ccc0a83cee6a4a24166.zip |
fs: move type checking on fs.fstat to js
PR-URL: https://github.com/nodejs/node/pull/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>
-rw-r--r-- | lib/fs.js | 4 | ||||
-rw-r--r-- | src/node_file.cc | 5 | ||||
-rw-r--r-- | test/parallel/test-fs-stat.js | 38 |
3 files changed, 41 insertions, 6 deletions
@@ -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<Value>& args) { static void FStat(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(); 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' + } + ); +}); |