fs: validate fds as int32s · nodejs/node@f194626 · GitHub
Skip to content

Commit f194626

Browse files
cjihrigtargos
authored andcommitted
fs: validate fds as int32s
This commit updates the JS layer's validation of file descriptors to check for int32s >= 0 instead of uint32s. PR-URL: #28984 Fixes: #28980 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 17d9495 commit f194626

3 files changed

Lines changed: 43 additions & 26 deletions

File tree

lib/fs.js

Lines changed: 18 additions & 18 deletions

test/parallel/test-fs-fchown.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ require('../common');
44
const assert = require('assert');
55
const fs = require('fs');
66

7-
function test(input, errObj) {
7+
function testFd(input, errObj) {
88
assert.throws(() => fs.fchown(input), errObj);
99
assert.throws(() => fs.fchownSync(input), errObj);
10-
errObj.message = errObj.message.replace('fd', 'uid');
10+
}
11+
12+
function testUid(input, errObj) {
1113
assert.throws(() => fs.fchown(1, input), errObj);
1214
assert.throws(() => fs.fchownSync(1, input), errObj);
13-
errObj.message = errObj.message.replace('uid', 'gid');
15+
}
16+
17+
function testGid(input, errObj) {
1418
assert.throws(() => fs.fchown(1, 1, input), errObj);
1519
assert.throws(() => fs.fchownSync(1, 1, input), errObj);
1620
}
@@ -22,7 +26,11 @@ function test(input, errObj) {
2226
message: 'The "fd" argument must be of type number. Received type ' +
2327
typeof input
2428
};
25-
test(input, errObj);
29+
testFd(input, errObj);
30+
errObj.message = errObj.message.replace('fd', 'uid');
31+
testUid(input, errObj);
32+
errObj.message = errObj.message.replace('uid', 'gid');
33+
testGid(input, errObj);
2634
});
2735

2836
[Infinity, NaN].forEach((input) => {
@@ -32,15 +40,24 @@ function test(input, errObj) {
3240
message: 'The value of "fd" is out of range. It must be an integer. ' +
3341
`Received ${input}`
3442
};
35-
test(input, errObj);
43+
testFd(input, errObj);
44+
errObj.message = errObj.message.replace('fd', 'uid');
45+
testUid(input, errObj);
46+
errObj.message = errObj.message.replace('uid', 'gid');
47+
testGid(input, errObj);
3648
});
3749

3850
[-1, 2 ** 32].forEach((input) => {
3951
const errObj = {
4052
code: 'ERR_OUT_OF_RANGE',
4153
name: 'RangeError',
4254
message: 'The value of "fd" is out of range. It must be ' +
43-
`>= 0 && < 4294967296. Received ${input}`
55+
`>= 0 && <= 2147483647. Received ${input}`
4456
};
45-
test(input, errObj);
57+
testFd(input, errObj);
58+
errObj.message = 'The value of "uid" is out of range. It must be >= 0 && ' +
59+
`< 4294967296. Received ${input}`;
60+
testUid(input, errObj);
61+
errObj.message = errObj.message.replace('uid', 'gid');
62+
testGid(input, errObj);
4663
});

test/parallel/test-fs-utimes.js

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)