Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interp: fix the behaviour of goto, continue and break #1392

Merged
merged 8 commits into from
May 19, 2022

Conversation

mvertes
Copy link
Member

@mvertes mvertes commented Apr 28, 2022

The logic of goto was false due to mixing break label and goto
label, despite being opposite. In case of break, jump to node (the
exit point) instead of node.start. Also always define label symbols
before their use is parsed.

Fixes #1354.

The logic of goto was false due to mixing break label and goto
label, despite being opposite. In case of break, jump to node (the
exit point) instead of node.start. Also always define label symbols
before their use is parsed.

Fixes traefik#1354.
@mvertes mvertes added this to the v0.11.x milestone Apr 28, 2022
@mvertes mvertes requested a review from mpl April 28, 2022 14:26
@mpl
Copy link
Collaborator

mpl commented May 6, 2022

Are you aware that even after this PR, it seems that continue is still broken? i.e. it behaves like a break, and exits the enclosing for, instead of advancing its execution. For example, in:

package main

func inbreak() {
	println("BREAK")
	n := 2
	m := 2
	foo := true
OuterLoop:
	for i := 0; i < n; i++ {
		println("I: ", i)
		for j := 0; j < m; j++ {
			switch foo {
			case true:
				println(foo)
				break OuterLoop
			case false:
				println(foo)
			}
		}
	}
}

func incontinue() {
	println("CONTINUE")
	n := 2
	m := 2
	foo := true
OuterLoop:
	for i := 0; i < n; i++ {
		println("I: ", i)
		for j := 0; j < m; j++ {
			switch foo {
			case true:
				println(foo)
				continue OuterLoop
			case false:
				println(foo)
			}
		}
	}
}

func ingoto() {
	println("GOTO")
	m := 2
	foo := true
	i := 0
OuterLoop:
	i++
	for {
		if i > 5 {
			break
		}
		println("I: ", i)
		for j := 0; j < m; j++ {
			switch foo {
			case true:
				println(foo)
				goto OuterLoop
			case false:
				println(foo)
			}
		}
	}
}

func main() {
	inbreak()
	incontinue()
	ingoto()
}

// Output:
// BREAK
// I:  0
// true
// CONTINUE
// I:  0
// true
// I:  1
// true
// GOTO
// I:  1
// true
// I:  2
// true
// I:  3
// true
// I:  4
// true
// I:  5
// true

the behavior of incontinue() is not the same with Go and with Yaegi.

@mvertes mvertes changed the title interp: fix the behaviour of goto and break interp: fix the behaviour of goto continue and break May 16, 2022
@mvertes mvertes changed the title interp: fix the behaviour of goto continue and break interp: fix the behaviour of goto, continue and break May 16, 2022
interp/cfg.go Outdated Show resolved Hide resolved
interp/cfg.go Show resolved Hide resolved
interp/cfg.go Outdated
err = n.cfgErrorf("invalid continue label %s", n.child[0].ident)
break
}
for _, c := range n.sym.from {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same remark as above.

interp/cfg.go Outdated
if n.sym.node == nil {
break
}
for _, c := range n.sym.from {
Copy link
Collaborator

Choose a reason for hiding this comment

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

same remark as in break and continue.

interp/cfg.go Outdated
gotoLabel(n.sym)
for _, c := range n.sym.from {
if c.kind == gotoStmt {
c.tnext = n.start
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we are not in a forward case, it seems useless to do that, since the code in the goto case (L887) will overwrite c.tnext.

If we are in the forward case, we are overwriting c.tnext here. Is that what we want to do?

@mvertes mvertes merged commit 00e3f92 into traefik:master May 19, 2022
@mvertes mvertes deleted the fix-1354 branch May 19, 2022 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unreachable goto affects program behavior
3 participants