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 fchmod{Sync} validation #20588

Merged
merged 2 commits into from
May 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ fs.unlinkSync = function(path) {
};

fs.fchmod = function(fd, mode, callback) {
validateUint32(fd, 'fd');
validateInt32(fd, 'fd', 0);
mode = validateAndMaskMode(mode, 'mode');
callback = makeCallback(callback);

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

fs.fchmodSync = function(fd, mode) {
validateUint32(fd, 'fd');
validateInt32(fd, 'fd', 0);
mode = validateAndMaskMode(mode, 'mode');
const ctx = {};
binding.fchmod(fd, mode, undefined, ctx);
Expand Down
10 changes: 7 additions & 3 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,23 @@ function validateAndMaskMode(value, name, def) {
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
}

function validateInt32(value, name) {
function validateInt32(value, name, min = -2147483648, max = 2147483647) {
// The defaults for min and max correspond to the limits of 32-bit integers.
if (!isInt32(value)) {
let err;
if (typeof value !== 'number') {
err = new ERR_INVALID_ARG_TYPE(name, 'number', value);
} else if (!Number.isInteger(value)) {
err = new ERR_OUT_OF_RANGE(name, 'an integer', value);
} else {
// 2 ** 31 === 2147483648
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move the comment to the beginning of this function ?

err = new ERR_OUT_OF_RANGE(name, '> -2147483649 && < 2147483648', value);
err = new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
}
Error.captureStackTrace(err, validateInt32);
throw err;
} else if (value < min || value > max) {
const err = new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
Error.captureStackTrace(err, validateInt32);
throw err;
}
}

Expand Down
15 changes: 12 additions & 3 deletions test/parallel/test-fs-fchmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ const fs = require('fs');
const errObj = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "fd" is out of range. It must be >= 0 && < ' +
`${2 ** 32}. Received ${input}`
message: 'The value of "fd" is out of range. It must be >= 0 && <= ' +
`2147483647. Received ${input}`
};
assert.throws(() => fs.fchmod(input), errObj);
assert.throws(() => fs.fchmodSync(input), errObj);
errObj.message = errObj.message.replace('fd', 'mode');
});

[-1, 2 ** 32].forEach((input) => {
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}`
};

assert.throws(() => fs.fchmod(1, input), errObj);
assert.throws(() => fs.fchmodSync(1, input), errObj);
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function testFtruncate(cb) {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "len" is out of range. It must be ' +
`> -2147483649 && < 2147483648. Received ${input}`
`>= -2147483648 && <= 2147483647. Received ${input}`
}
);
});
Expand Down