checker: add an error when high overflows low in a for-in range #27854
Open
rilaaax wants to merge 2 commits into
Open
checker: add an error when high overflows low in a for-in range #27854rilaaax wants to merge 2 commits into
high overflows low in a for-in range #27854rilaaax wants to merge 2 commits into
Conversation
Contributor
|
While testing the PR, I found two remaining edge cases:
These appear to be one newly rejected valid case and one overflow case that remains undetected. Could you please take a look? Thanks! |
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.
Fixes #27728
Problem
A
for i in low .. highloop wherelowhas an explicit narrow integer type (e.g.u8) andhighis a compile-time constant that exceeds that type's range (e.g.for b in min_u8 .. int(max_u8) + 1) compiles and runs, but results in an infinite loop: the loop variable's type silently defaults to the narrow type oflow, so it wraps around (255 -> 0 foru8) instead of ever reachinghigh, and the loop never terminates.Cause
In
for_in_stmt(vlib/v/checker/for.v), wheneverhigh's type isint/int_literal, the checker unconditionally sets the loop variable's type (node.val_type) to the type oflow, without checking whetherhigh's value actually fits in that (potentially narrower) type. The C code generator then emits a loop likefor (u8 b = 0; b < 256; ++b), wherebcan never reach256because it overflows and wraps back to0first.Fix
Added a check in
for_in_stmt: whenlow's type is a fixed-width integer type narrower thanint/i64/u64(i8,i16,i32,u8,u16,u32), andhighcan be evaluated as a compile-time constant (via the existingeval_comptime_const_expr, which already resolves builtin constants likemax_u8and simple casts/arithmetic), the checker now verifies that this constant value fits within the maximum representable value oflow's type. If it doesn't, a compile-time error is raised instead of silently generating an infinite loop. Ranges whose bounds cannot be evaluated statically (e.g. built from variables) are left unaffected, since this cannot be reliably checked at compile time.Test
Regression test added.