Skip to content

Commit db0bb52

Browse files
cjihrigtargos
authored andcommitted
fs: improve fchmod{Sync} validation
This commit validates the fd parameters to fs.fchmod{Sync} as int32s instead of uint32s because they are ints in the binding layer. PR-URL: #20588 Fixes: #20498 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Weijia Wang <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Backport-PR-URL: #21172
1 parent 36e5100 commit db0bb52

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/fs.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const {
8686
isUint32,
8787
validateAndMaskMode,
8888
validateInteger,
89+
validateInt32,
8990
validateUint32
9091
} = require('internal/validators');
9192

@@ -1054,7 +1055,7 @@ fs.unlinkSync = function(path) {
10541055
};
10551056

10561057
fs.fchmod = function(fd, mode, callback) {
1057-
validateUint32(fd, 'fd');
1058+
validateInt32(fd, 'fd', 0);
10581059
mode = validateAndMaskMode(mode, 'mode');
10591060
callback = makeCallback(callback);
10601061

@@ -1064,7 +1065,7 @@ fs.fchmod = function(fd, mode, callback) {
10641065
};
10651066

10661067
fs.fchmodSync = function(fd, mode) {
1067-
validateUint32(fd, 'fd');
1068+
validateInt32(fd, 'fd', 0);
10681069
mode = validateAndMaskMode(mode, 'mode');
10691070
const ctx = {};
10701071
binding.fchmod(fd, mode, undefined, ctx);

test/parallel/test-fs-fchmod.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,21 @@ const fs = require('fs');
3535
const errObj = {
3636
code: 'ERR_OUT_OF_RANGE',
3737
name: 'RangeError [ERR_OUT_OF_RANGE]',
38-
message: 'The value of "fd" is out of range. It must be >= 0 && < ' +
39-
`${2 ** 32}. Received ${input}`
38+
message: 'The value of "fd" is out of range. It must be >= 0 && <= ' +
39+
`2147483647. Received ${input}`
4040
};
4141
assert.throws(() => fs.fchmod(input), errObj);
4242
assert.throws(() => fs.fchmodSync(input), errObj);
43-
errObj.message = errObj.message.replace('fd', 'mode');
43+
});
44+
45+
[-1, 2 ** 32].forEach((input) => {
46+
const errObj = {
47+
code: 'ERR_OUT_OF_RANGE',
48+
name: 'RangeError [ERR_OUT_OF_RANGE]',
49+
message: 'The value of "mode" is out of range. It must be >= 0 && < ' +
50+
`4294967296. Received ${input}`
51+
};
52+
4453
assert.throws(() => fs.fchmod(1, input), errObj);
4554
assert.throws(() => fs.fchmodSync(1, input), errObj);
4655
});

0 commit comments

Comments
 (0)