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: https://github.com/nodejs/node/pull/26575 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Masashi Hirano <shisama07@gmail.com> Reviewed-By: Rod Vagg <rod@vagg.org>
This commit is contained in:
parent
6f77af541e
commit
1cdeb9f956
@ -35,24 +35,17 @@ function validateMode(value, name, def) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof value === 'number') {
|
if (typeof value === 'number') {
|
||||||
if (!Number.isInteger(value)) {
|
validateInt32(value, name, 0, 2 ** 32 - 1);
|
||||||
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
|
|
||||||
} else {
|
|
||||||
// 2 ** 32 === 4294967296
|
|
||||||
throw new ERR_OUT_OF_RANGE(name, '>= 0 && < 4294967296', value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
if (!octalReg.test(value)) {
|
if (!octalReg.test(value)) {
|
||||||
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
|
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
|
||||||
}
|
}
|
||||||
const parsed = parseInt(value, 8);
|
return parseInt(value, 8);
|
||||||
return parsed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(BridgeAR): Only return `def` in case `value == null`
|
if (def !== undefined && value == null) {
|
||||||
if (def !== undefined) {
|
|
||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,8 +46,8 @@ const fs = require('fs');
|
|||||||
const errObj = {
|
const errObj = {
|
||||||
code: 'ERR_OUT_OF_RANGE',
|
code: 'ERR_OUT_OF_RANGE',
|
||||||
name: 'RangeError [ERR_OUT_OF_RANGE]',
|
name: 'RangeError [ERR_OUT_OF_RANGE]',
|
||||||
message: 'The value of "mode" is out of range. It must be >= 0 && < ' +
|
message: 'The value of "mode" is out of range. It must be >= 0 && <= ' +
|
||||||
`4294967296. Received ${input}`
|
`4294967295. Received ${input}`
|
||||||
};
|
};
|
||||||
|
|
||||||
assert.throws(() => fs.fchmod(1, input), errObj);
|
assert.throws(() => fs.fchmod(1, input), errObj);
|
||||||
|
@ -46,9 +46,7 @@ assert.throws(() => fs.lchmod(f, {}), { code: 'ERR_INVALID_CALLBACK' });
|
|||||||
`octal string. Received ${util.inspect(input)}`
|
`octal string. Received ${util.inspect(input)}`
|
||||||
};
|
};
|
||||||
|
|
||||||
promises.lchmod(f, input, () => {})
|
assert.rejects(promises.lchmod(f, input, () => {}), errObj);
|
||||||
.then(common.mustNotCall())
|
|
||||||
.catch(common.expectsError(errObj));
|
|
||||||
assert.throws(() => fs.lchmodSync(f, input), errObj);
|
assert.throws(() => fs.lchmodSync(f, input), errObj);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -56,12 +54,10 @@ assert.throws(() => fs.lchmod(f, {}), { code: 'ERR_INVALID_CALLBACK' });
|
|||||||
const errObj = {
|
const errObj = {
|
||||||
code: 'ERR_OUT_OF_RANGE',
|
code: 'ERR_OUT_OF_RANGE',
|
||||||
name: 'RangeError [ERR_OUT_OF_RANGE]',
|
name: 'RangeError [ERR_OUT_OF_RANGE]',
|
||||||
message: 'The value of "mode" is out of range. It must be >= 0 && < ' +
|
message: 'The value of "mode" is out of range. It must be >= 0 && <= ' +
|
||||||
`4294967296. Received ${input}`
|
`4294967295. Received ${input}`
|
||||||
};
|
};
|
||||||
|
|
||||||
promises.lchmod(f, input, () => {})
|
assert.rejects(promises.lchmod(f, input, () => {}), errObj);
|
||||||
.then(common.mustNotCall())
|
|
||||||
.catch(common.expectsError(errObj));
|
|
||||||
assert.throws(() => fs.lchmodSync(f, input), errObj);
|
assert.throws(() => fs.lchmodSync(f, input), errObj);
|
||||||
});
|
});
|
||||||
|
@ -98,15 +98,36 @@ for (const extra of [[], ['r'], ['r', 0], ['r', 0, 'bad callback']]) {
|
|||||||
type: TypeError
|
type: TypeError
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
fs.promises.open(i, 'r')
|
assert.rejects(
|
||||||
.then(common.mustNotCall())
|
fs.promises.open(i, 'r'),
|
||||||
.catch(common.mustCall((err) => {
|
{
|
||||||
common.expectsError(
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
() => { throw err; },
|
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
|
||||||
{
|
}
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
);
|
||||||
type: TypeError
|
});
|
||||||
}
|
|
||||||
);
|
// Check invalid modes.
|
||||||
}));
|
[false, [], {}].forEach((mode) => {
|
||||||
|
assert.throws(
|
||||||
|
() => fs.open(__filename, 'r', mode, common.mustNotCall()),
|
||||||
|
{
|
||||||
|
message: /'mode' must be a 32-bit/,
|
||||||
|
code: 'ERR_INVALID_ARG_VALUE'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert.throws(
|
||||||
|
() => fs.openSync(__filename, 'r', mode, common.mustNotCall()),
|
||||||
|
{
|
||||||
|
message: /'mode' must be a 32-bit/,
|
||||||
|
code: 'ERR_INVALID_ARG_VALUE'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert.rejects(
|
||||||
|
fs.promises.open(__filename, 'r', mode),
|
||||||
|
{
|
||||||
|
message: /'mode' must be a 32-bit/,
|
||||||
|
code: 'ERR_INVALID_ARG_VALUE'
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user