Skip to content

Commit

Permalink
lib: fix misleading argument of validateUint32
Browse files Browse the repository at this point in the history
The type of the argument `positive` was declared as `boolean|number`,
which is misleading because the function treats it as a boolean only.
Some call sites even passed numbers, specifically, either `0` or `1`,
which happen to work as expected because they are interpreted as `false`
and `true`, respectively. However, passing `2` would silently lead to
unexpected behavior. Thus, strictly make the argument a boolean.

PR-URL: #53307
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Zeyu "Alex" Yang <[email protected]>
Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
tniessen authored and marco-ippolito committed Jul 19, 2024
1 parent 37e725a commit 8689ce4
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function stopTest(timeout, signal) {

class TestPlan {
constructor(count) {
validateUint32(count, 'count', 0);
validateUint32(count, 'count');
this.expected = count;
this.actual = 0;
}
Expand Down Expand Up @@ -388,7 +388,7 @@ class Test extends AsyncResource {

switch (typeof concurrency) {
case 'number':
validateUint32(concurrency, 'options.concurrency', 1);
validateUint32(concurrency, 'options.concurrency', true);
this.concurrency = concurrency;
break;

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const validateInt32 = hideStackFrames(
* @callback validateUint32
* @param {*} value
* @param {string} name
* @param {number|boolean} [positive=false]
* @param {boolean} [positive=false]
* @returns {asserts value is number}
*/

Expand Down
2 changes: 1 addition & 1 deletion lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ function getHeapCodeStatistics() {

let heapSnapshotNearHeapLimitCallbackAdded = false;
function setHeapSnapshotNearHeapLimit(limit) {
validateUint32(limit, 'limit', 1);
validateUint32(limit, 'limit', true);
if (heapSnapshotNearHeapLimitCallbackAdded ||
getOptionValue('--heapsnapshot-near-heap-limit') > 0
) {
Expand Down

0 comments on commit 8689ce4

Please sign in to comment.