From 505e9a425b559dae25e996fba592c8a78dd34832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Thu, 6 Jun 2024 00:59:05 +0200 Subject: [PATCH] lib: fix misleading argument of validateUint32 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: https://github.com/nodejs/node/pull/53307 Reviewed-By: Luigi Pinca Reviewed-By: Yagiz Nizipli Reviewed-By: Zeyu "Alex" Yang Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Antoine du Hamel --- lib/internal/test_runner/test.js | 4 ++-- lib/internal/validators.js | 2 +- lib/v8.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 6874227b7cd2d8..eacaf62bb34d11 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -194,7 +194,7 @@ function testMatchesPattern(test, patterns) { class TestPlan { constructor(count) { - validateUint32(count, 'count', 0); + validateUint32(count, 'count'); this.expected = count; this.actual = 0; } @@ -428,7 +428,7 @@ class Test extends AsyncResource { switch (typeof concurrency) { case 'number': - validateUint32(concurrency, 'options.concurrency', 1); + validateUint32(concurrency, 'options.concurrency', true); this.concurrency = concurrency; break; diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 7bda5150a97c0f..298eab9bbdba2f 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -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} */ diff --git a/lib/v8.js b/lib/v8.js index fcc1de6703a51e..d6f7d2ca47251a 100644 --- a/lib/v8.js +++ b/lib/v8.js @@ -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 ) {