Skip to content

Commit 1cdeb9f

Browse files
committedMar 15, 2019
fs: improve mode validation
Do not return a default mode in case invalid mode values are provided. This strictens and simplifies the function by reusing existing functionality. PR-URL: #26575 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Masashi Hirano <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
1 parent 6f77af5 commit 1cdeb9f

File tree

4 files changed

+41
-31
lines changed

4 files changed

+41
-31
lines changed
 

‎lib/internal/validators.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,17 @@ function validateMode(value, name, def) {
3535
}
3636

3737
if (typeof value === 'number') {
38-
if (!Number.isInteger(value)) {
39-
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
40-
} else {
41-
// 2 ** 32 === 4294967296
42-
throw new ERR_OUT_OF_RANGE(name, '>= 0 && < 4294967296', value);
43-
}
38+
validateInt32(value, name, 0, 2 ** 32 - 1);
4439
}
4540

4641
if (typeof value === 'string') {
4742
if (!octalReg.test(value)) {
4843
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
4944
}
50-
const parsed = parseInt(value, 8);
51-
return parsed;
45+
return parseInt(value, 8);
5246
}
5347

54-
// TODO(BridgeAR): Only return `def` in case `value == null`
55-
if (def !== undefined) {
48+
if (def !== undefined && value == null) {
5649
return def;
5750
}
5851

‎test/parallel/test-fs-fchmod.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ const fs = require('fs');
4646
const errObj = {
4747
code: 'ERR_OUT_OF_RANGE',
4848
name: 'RangeError [ERR_OUT_OF_RANGE]',
49-
message: 'The value of "mode" is out of range. It must be >= 0 && < ' +
50-
`4294967296. Received ${input}`
49+
message: 'The value of "mode" is out of range. It must be >= 0 && <= ' +
50+
`4294967295. Received ${input}`
5151
};
5252

5353
assert.throws(() => fs.fchmod(1, input), errObj);

‎test/parallel/test-fs-lchmod.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,18 @@ assert.throws(() => fs.lchmod(f, {}), { code: 'ERR_INVALID_CALLBACK' });
4646
`octal string. Received ${util.inspect(input)}`
4747
};
4848

49-
promises.lchmod(f, input, () => {})
50-
.then(common.mustNotCall())
51-
.catch(common.expectsError(errObj));
49+
assert.rejects(promises.lchmod(f, input, () => {}), errObj);
5250
assert.throws(() => fs.lchmodSync(f, input), errObj);
5351
});
5452

5553
[-1, 2 ** 32].forEach((input) => {
5654
const errObj = {
5755
code: 'ERR_OUT_OF_RANGE',
5856
name: 'RangeError [ERR_OUT_OF_RANGE]',
59-
message: 'The value of "mode" is out of range. It must be >= 0 && < ' +
60-
`4294967296. Received ${input}`
57+
message: 'The value of "mode" is out of range. It must be >= 0 && <= ' +
58+
`4294967295. Received ${input}`
6159
};
6260

63-
promises.lchmod(f, input, () => {})
64-
.then(common.mustNotCall())
65-
.catch(common.expectsError(errObj));
61+
assert.rejects(promises.lchmod(f, input, () => {}), errObj);
6662
assert.throws(() => fs.lchmodSync(f, input), errObj);
6763
});

‎test/parallel/test-fs-open.js

+32-11
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,36 @@ for (const extra of [[], ['r'], ['r', 0], ['r', 0, 'bad callback']]) {
9898
type: TypeError
9999
}
100100
);
101-
fs.promises.open(i, 'r')
102-
.then(common.mustNotCall())
103-
.catch(common.mustCall((err) => {
104-
common.expectsError(
105-
() => { throw err; },
106-
{
107-
code: 'ERR_INVALID_ARG_TYPE',
108-
type: TypeError
109-
}
110-
);
111-
}));
101+
assert.rejects(
102+
fs.promises.open(i, 'r'),
103+
{
104+
code: 'ERR_INVALID_ARG_TYPE',
105+
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
106+
}
107+
);
108+
});
109+
110+
// Check invalid modes.
111+
[false, [], {}].forEach((mode) => {
112+
assert.throws(
113+
() => fs.open(__filename, 'r', mode, common.mustNotCall()),
114+
{
115+
message: /'mode' must be a 32-bit/,
116+
code: 'ERR_INVALID_ARG_VALUE'
117+
}
118+
);
119+
assert.throws(
120+
() => fs.openSync(__filename, 'r', mode, common.mustNotCall()),
121+
{
122+
message: /'mode' must be a 32-bit/,
123+
code: 'ERR_INVALID_ARG_VALUE'
124+
}
125+
);
126+
assert.rejects(
127+
fs.promises.open(__filename, 'r', mode),
128+
{
129+
message: /'mode' must be a 32-bit/,
130+
code: 'ERR_INVALID_ARG_VALUE'
131+
}
132+
);
112133
});

0 commit comments

Comments
 (0)