From 3983010835e49e99d99d67bf10b3248ab84d45da Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Thu, 15 May 2025 21:38:50 -0700 Subject: [PATCH] fix `test-set-process-debug-port.js` (#19662) Co-authored-by: Ciro Spaciari --- src/bun.js/bindings/BunProcess.cpp | 30 ++++----- .../parallel/test-set-process-debug-port.js | 64 +++++++++++++++++++ 2 files changed, 76 insertions(+), 18 deletions(-) create mode 100644 test/js/node/test/parallel/test-set-process-debug-port.js diff --git a/src/bun.js/bindings/BunProcess.cpp b/src/bun.js/bindings/BunProcess.cpp index 4752d5cfc8..7d8f7882f2 100644 --- a/src/bun.js/bindings/BunProcess.cpp +++ b/src/bun.js/bindings/BunProcess.cpp @@ -3310,15 +3310,11 @@ static JSValue constructFeatures(VM& vm, JSObject* processObject) return object; } -static int _debugPort; +static uint16_t debugPort; JSC_DEFINE_CUSTOM_GETTER(processDebugPort, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) { - if (_debugPort == 0) { - _debugPort = 9229; - } - - return JSC::JSValue::encode(jsNumber(_debugPort)); + return JSC::JSValue::encode(jsNumber(debugPort)); } JSC_DEFINE_CUSTOM_SETTER(setProcessDebugPort, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::EncodedJSValue encodedValue, JSC::PropertyName)) @@ -3327,21 +3323,19 @@ JSC_DEFINE_CUSTOM_SETTER(setProcessDebugPort, (JSC::JSGlobalObject * globalObjec auto scope = DECLARE_THROW_SCOPE(vm); JSValue value = JSValue::decode(encodedValue); - if (!value.isInt32AsAnyInt()) { - throwNodeRangeError(globalObject, scope, "debugPort must be 0 or in range 1024 to 65535"_s); + double port = value.toNumber(globalObject); + RETURN_IF_EXCEPTION(scope, {}); + + if (std::isnan(port) || std::isinf(port)) { + port = 0; + } + + if ((port != 0 && port < 1024) || port > 65535) { + throwNodeRangeError(globalObject, scope, "process.debugPort must be 0 or in range 1024 to 65535"_s); return false; } - int port = value.toInt32(globalObject); - - if (port != 0) { - if (port < 1024 || port > 65535) { - throwNodeRangeError(globalObject, scope, "debugPort must be 0 or in range 1024 to 65535"_s); - return false; - } - } - - _debugPort = port; + debugPort = floor(port); return true; } diff --git a/test/js/node/test/parallel/test-set-process-debug-port.js b/test/js/node/test/parallel/test-set-process-debug-port.js new file mode 100644 index 0000000000..dc0a4b9097 --- /dev/null +++ b/test/js/node/test/parallel/test-set-process-debug-port.js @@ -0,0 +1,64 @@ +'use strict'; +const common = require('../common'); + +common.skipIfInspectorDisabled(); +const { isMainThread } = require('worker_threads'); + +if (!isMainThread) { + common.skip('This test only works on a main thread'); +} + +const assert = require('assert'); +const kMinPort = 1024; +const kMaxPort = 65535; + +function check(value, expected) { + process.debugPort = value; + assert.strictEqual(process.debugPort, expected); +} + +// Expected usage with numbers. +check(0, 0); +check(kMinPort, kMinPort); +check(kMinPort + 1, kMinPort + 1); +check(kMaxPort - 1, kMaxPort - 1); +check(kMaxPort, kMaxPort); + +// Numeric strings coerce. +check('0', 0); +check(`${kMinPort}`, kMinPort); +check(`${kMinPort + 1}`, kMinPort + 1); +check(`${kMaxPort - 1}`, kMaxPort - 1); +check(`${kMaxPort}`, kMaxPort); + +// Most other values are coerced to 0. +check('', 0); +check(false, 0); +check(NaN, 0); +check(Infinity, 0); +check(-Infinity, 0); +check(function() {}, 0); +check({}, 0); +check([], 0); + +// Symbols do not coerce. +assert.throws(() => { + process.debugPort = Symbol(); +}, /^TypeError: Cannot convert a symbol to a number$/); + +// Verify port bounds checking. +[ + true, + -1, + 1, + kMinPort - 1, + kMaxPort + 1, + '-1', + '1', + `${kMinPort - 1}`, + `${kMaxPort + 1}`, +].forEach((value) => { + assert.throws(() => { + process.debugPort = value; + }, /^RangeError: process\.debugPort must be 0 or in range 1024 to 65535$/); +});