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

testing: coverage calculation is wrong when runtime error occurs #62258

Closed
Eiwinda opened this issue Aug 24, 2023 · 3 comments
Closed

testing: coverage calculation is wrong when runtime error occurs #62258

Eiwinda opened this issue Aug 24, 2023 · 3 comments

Comments

@Eiwinda
Copy link

Eiwinda commented Aug 24, 2023

What version of Go are you using (go version)?

$ go version
go1.20.5 linux/amd64

Does this issue reproduce with the latest release?

YES

What operating system and processor architecture are you using (go env)?

$ go env
<details><summary><code>GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jade/.cache/go-build"
GOENV="/home/jade/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/jade/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/jade/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/jade/pasercover/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3250049939=/tmp/go-build -gno-record-gcc-switches"
</code> Output</summary><br><pre>
$ go env

</pre></details>

What did you do?

Here is the code to reproduce the issue.

package main

import (
	"fmt"
)

type Node struct {
	data int
	next *Node
}

type LinkedList struct {
	head *Node
}

func (ll *LinkedList) Insert(data int) {
	newNode := &Node{data: data, next: nil}

	if ll.head == nil {
		ll.head = newNode
	} else {
		current := ll.head
		for current.next != nil {
			current = current.next
		}
		current.next = newNode
	}
}

func (ll *LinkedList) Display() {
	current := ll.head
	for current != nil {
		fmt.Printf("%d ", current.data)
		current = current.next
	}
	fmt.Println()
}
func main() {
	divideByZero()
	fmt.Println("Main function continues to execute...")
}

func divideByZero() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("Recovered from:", err)
		}
	}()
	n := Node{1, nil}
	fmt.Println("Result:")
	fmt.Println("Result:")
	fmt.Println(n.next.data)
        // lines below will be included in coverage, even n.next.data will trigger a nil error.
	fmt.Println("Result:") 
	fmt.Println("Result:")
	fmt.Println("Result:")
	fmt.Println("Result:")
	fmt.Println("Result:")
	fmt.Println("Result:")
}

What did you expect to see?

Lines below "fmt.Println(n.next.data)" will be excluded from the coverage.

What did you see instead?

parsecover.go:146: divideByZero 100.0%

@Eiwinda Eiwinda changed the title testing/cover: If an error occurs in a line of code while calculating code coverage, the code that follows this line within the same code block will still be included in the coverage calculation. testing/cover: Coverage calculation is wrong when runtime error occurs Aug 24, 2023
@cagedmantis cagedmantis changed the title testing/cover: Coverage calculation is wrong when runtime error occurs testing: coverage calculation is wrong when runtime error occurs Aug 24, 2023
@cagedmantis
Copy link
Contributor

Can you include the code for parsecover.go? Which command are you running to retrieve coverage data?

@cagedmantis cagedmantis added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Aug 24, 2023
@Eiwinda
Copy link
Author

Eiwinda commented Aug 25, 2023

Can you include the code for parsecover.go? Which command are you running to retrieve coverage data?

The code posted above is parsecover.go.
I use go build -cover -o myprogram parsecover.go
GOCOVERDIR=./hhh ./myprogram
go tool covdata textfmt -i=hhh -o profile.txt
go tool cover -func=profile.txt

@robpike
Copy link
Contributor

robpike commented Aug 25, 2023

Since coverage calculation is block-based, it assumes that all statements in a (branch-free) block will be executed. Without making the generated coverage code more fine-grained and inefficient, this would be hard to fix. It is an unusual enough case that it doesn't seem worth fixing.

@seankhliao seankhliao removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 7, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Oct 7, 2023
@golang golang locked and limited conversation to collaborators Oct 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants