From 8689ce4b41e9998a23547fc52d2c51dde13ac477 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 65d1fb00045201..58490ecac84b2c 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -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; } @@ -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; diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 6abf4d9a3cd7d3..2862a8f10c7bed 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 a9eee19fb9e19c..ae40a682079325 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 ) {