diff --git a/src/node_debug_options.cc b/src/node_debug_options.cc index 410e23acb3101b..88c5af03d7fc9a 100644 --- a/src/node_debug_options.cc +++ b/src/node_debug_options.cc @@ -22,7 +22,8 @@ int parse_and_validate_port(const std::string& port) { char* endptr; errno = 0; const long result = strtol(port.c_str(), &endptr, 10); // NOLINT(runtime/int) - if (errno != 0 || *endptr != '\0'|| result < 1024 || result > 65535) { + if (errno != 0 || *endptr != '\0' || + (result != 0 && (result < 1024 || result > 65535))) { fprintf(stderr, "Debug port must be in range 1024 to 65535.\n"); exit(12); } diff --git a/test/parallel/test-inspector-ephemeral-port.js b/test/parallel/test-inspector-ephemeral-port.js new file mode 100644 index 00000000000000..f3c3314157365d --- /dev/null +++ b/test/parallel/test-inspector-ephemeral-port.js @@ -0,0 +1,23 @@ +'use strict'; +const common = require('../common'); +common.skipIfInspectorDisabled(); + +// Check that inspector can run on an ephemeral port. + +const assert = require('assert'); +const exec = require('child_process').exec; + +const printA = require.resolve('../fixtures/printA.js'); +const cmd = [ + process.execPath, + '--inspect=0', + printA, +].join(' '); + +exec(cmd, common.mustCall(function(err, _, stderr) { + assert.ifError(err); + const port1 = Number(stderr.match(/Debugger listening on port (\d+)/)[1]); + const port2 = Number(stderr.match(/chrome-devtools.*:(\d+)/)[1]); + assert.strictEqual(port1, port2); + assert(port1 > 0); +}));