lib: remove return values from validation functions
This makes sure the validation functions do not cause any side effects. Validation functions should ideally only validate the input without any other effect. Since the input value must be known from the callee, there is no reason to return the input value. PR-URL: https://github.com/nodejs/node/pull/26809 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
50a3fe20ea
commit
6c913fb028
@ -76,31 +76,42 @@ function check(password, salt, keylen, options) {
|
|||||||
|
|
||||||
password = validateArrayBufferView(password, 'password');
|
password = validateArrayBufferView(password, 'password');
|
||||||
salt = validateArrayBufferView(salt, 'salt');
|
salt = validateArrayBufferView(salt, 'salt');
|
||||||
keylen = validateUint32(keylen, 'keylen');
|
validateUint32(keylen, 'keylen');
|
||||||
|
|
||||||
let { N, r, p, maxmem } = defaults;
|
let { N, r, p, maxmem } = defaults;
|
||||||
if (options && options !== defaults) {
|
if (options && options !== defaults) {
|
||||||
let has_N, has_r, has_p;
|
let has_N, has_r, has_p;
|
||||||
if (has_N = (options.N !== undefined))
|
if (has_N = (options.N !== undefined)) {
|
||||||
N = validateUint32(options.N, 'N');
|
validateUint32(options.N, 'N');
|
||||||
|
N = options.N;
|
||||||
|
}
|
||||||
if (options.cost !== undefined) {
|
if (options.cost !== undefined) {
|
||||||
if (has_N) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
|
if (has_N) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
|
||||||
N = validateUint32(options.cost, 'cost');
|
validateUint32(options.cost, 'cost');
|
||||||
|
N = options.cost;
|
||||||
|
}
|
||||||
|
if (has_r = (options.r !== undefined)) {
|
||||||
|
validateUint32(options.r, 'r');
|
||||||
|
r = options.r;
|
||||||
}
|
}
|
||||||
if (has_r = (options.r !== undefined))
|
|
||||||
r = validateUint32(options.r, 'r');
|
|
||||||
if (options.blockSize !== undefined) {
|
if (options.blockSize !== undefined) {
|
||||||
if (has_r) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
|
if (has_r) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
|
||||||
r = validateUint32(options.blockSize, 'blockSize');
|
validateUint32(options.blockSize, 'blockSize');
|
||||||
|
r = options.blockSize;
|
||||||
|
}
|
||||||
|
if (has_p = (options.p !== undefined)) {
|
||||||
|
validateUint32(options.p, 'p');
|
||||||
|
p = options.p;
|
||||||
}
|
}
|
||||||
if (has_p = (options.p !== undefined))
|
|
||||||
p = validateUint32(options.p, 'p');
|
|
||||||
if (options.parallelization !== undefined) {
|
if (options.parallelization !== undefined) {
|
||||||
if (has_p) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
|
if (has_p) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
|
||||||
p = validateUint32(options.parallelization, 'parallelization');
|
validateUint32(options.parallelization, 'parallelization');
|
||||||
|
p = options.parallelization;
|
||||||
|
}
|
||||||
|
if (options.maxmem !== undefined) {
|
||||||
|
validateUint32(options.maxmem, 'maxmem');
|
||||||
|
maxmem = options.maxmem;
|
||||||
}
|
}
|
||||||
if (options.maxmem !== undefined)
|
|
||||||
maxmem = validateUint32(options.maxmem, 'maxmem');
|
|
||||||
if (N === 0) N = defaults.N;
|
if (N === 0) N = defaults.N;
|
||||||
if (r === 0) r = defaults.r;
|
if (r === 0) r = defaults.r;
|
||||||
if (p === 0) p = defaults.p;
|
if (p === 0) p = defaults.p;
|
||||||
|
@ -60,7 +60,6 @@ const validateInteger = hideStackFrames((value, name) => {
|
|||||||
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
|
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
|
||||||
if (!Number.isSafeInteger(value))
|
if (!Number.isSafeInteger(value))
|
||||||
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
|
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
|
||||||
return value;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const validateInt32 = hideStackFrames(
|
const validateInt32 = hideStackFrames(
|
||||||
@ -78,7 +77,6 @@ const validateInt32 = hideStackFrames(
|
|||||||
if (value < min || value > max) {
|
if (value < min || value > max) {
|
||||||
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
|
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
|
||||||
}
|
}
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -97,9 +95,6 @@ const validateUint32 = hideStackFrames((value, name, positive) => {
|
|||||||
if (positive && value === 0) {
|
if (positive && value === 0) {
|
||||||
throw new ERR_OUT_OF_RANGE(name, '>= 1 && < 4294967296', value);
|
throw new ERR_OUT_OF_RANGE(name, '>= 1 && < 4294967296', value);
|
||||||
}
|
}
|
||||||
// TODO(BridgeAR): Remove return values from validation functions and
|
|
||||||
// especially reduce side effects caused by validation functions.
|
|
||||||
return value;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function validateString(value, name) {
|
function validateString(value, name) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user