Skip to content

Conversation

garretcarrot
Copy link
Contributor

@garretcarrot garretcarrot commented Aug 4, 2025

Closes #30313

Changed:

  • Check that both min and max are safe integers before proceeding with the random integer calculation, to guard against a lack of arguments or an undefined first argument to the randomInt call.

I'm not sure why non-number values were prevented from being checked with NumberIsSafeInteger or why a generic Error was used instead of a TypeError — I assume there may have been reasons and so may have missed something here. But it did seem the simplest way to avoid the panic.

@CLAassistant
Copy link

CLAassistant commented Aug 4, 2025

CLA assistant check
All committers have signed the CLA.

Comment on lines 43 to 44
if (!NumberIsSafeInteger(min) || !NumberIsSafeInteger(max)) {
throw new TypeError("max or min is not a Safe Number");
Copy link
Contributor

@Tango992 Tango992 Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we follow Node.js's implementation, so that we can have the same error message:
https://github.com/nodejs/node/blob/9d2368f64329bf194c4e82b349e76fdad879d32a/lib/internal/crypto/random.js#L225-L230

You can import the error from here:

import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts";

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, of course — thanks for the suggestion

@bartlomieju
Copy link
Member

Thanks @garretcarrot, this looks good. Can you please run tools/format.js and push again?

@garretcarrot
Copy link
Contributor Author

Oops, I did for the first commit and not the last one. Would it be OK to change the other Error exceptions to use ERR_ types here, or is that more appropriate in a separate PR?

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@bartlomieju bartlomieju merged commit 038d5a5 into denoland:main Aug 5, 2025
30 of 35 checks passed
@garretcarrot garretcarrot deleted the fix_crypto_randomint_with_no_args branch August 5, 2025 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crypto.randomInt panics when called with no arguments

4 participants