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

cmd/cover: Limit on number of consecutive if..then..else statements in "go test -cover" #10950

Closed
JoshVarga opened this issue May 26, 2015 · 4 comments

Comments

@JoshVarga
Copy link

@JoshVarga JoshVarga commented May 26, 2015

When compiling the following code "go build" and "go test" work as expected, however when runing "go test -cover" it gets:

/invalid.go:166: memory exhausted

invalid.go

package invalid
func Exhausted(i int) {
        if i == 1 {
        } else if i == 1 {
        } else if i == 2 {
        } else if i == 3 {
        } else if i == 4 {
        } else if i == 5 {
        } else if i == 6 {
        } else if i == 7 {
        } else if i == 8 {
        } else if i == 9 {
        } else if i == 10 {
        } else if i == 11 {
        } else if i == 12 {
        } else if i == 13 {
        } else if i == 14 {
        } else if i == 15 {
        } else if i == 16 {
        } else if i == 17 {
        } else if i == 18 {
        } else if i == 19 {
        } else if i == 20 {
        } else if i == 21 {
        } else if i == 22 {
        } else if i == 23 {
        } else if i == 24 {
        } else if i == 25 {
        } else if i == 26 {
        } else if i == 27 {
        } else if i == 28 {
        } else if i == 29 {
        } else if i == 30 {
        } else if i == 31 {
        } else if i == 32 {
        } else if i == 33 {
        } else if i == 34 {
        } else if i == 35 {
        } else if i == 36 {
        } else if i == 37 {
        } else if i == 38 {
        } else if i == 39 {
        } else if i == 40 {
        }
}

invalid_test.go

package invalid_test
import(
        "testing"
        . "invalid"
)
func TestExhausted(t *testing.T) {
        Exhausted(1)
}

The source does require 40 or more if-then-else statements on my machine to replicate this issue.

The error text is also confusing mainly because the file only has 46 lines yet it says the issue is on line 166 and it doesn't tell me what is actually wrong.

@dsymonds
Copy link
Member

@dsymonds dsymonds commented May 26, 2015

I can't reproduce this at HEAD (714291f), even if I extend it up to 80 conditionals.

Can you tell us what go version says? I suspect this was a bug that has already been fixed.

@dsymonds
Copy link
Member

@dsymonds dsymonds commented May 26, 2015

FYI, go tool cover -mode=set -var=cov invalid.go produces something pretty heavily nested, since it inserts a coverage point both between the else and if i == xx as well as inside each source block, so you get something looking like:

package invalid

func Exhausted(i int) {
        cov.Count[0] = 1
        if i == 1 {
                cov.Count[1] = 1
        } else {
                cov.Count[2] = 1
                if i == 1 {
                        cov.Count[3] = 1
                } else {
                        cov.Count[4] = 1
                        if i == 2 {
                                cov.Count[5] = 1
                        } else {
                                cov.Count[6] = 1
                                if i == 3 {
                                        cov.Count[7] = 1
                                } else {
                                        cov.Count[8] = 1
                                        if i == 4 {
                                                cov.Count[9] = 1
                                        } else {
                                                cov.Count[10] = 1
                                                if i == 5 {
                                                        cov.Count[11] = 1
                                                } else {
                                                        cov.Count[12] = 1
                                                        if i == 6 {
                                                                cov.Count[13] = 1
                                                        } else {
                                                                cov.Count[14] = 1
                                                                if i == 7 {
                                                                        cov.Count[15] = 1
                                                                } else {
                                                                        cov.Count[16] = 1
                                                                        if i == 8 {
                                                                                cov.Count[17] = 1
                                                                        } else {
                                                                                cov.Count[18] = 1
...
@minux
Copy link
Member

@minux minux commented May 26, 2015

It can be reproduced by Go 1.4, where we're still using
bison generated parser, and the parser stack gets overflowed
by the heavily nested if statements.

It has been fixed by the Go rewrite. I've tested a much more
extreme case (up to 10k), and although go test -cover uses
about 1GB memory, it still works fine.

@minux minux closed this May 26, 2015
@mikioh mikioh changed the title Limit on number of consecutive if..then..else statements in "go test -cover" cmd/cover: Limit on number of consecutive if..then..else statements in "go test -cover" May 26, 2015
@aresetian
Copy link

@aresetian aresetian commented Jun 16, 2015

When I execute "go test -coverprofile=c.out" with the version "go version go1.4 darwin/amd64" I can reproduced the error. I solved reducing the number of IFs like JoshVarga said.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.