Repository navigation
[mypyc] Fix librt.random ops treating -113 results as errors - #22420
Merged
JukkaL merged 1 commit intoNov 9, 2026
Merged
Conversation
Random.randint, Random.randrange(start, stop) and the module-level randint/randrange(start, stop) primitives used ERR_MAGIC, so a valid result of -113 (the i64 error value) was treated as an error without checking PyErr_Occurred(). This propagated a NULL exception and crashed. Use ERR_MAGIC_OVERLAPPING for these ops. randrange(stop) can only return non-negative values, so it keeps ERR_MAGIC.
JukkaL
approved these changes
Nov 9, 2026
JukkaL
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the fix, looks good!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Several
librt.randomprimitives returningi64were registered withERR_MAGIC:Random.randint,Random.randrange(start, stop), and the module-levelrandint/randrange(start, stop). Their valid output range includes-113, which is thei64error value, so a legitimate-113result took the error path without checkingPyErr_Occurred()and crashed.Fix: use
ERR_MAGIC_OVERLAPPINGfor these ops. The C implementations already set an exception on every error path.randrange(stop)only returns values in[0, stop)andrandom()only returns values in[0.0, 1.0), so both keepERR_MAGIC.I also checked the other librt primitives (strings, time, threading, vecs, base64). Each one either already uses
ERR_MAGIC_OVERLAPPINGor returns an object,Noneorbool, where a valid result can't equal the error value.