Skip to content

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

Closed as not planned

Description

@Eiwinda

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%

Activity

  1. 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[/+] on Aug 24, 2023
  2. changed the title [-]testing/cover: Coverage calculation is wrong when runtime error occurs[/-] [+]testing: coverage calculation is wrong when runtime error occurs[/+] on Aug 24, 2023
  3. cagedmantis commented on Aug 24, 2023

    @cagedmantis
    Contributor

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

  4. added
    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
    on Aug 24, 2023
  5. Eiwinda commented on Aug 25, 2023

    @Eiwinda
    Author

    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

  6. robpike commented on Aug 25, 2023

    @robpike
    Contributor

    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.

  7. removed
    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
    on Oct 7, 2023
  8. locked and limited conversation to collaborators on Oct 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions