Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: move type checking for multiple fs methods to js #17334

Closed
wants to merge 10 commits into from
Prev Previous commit
Next Next commit
fs: move type checking for fs.fchmod to js
  • Loading branch information
jasnell committed Nov 26, 2017
commit 8b03b46977018d75e713e449997df3adb955c0f9
8 changes: 4 additions & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1147,11 +1147,11 @@ fs.unlinkSync = function(path) {

fs.fchmod = function(fd, mode, callback) {
mode = modeNum(mode);
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');
if (typeof mode !== 'number')
if (!Number.isInteger(mode))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'number');
if (mode < 0 || mode > 0o777)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
Expand All @@ -1163,11 +1163,11 @@ fs.fchmod = function(fd, mode, callback) {

fs.fchmodSync = function(fd, mode) {
mode = modeNum(mode);
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');
if (typeof mode !== 'number')
if (!Number.isInteger(mode))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'number');
if (mode < 0 || mode > 0o777)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
Expand Down
8 changes: 2 additions & 6 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1254,12 +1254,8 @@ static void Chmod(const FunctionCallbackInfo<Value>& args) {
static void FChmod(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 2)
return TYPE_ERROR("fd and mode are required");
if (!args[0]->IsInt32())
return TYPE_ERROR("fd must be a file descriptor");
if (!args[1]->IsInt32())
return TYPE_ERROR("mode must be an integer");
CHECK(args[0]->IsInt32());
CHECK(args[1]->IsInt32());

int fd = args[0]->Int32Value();
int mode = static_cast<int>(args[1]->Int32Value());
Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-fs-chmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ fs.open(file2, 'w', common.mustCall((err, fd) => {
assert.strictEqual(mode_async, fs.fstatSync(fd).mode & 0o777);
}

common.expectsError(
() => fs.fchmod(fd, {}),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "mode" argument must be of type number'
}
);

fs.fchmodSync(fd, mode_sync);
if (common.isWindows) {
assert.ok((fs.fstatSync(fd).mode & 0o777) & mode_sync);
Expand Down Expand Up @@ -136,6 +145,43 @@ if (fs.lchmod) {
}));
}

['', false, null, undefined, {}, []].forEach((i) => {
common.expectsError(
() => fs.fchmod(i, 0o000),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type number'
}
);
common.expectsError(
() => fs.fchmodSync(i, 0o000),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type number'
}
);
});

[-1, 0xFFFFFFFF + 1].forEach((i) => {
common.expectsError(
() => fs.fchmod(i, 0o000),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The "fd" argument is out of range'
}
);
common.expectsError(
() => fs.fchmodSync(i, 0o000),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The "fd" argument is out of range'
}
);
});

process.on('exit', function() {
assert.strictEqual(0, openCount);
Expand Down