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>
This commit is contained in:
parent
8974df15a9
commit
639096855e
@ -981,7 +981,7 @@ fs.readdirSync = function(path, options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs.fstat = function(fd, callback) {
|
fs.fstat = function(fd, callback) {
|
||||||
if (typeof fd !== 'number')
|
if (!Number.isInteger(fd))
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
|
||||||
if (fd < 0 || fd > 0xFFFFFFFF)
|
if (fd < 0 || fd > 0xFFFFFFFF)
|
||||||
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
|
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
|
||||||
@ -1011,7 +1011,7 @@ fs.stat = function(path, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs.fstatSync = function(fd) {
|
fs.fstatSync = function(fd) {
|
||||||
if (typeof fd !== 'number')
|
if (!Number.isInteger(fd))
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
|
||||||
if (fd < 0 || fd > 0xFFFFFFFF)
|
if (fd < 0 || fd > 0xFFFFFFFF)
|
||||||
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
|
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
|
||||||
|
@ -634,10 +634,7 @@ static void LStat(const FunctionCallbackInfo<Value>& args) {
|
|||||||
static void FStat(const FunctionCallbackInfo<Value>& args) {
|
static void FStat(const FunctionCallbackInfo<Value>& args) {
|
||||||
Environment* env = Environment::GetCurrent(args);
|
Environment* env = Environment::GetCurrent(args);
|
||||||
|
|
||||||
if (args.Length() < 1)
|
CHECK(args[0]->IsInt32());
|
||||||
return TYPE_ERROR("fd is required");
|
|
||||||
if (!args[0]->IsInt32())
|
|
||||||
return TYPE_ERROR("fd must be a file descriptor");
|
|
||||||
|
|
||||||
int fd = args[0]->Int32Value();
|
int fd = args[0]->Int32Value();
|
||||||
|
|
||||||
|
@ -130,3 +130,41 @@ fs.stat(__filename, common.mustCall(function(err, s) {
|
|||||||
assert.strictEqual(typeof parsed[k], 'string', `${k} should be a string`);
|
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'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user