Skip to content

Commit 1ae184a

Browse files
committed
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: nodejs#20588 Fixes: nodejs#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]>
1 parent 1c1b8ae commit 1ae184a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lib/fs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ fs.unlinkSync = function(path) {
10571057
};
10581058

10591059
fs.fchmod = function(fd, mode, callback) {
1060-
validateUint32(fd, 'fd');
1060+
validateInt32(fd, 'fd', 0);
10611061
mode = validateAndMaskMode(mode, 'mode');
10621062
callback = makeCallback(callback);
10631063

@@ -1067,7 +1067,7 @@ fs.fchmod = function(fd, mode, callback) {
10671067
};
10681068

10691069
fs.fchmodSync = function(fd, mode) {
1070-
validateUint32(fd, 'fd');
1070+
validateInt32(fd, 'fd', 0);
10711071
mode = validateAndMaskMode(mode, 'mode');
10721072
const ctx = {};
10731073
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)