Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: improve mode validation #26575

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,17 @@ function validateMode(value, name, def) {
}

if (typeof value === 'number') {
if (!Number.isInteger(value)) {
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
} else {
// 2 ** 32 === 4294967296
throw new ERR_OUT_OF_RANGE(name, '>= 0 && < 4294967296', value);
}
validateInt32(value, name, 0, 2 ** 32 - 1);
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
}

if (typeof value === 'string') {
if (!octalReg.test(value)) {
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
}
const parsed = parseInt(value, 8);
return parsed;
return parseInt(value, 8);
}

// TODO(BridgeAR): Only return `def` in case `value == null`
if (def !== undefined) {
if (def !== undefined && value == null) {
return def;
}

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-fs-fchmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const fs = require('fs');
const errObj = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "mode" is out of range. It must be >= 0 && < ' +
`4294967296. Received ${input}`
message: 'The value of "mode" is out of range. It must be >= 0 && <= ' +
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
`4294967295. Received ${input}`
};

assert.throws(() => fs.fchmod(1, input), errObj);
Expand Down
43 changes: 32 additions & 11 deletions test/parallel/test-fs-open.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,36 @@ for (const extra of [[], ['r'], ['r', 0], ['r', 0, 'bad callback']]) {
type: TypeError
}
);
fs.promises.open(i, 'r')
.then(common.mustNotCall())
.catch(common.mustCall((err) => {
common.expectsError(
() => { throw err; },
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
}
);
}));
assert.rejects(
fs.promises.open(i, 'r'),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
}
);
});

// 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'
}
);
});