Skip to content

fix: don't panic on a constant non-bool condition - #1729

Open
Endika wants to merge 1 commit into
traefik:masterfrom
Endika:fix-non-bool-constant-condition-panic
Open

fix: don't panic on a constant non-bool condition#1729
Endika wants to merge 1 commit into
traefik:masterfrom
Endika:fix-non-bool-constant-condition-panic

Conversation

@Endika

@Endika Endika commented Jul 27, 2026

Copy link
Copy Markdown

A non-bool condition in if or for is reported as an error when it is a variable, but
crashes when it is a constant:

package main

func main() {
        if 1 {
        }
}
$ yaegi run cond.go
panic: reflect: call of reflect.Value.Bool on int64 Value [recovered]
        panic: cond.go:4:2: CFG post-order panic: reflect: call of reflect.Value.Bool on int64 Value

Expected, and what var i int; if i {} already produces today: 4:5: non-bool used as if condition. String, float and typed constants panic the same way, and so do the for forms.
Since the panic escapes, it takes down a host program that embeds yaegi.

The check records the error but does not stop the case, so execution reaches the
compile-time-constant fast path and calls rval.Bool() on a value that is not a bool. A
non-constant condition has no valid rval and takes the else branch, which is why only the
constant case crashes. This is the part left uncovered when #438 was fixed.

The fix breaks out of the case as soon as the error is recorded, as the other cfgErrorf sites
in this switch already do, for all eight condition forms. Five _test files cover the plain
if, the if/else form, if with an init statement, the bare for and the three-clause
for.

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.

1 participant