mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 07:19:19 +00:00

- Introduce the `validateAndMaskMode` validator that validates `mode_t` arguments and mask them with 0o777 if they are 32-bit unsigned integer or octal string to be more consistent with POSIX APIs. - Use the validator in fs APIs and process.umask for consistency. - Add tests for 32-bit unsigned modes larger than 0o777. PR-URL: https://github.com/nodejs/node/pull/20636 Fixes: https://github.com/nodejs/node/issues/20498 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
95 lines
2.5 KiB
JavaScript
95 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
ERR_INVALID_ARG_TYPE,
|
|
ERR_INVALID_ARG_VALUE,
|
|
ERR_OUT_OF_RANGE
|
|
} = require('internal/errors').codes;
|
|
|
|
function isInt32(value) {
|
|
return value === (value | 0);
|
|
}
|
|
|
|
function isUint32(value) {
|
|
return value === (value >>> 0);
|
|
}
|
|
|
|
const octalReg = /^[0-7]+$/;
|
|
const modeDesc = 'must be a 32-bit unsigned integer or an octal string';
|
|
// Validator for mode_t (the S_* constants). Valid numbers or octal strings
|
|
// will be masked with 0o777 to be consistent with the behavior in POSIX APIs.
|
|
function validateAndMaskMode(value, name, def) {
|
|
if (isUint32(value)) {
|
|
return value & 0o777;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
if (typeof value === 'string') {
|
|
if (!octalReg.test(value)) {
|
|
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
|
|
}
|
|
const parsed = parseInt(value, 8);
|
|
return parsed & 0o777;
|
|
}
|
|
|
|
// TODO(BridgeAR): Only return `def` in case `value == null`
|
|
if (def !== undefined) {
|
|
return def;
|
|
}
|
|
|
|
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
|
|
}
|
|
|
|
function validateInt32(value, name) {
|
|
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
|
|
err = new ERR_OUT_OF_RANGE(name, '> -2147483649 && < 2147483648', value);
|
|
}
|
|
Error.captureStackTrace(err, validateInt32);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
function validateUint32(value, name, positive) {
|
|
if (!isUint32(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 {
|
|
const min = positive ? 1 : 0;
|
|
// 2 ** 32 === 4294967296
|
|
err = new ERR_OUT_OF_RANGE(name, `>= ${min} && < 4294967296`, value);
|
|
}
|
|
Error.captureStackTrace(err, validateUint32);
|
|
throw err;
|
|
} else if (positive && value === 0) {
|
|
const err = new ERR_OUT_OF_RANGE(name, '>= 1 && < 4294967296', value);
|
|
Error.captureStackTrace(err, validateUint32);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
isInt32,
|
|
isUint32,
|
|
validateAndMaskMode,
|
|
validateInt32,
|
|
validateUint32
|
|
};
|