Skip to content

Commit 0a01aa8

Browse files
committed
fs: move type checking for fs.fchmod to js
PR-URL: #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>
1 parent d453fac commit 0a01aa8

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

lib/fs.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,11 +1147,11 @@ fs.unlinkSync = function(path) {
11471147

11481148
fs.fchmod = function(fd, mode, callback) {
11491149
mode = modeNum(mode);
1150-
if (typeof fd !== 'number')
1150+
if (!Number.isInteger(fd))
11511151
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
11521152
if (fd < 0 || fd > 0xFFFFFFFF)
11531153
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
1154-
if (typeof mode !== 'number')
1154+
if (!Number.isInteger(mode))
11551155
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'number');
11561156
if (mode < 0 || mode > 0o777)
11571157
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
@@ -1163,11 +1163,11 @@ fs.fchmod = function(fd, mode, callback) {
11631163

11641164
fs.fchmodSync = function(fd, mode) {
11651165
mode = modeNum(mode);
1166-
if (typeof fd !== 'number')
1166+
if (!Number.isInteger(fd))
11671167
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
11681168
if (fd < 0 || fd > 0xFFFFFFFF)
11691169
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
1170-
if (typeof mode !== 'number')
1170+
if (!Number.isInteger(mode))
11711171
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'number');
11721172
if (mode < 0 || mode > 0o777)
11731173
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');

src/node_file.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,12 +1254,8 @@ static void Chmod(const FunctionCallbackInfo<Value>& args) {
12541254
static void FChmod(const FunctionCallbackInfo<Value>& args) {
12551255
Environment* env = Environment::GetCurrent(args);
12561256

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

12641260
int fd = args[0]->Int32Value();
12651261
int mode = static_cast<int>(args[1]->Int32Value());

test/parallel/test-fs-chmod.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ fs.open(file2, 'w', common.mustCall((err, fd) => {
108108
assert.strictEqual(mode_async, fs.fstatSync(fd).mode & 0o777);
109109
}
110110

111+
common.expectsError(
112+
() => fs.fchmod(fd, {}),
113+
{
114+
code: 'ERR_INVALID_ARG_TYPE',
115+
type: TypeError,
116+
message: 'The "mode" argument must be of type number'
117+
}
118+
);
119+
111120
fs.fchmodSync(fd, mode_sync);
112121
if (common.isWindows) {
113122
assert.ok((fs.fstatSync(fd).mode & 0o777) & mode_sync);
@@ -136,6 +145,43 @@ if (fs.lchmod) {
136145
}));
137146
}
138147

148+
['', false, null, undefined, {}, []].forEach((i) => {
149+
common.expectsError(
150+
() => fs.fchmod(i, 0o000),
151+
{
152+
code: 'ERR_INVALID_ARG_TYPE',
153+
type: TypeError,
154+
message: 'The "fd" argument must be of type number'
155+
}
156+
);
157+
common.expectsError(
158+
() => fs.fchmodSync(i, 0o000),
159+
{
160+
code: 'ERR_INVALID_ARG_TYPE',
161+
type: TypeError,
162+
message: 'The "fd" argument must be of type number'
163+
}
164+
);
165+
});
166+
167+
[-1, 0xFFFFFFFF + 1].forEach((i) => {
168+
common.expectsError(
169+
() => fs.fchmod(i, 0o000),
170+
{
171+
code: 'ERR_OUT_OF_RANGE',
172+
type: RangeError,
173+
message: 'The "fd" argument is out of range'
174+
}
175+
);
176+
common.expectsError(
177+
() => fs.fchmodSync(i, 0o000),
178+
{
179+
code: 'ERR_OUT_OF_RANGE',
180+
type: RangeError,
181+
message: 'The "fd" argument is out of range'
182+
}
183+
);
184+
});
139185

140186
process.on('exit', function() {
141187
assert.strictEqual(0, openCount);

0 commit comments

Comments
 (0)