diff options
author | Kenta Moriuchi <moriken@kimamass.com> | 2024-06-03 10:29:01 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-02 21:29:01 -0400 |
commit | eda43c46de12ed589fdbe62ba0574887cfbb3574 (patch) | |
tree | 9703b6c92b2e3ca21768d9ae6d82730c41e41157 /ext/node/polyfills/process.ts | |
parent | 754f21f0cd6b788ded48238c5acc3f635329d473 (diff) |
fix: validate integer values in `Deno.exitCode` setter (#24068)
Diffstat (limited to 'ext/node/polyfills/process.ts')
-rw-r--r-- | ext/node/polyfills/process.ts | 41 |
1 files changed, 13 insertions, 28 deletions
diff --git a/ext/node/polyfills/process.ts b/ext/node/polyfills/process.ts index 9a28137af..a001d2e0f 100644 --- a/ext/node/polyfills/process.ts +++ b/ext/node/polyfills/process.ts @@ -19,6 +19,7 @@ import { report } from "ext:deno_node/internal/process/report.ts"; import { validateString } from "ext:deno_node/internal/validators.mjs"; import { ERR_INVALID_ARG_TYPE, + ERR_OUT_OF_RANGE, ERR_UNKNOWN_SIGNAL, errnoException, } from "ext:deno_node/internal/errors.ts"; @@ -439,38 +440,22 @@ Object.defineProperty(Process.prototype, "exitCode", { return ProcessExitCode; }, set(code: number | string | null | undefined) { - let parsedCode; - - if (typeof code === "number") { - if (Number.isNaN(code)) { - parsedCode = 1; - denoOs.setExitCode(parsedCode); - ProcessExitCode = parsedCode; - return; - } - - // This is looser than `denoOs.setExitCode` which requires exit code - // to be decimal or string of a decimal, but Node accept eg. 0x10. - parsedCode = parseInt(code); - denoOs.setExitCode(parsedCode); - ProcessExitCode = parsedCode; - return; + let parsedCode: number; + if (code == null) { + parsedCode = 0; + } else if (typeof code === "number") { + parsedCode = code; + } else if (typeof code === "string") { + parsedCode = Number(code); + } else { + throw new ERR_INVALID_ARG_TYPE("code", "number", code); } - if (typeof code === "string") { - parsedCode = parseInt(code); - if (Number.isNaN(parsedCode)) { - throw new TypeError( - `The "code" argument must be of type number. Received type ${typeof code} (${code})`, - ); - } - denoOs.setExitCode(parsedCode); - ProcessExitCode = parsedCode; - return; + if (!Number.isInteger(parsedCode)) { + throw new ERR_OUT_OF_RANGE("code", "an integer", parsedCode); } - // TODO(bartlomieju): hope for the best here. This should be further tightened. - denoOs.setExitCode(code); + denoOs.setExitCode(parsedCode); ProcessExitCode = code; }, }); |