Repository navigation
Conversation
A for loop over range(a, b, step) with a literal step other than 1 or -1 could overflow the index near the limits of its type, and keep looping with a wrapped-around index. For example, with u8 bounds, range(250, 255, 2) produced 250, 252, 254, 0, 2, ... For a native int index, exit the loop before the increment if index + step wouldn't fit in the type. Such a value would be past the end anyway. This is a single compare against a constant. It's skipped if the step is 1 or -1, or if the end is a constant that rules out overflow. For a short int index, fall back to an int index if the step could overflow a short int. This happens only for literal bounds near +-2**62 or huge steps. Also load a step that doesn't fit in a short int as an int literal. Previously this generated an out-of-range C literal, which either broke the build or silently wrapped the step (so the loop could hang). Alternative to #22470, since this generates a bit simpler IR and appears to be faster on my mac laptop (perf is similar on x86-64 Linux).
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.
A for loop over range(a, b, step) with a literal step other than 1 or -1
could overflow the index near the limits of its type, and keep looping with
a wrapped-around index. For example, with u8 bounds, range(250, 255, 2)
produced 250, 252, 254, 0, 2, ...
For a native int index, exit the loop before the increment if index + step
wouldn't fit in the type. Such a value would be past the end anyway. This is
a single compare against a constant. It's skipped if the step is 1 or -1, or
if the end is a constant that rules out overflow.
For a short int index, fall back to an int index if the step could overflow
a short int. This happens only for literal bounds near +-2**62 or huge
steps. Also load a step that doesn't fit in a short int as an int literal.
Previously this generated an out-of-range C literal, which either broke the
build or silently wrapped the step (so the loop could hang).
Alternative to #22470, since this generates a bit simpler IR and appears
to be faster on my mac laptop (perf is similar on x86-64 Linux).