node icon indicating copy to clipboard operation
node copied to clipboard

process: add `code` validation in `process.exit()`

Open daeyeon opened this issue 1 year ago • 15 comments

This commit validates the code in process.exit([code]).

According to the API doc, the code argument must be an integer value. However, the current implementation accepts various types of values and internally converts them into integer values. ~Also, the actual meaningful code range is 0 - 255, but it doesn't validate the range.~ The range of the code is OS-dependent.

The following are the current behaviors in the Linux shell:

$ node -e "process.exit('')"; echo $?
0
$ node -e "process.exit('2')"; echo $?
2
$ node -e "process.exit('256')"; echo $?
0
$ node -e "process.exit(-1)"; echo $?
255
$ node -e "process.exit([])"; echo $?
0
$ node -e "process.exit({})"; echo $?
0
$ node -e "process.exit('ERROR')"; echo $?
0

Signed-off-by: Daeyeon Jeong [email protected]

daeyeon avatar Jul 07 '22 12:07 daeyeon

Should this be semver-major, as this could easily cause crashes to apparently working programs? if Node already handles non-string values in a somewhat sane fashion, maybe the docs need to be changed instead?

Linkgoron avatar Jul 07 '22 15:07 Linkgoron

IMHO, if there aren't any other node APIs that operate like this way, for consistency, semver-major seems convincing.

daeyeon avatar Jul 07 '22 16:07 daeyeon

That's very much semver major, process.exit(-1) is a common value to indicate an error (https://github.com/search?q=process.exit%28-1%29&type=code), I don't think it's worth breaking at this point.

aduh95 avatar Jul 07 '22 17:07 aduh95

@aduh95 @tniessen Thanks for the review. I removed the range restriction, PTAL.

daeyeon avatar Jul 07 '22 23:07 daeyeon

@RaisinTen Thanks for explaining the deprecation procedures and the search result. I will create a new PR.

daeyeon avatar Jul 08 '22 17:07 daeyeon

I think strings that clearly represent an integer should continue to be supported as well as it would otherwise unnecessarly disrupt many users. The following should pretty much be accepted: input == null || Number.isInteger(input) || typeof input === 'string' && String(Number(input)) === input. That way weird strings would be rejected while integers get through.

BridgeAR avatar Jul 08 '22 18:07 BridgeAR

@BridgeAR Applied your view. Thanks!

daeyeon avatar Jul 09 '22 11:07 daeyeon

@aduh95 Thanks for the heads up. I updated process.md. Please let me know any other docs left.

daeyeon avatar Sep 09 '22 12:09 daeyeon

On second thought, validating the process.exitCode seems to be a better approach for process.exit([code]).

  1. The following case is still possible since there is no validation on process.exitCode.
process.exitCode = 'ERROR';
process.exit();

// Or 

process.exitCode = 'ERROR';
  1. In the current version of process.exit([code]), process.exitCode will be set to the code value if the parameter validation is passed. So, process.exit([code]) can delegate the validation to process.exitCode.
In a quick search for `process.exitCode` below, I can't find invalid uses but it may need the doc-only deprecation also.

Can you please take a look at https://github.com/nodejs/node/pull/43716/commits/d4ad2fe4aae7eced44b850c5baecfae9970049a6 again?

daeyeon avatar Sep 11 '22 00:09 daeyeon

I left the conflicts on purpose for easy review.

daeyeon avatar Sep 11 '22 00:09 daeyeon

CI: https://ci.nodejs.org/job/node-test-pull-request/46553/

nodejs-github-bot avatar Sep 12 '22 23:09 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/46568/

nodejs-github-bot avatar Sep 13 '22 15:09 nodejs-github-bot

Thanks for reminding our deprecation process. :-) I'll do the follow-up after the next major release.

daeyeon avatar Sep 15 '22 01:09 daeyeon

I'll do the follow-up after the next major release.

Would you be able to open a PR to add a runtime deprecation before the next major? v19.x semver cutoff is one month away, and if the runtime deprecation has not landed by then, this PR can't land until Node.js v21.x.. (assuming the runtime deprecation lands before Node.js 20.x semver cutoff)

aduh95 avatar Sep 15 '22 07:09 aduh95

Oh now I got it. I'll prepare a PR for a runtime deprecation so that it can be landed before the cutoff.

daeyeon avatar Sep 15 '22 09:09 daeyeon

This needs a rebase.

aduh95 avatar Oct 10 '22 00:10 aduh95

While rebasing the code I've squashed the previous commits because of many conflicts in several fixup commits. I will rebase this PR again once the release version for runtime deprecation is updated.

daeyeon avatar Oct 11 '22 07:10 daeyeon

CI: https://ci.nodejs.org/job/node-test-pull-request/47183/

nodejs-github-bot avatar Oct 11 '22 15:10 nodejs-github-bot

CITGM: https://ci.nodejs.org/view/Node.js-citgm/job/citgm-smoker/3026/ CITGM(19.0.0): https://ci.nodejs.org/view/Node.js-citgm/job/citgm-smoker/3023/

aduh95 avatar Oct 18 '22 14:10 aduh95

The runtime deprecation has been versioned as v19.0.0. Rebased to resolve a conflict from the change.

daeyeon avatar Oct 19 '22 14:10 daeyeon

CI: https://ci.nodejs.org/job/node-test-pull-request/47340/

nodejs-github-bot avatar Oct 19 '22 14:10 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/47341/

nodejs-github-bot avatar Oct 19 '22 16:10 nodejs-github-bot

Landed in 2d0d99733b86567f1261a55124a42fb3d90309d2

nodejs-github-bot avatar Oct 19 '22 18:10 nodejs-github-bot