Skip to content

Commit 6c913fb

Browse files
committed
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: #26809 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 50a3fe2 commit 6c913fb

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

lib/internal/crypto/scrypt.js

+23-12
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,42 @@ function check(password, salt, keylen, options) {
7676

7777
password = validateArrayBufferView(password, 'password');
7878
salt = validateArrayBufferView(salt, 'salt');
79-
keylen = validateUint32(keylen, 'keylen');
79+
validateUint32(keylen, 'keylen');
8080

8181
let { N, r, p, maxmem } = defaults;
8282
if (options && options !== defaults) {
8383
let has_N, has_r, has_p;
84-
if (has_N = (options.N !== undefined))
85-
N = validateUint32(options.N, 'N');
84+
if (has_N = (options.N !== undefined)) {
85+
validateUint32(options.N, 'N');
86+
N = options.N;
87+
}
8688
if (options.cost !== undefined) {
8789
if (has_N) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
88-
N = validateUint32(options.cost, 'cost');
90+
validateUint32(options.cost, 'cost');
91+
N = options.cost;
92+
}
93+
if (has_r = (options.r !== undefined)) {
94+
validateUint32(options.r, 'r');
95+
r = options.r;
8996
}
90-
if (has_r = (options.r !== undefined))
91-
r = validateUint32(options.r, 'r');
9297
if (options.blockSize !== undefined) {
9398
if (has_r) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
94-
r = validateUint32(options.blockSize, 'blockSize');
99+
validateUint32(options.blockSize, 'blockSize');
100+
r = options.blockSize;
101+
}
102+
if (has_p = (options.p !== undefined)) {
103+
validateUint32(options.p, 'p');
104+
p = options.p;
95105
}
96-
if (has_p = (options.p !== undefined))
97-
p = validateUint32(options.p, 'p');
98106
if (options.parallelization !== undefined) {
99107
if (has_p) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
100-
p = validateUint32(options.parallelization, 'parallelization');
108+
validateUint32(options.parallelization, 'parallelization');
109+
p = options.parallelization;
110+
}
111+
if (options.maxmem !== undefined) {
112+
validateUint32(options.maxmem, 'maxmem');
113+
maxmem = options.maxmem;
101114
}
102-
if (options.maxmem !== undefined)
103-
maxmem = validateUint32(options.maxmem, 'maxmem');
104115
if (N === 0) N = defaults.N;
105116
if (r === 0) r = defaults.r;
106117
if (p === 0) p = defaults.p;

lib/internal/validators.js

-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ const validateInteger = hideStackFrames((value, name) => {
6060
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
6161
if (!Number.isSafeInteger(value))
6262
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
63-
return value;
6463
});
6564

6665
const validateInt32 = hideStackFrames(
@@ -78,7 +77,6 @@ const validateInt32 = hideStackFrames(
7877
if (value < min || value > max) {
7978
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
8079
}
81-
return value;
8280
}
8381
);
8482

@@ -97,9 +95,6 @@ const validateUint32 = hideStackFrames((value, name, positive) => {
9795
if (positive && value === 0) {
9896
throw new ERR_OUT_OF_RANGE(name, '>= 1 && < 4294967296', value);
9997
}
100-
// TODO(BridgeAR): Remove return values from validation functions and
101-
// especially reduce side effects caused by validation functions.
102-
return value;
10398
});
10499

105100
function validateString(value, name) {

0 commit comments

Comments
 (0)