Skip to content

Commit 82eb459

Browse files
committed
fs: move type checking for fs.fchown 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 0a01aa8 commit 82eb459

File tree

3 files changed

+140
-14
lines changed

3 files changed

+140
-14
lines changed

lib/fs.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,12 +1249,38 @@ if (constants.O_SYMLINK !== undefined) {
12491249
}
12501250

12511251
fs.fchown = function(fd, uid, gid, callback) {
1252-
var req = new FSReqWrap();
1252+
if (!Number.isInteger(fd))
1253+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
1254+
if (fd < 0 || fd > 0xFFFFFFFF)
1255+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
1256+
if (!Number.isInteger(uid))
1257+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'number');
1258+
if (uid < 0)
1259+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'uid');
1260+
if (!Number.isInteger(gid))
1261+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'number');
1262+
if (gid < 0)
1263+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'gid');
1264+
1265+
const req = new FSReqWrap();
12531266
req.oncomplete = makeCallback(callback);
12541267
binding.fchown(fd, uid, gid, req);
12551268
};
12561269

12571270
fs.fchownSync = function(fd, uid, gid) {
1271+
if (!Number.isInteger(fd))
1272+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
1273+
if (fd < 0 || fd > 0xFFFFFFFF)
1274+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
1275+
if (!Number.isInteger(uid))
1276+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'number');
1277+
if (uid < 0)
1278+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'uid');
1279+
if (!Number.isInteger(gid))
1280+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'number');
1281+
if (gid < 0)
1282+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'gid');
1283+
12581284
return binding.fchown(fd, uid, gid);
12591285
};
12601286

src/node_file.cc

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,19 +1306,9 @@ static void Chown(const FunctionCallbackInfo<Value>& args) {
13061306
static void FChown(const FunctionCallbackInfo<Value>& args) {
13071307
Environment* env = Environment::GetCurrent(args);
13081308

1309-
int len = args.Length();
1310-
if (len < 1)
1311-
return TYPE_ERROR("fd required");
1312-
if (len < 2)
1313-
return TYPE_ERROR("uid required");
1314-
if (len < 3)
1315-
return TYPE_ERROR("gid required");
1316-
if (!args[0]->IsInt32())
1317-
return TYPE_ERROR("fd must be an int");
1318-
if (!args[1]->IsUint32())
1319-
return TYPE_ERROR("uid must be an unsigned int");
1320-
if (!args[2]->IsUint32())
1321-
return TYPE_ERROR("gid must be an unsigned int");
1309+
CHECK(args[0]->IsInt32());
1310+
CHECK(args[1]->IsUint32());
1311+
CHECK(args[2]->IsUint32());
13221312

13231313
int fd = args[0]->Int32Value();
13241314
uv_uid_t uid = static_cast<uv_uid_t>(args[1]->Uint32Value());

test/parallel/test-fs-fchown.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
6+
['', false, null, undefined, {}, []].forEach((i) => {
7+
common.expectsError(
8+
() => fs.fchown(i),
9+
{
10+
code: 'ERR_INVALID_ARG_TYPE',
11+
type: TypeError,
12+
message: 'The "fd" argument must be of type number'
13+
}
14+
);
15+
common.expectsError(
16+
() => fs.fchownSync(i),
17+
{
18+
code: 'ERR_INVALID_ARG_TYPE',
19+
type: TypeError,
20+
message: 'The "fd" argument must be of type number'
21+
}
22+
);
23+
24+
common.expectsError(
25+
() => fs.fchown(1, i),
26+
{
27+
code: 'ERR_INVALID_ARG_TYPE',
28+
type: TypeError,
29+
message: 'The "uid" argument must be of type number'
30+
}
31+
);
32+
common.expectsError(
33+
() => fs.fchownSync(1, i),
34+
{
35+
code: 'ERR_INVALID_ARG_TYPE',
36+
type: TypeError,
37+
message: 'The "uid" argument must be of type number'
38+
}
39+
);
40+
41+
common.expectsError(
42+
() => fs.fchown(1, 1, i),
43+
{
44+
code: 'ERR_INVALID_ARG_TYPE',
45+
type: TypeError,
46+
message: 'The "gid" argument must be of type number'
47+
}
48+
);
49+
common.expectsError(
50+
() => fs.fchownSync(1, 1, i),
51+
{
52+
code: 'ERR_INVALID_ARG_TYPE',
53+
type: TypeError,
54+
message: 'The "gid" argument must be of type number'
55+
}
56+
);
57+
});
58+
59+
[-1, 0xFFFFFFFF + 1].forEach((i) => {
60+
common.expectsError(
61+
() => fs.fchown(i),
62+
{
63+
code: 'ERR_OUT_OF_RANGE',
64+
type: RangeError,
65+
message: 'The "fd" argument is out of range'
66+
}
67+
);
68+
common.expectsError(
69+
() => fs.fchownSync(i),
70+
{
71+
code: 'ERR_OUT_OF_RANGE',
72+
type: RangeError,
73+
message: 'The "fd" argument is out of range'
74+
}
75+
);
76+
});
77+
78+
common.expectsError(
79+
() => fs.fchown(1, -1),
80+
{
81+
code: 'ERR_OUT_OF_RANGE',
82+
type: RangeError,
83+
message: 'The "uid" argument is out of range'
84+
}
85+
);
86+
common.expectsError(
87+
() => fs.fchownSync(1, -1),
88+
{
89+
code: 'ERR_OUT_OF_RANGE',
90+
type: RangeError,
91+
message: 'The "uid" argument is out of range'
92+
}
93+
);
94+
95+
common.expectsError(
96+
() => fs.fchown(1, 1, -1),
97+
{
98+
code: 'ERR_OUT_OF_RANGE',
99+
type: RangeError,
100+
message: 'The "gid" argument is out of range'
101+
}
102+
);
103+
common.expectsError(
104+
() => fs.fchownSync(1, 1, -1),
105+
{
106+
code: 'ERR_OUT_OF_RANGE',
107+
type: RangeError,
108+
message: 'The "gid" argument is out of range'
109+
}
110+
);

0 commit comments

Comments
 (0)