Skip to content

Nil pointer dereference in github.go:64 with Go 1.25 #266

@claytono

Description

@claytono

Description

The Store method in pkg/testcoverage/badgestorer/github.go has a nil pointer dereference on line 64 when building with Go 1.25:

fc, _, httpResp, err := client.Repositories.GetContents(...)
if httpResp.StatusCode == http.StatusNotFound { // <-- BUG: httpResp can be nil when err != nil
    return updateBadge(nil)
}

When the GitHub API call fails (e.g., invalid token), httpResp is nil, and accessing .StatusCode before checking err causes a panic.

Go 1.25 Behavior Change

Go 1.25 fixed a spec violation where the compiler sometimes delayed nil pointer checks. This masked the bug in earlier Go versions. See: https://go.dev/doc/go1.25#nil-checks

How to Reproduce

# Build with Go 1.25 and run tests
go test ./pkg/testcoverage/badgestorer/...

The Test_Github_Error test triggers this because it uses an invalid token (🔑), causing the API call to fail.

Stack Trace

--- FAIL: Test_Github_Error (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered, repanicked]
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x8ceba5]

github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/badgestorer.(*githubStorer).Store(0xc000071f10, {0xc000014270, 0xa, 0xa})
    /build/source/pkg/testcoverage/badgestorer/github.go:64 +0x1a5

Suggested Fix

Check err before accessing httpResp:

fc, _, httpResp, err := client.Repositories.GetContents(...)
if err != nil {
    // Check for 404 via error type, or return error
    var ghErr *github.ErrorResponse
    if errors.As(err, &ghErr) && ghErr.Response != nil && ghErr.Response.StatusCode == http.StatusNotFound {
        return updateBadge(nil)
    }
    return false, fmt.Errorf("get badge content: %w", err)
}

// Now safe to access httpResp
if httpResp != nil && httpResp.StatusCode == http.StatusNotFound {
    return updateBadge(nil)
}

Environment

  • Go version: 1.25.5
  • go-test-coverage version: v2.18.3
  • OS: Linux (GitHub Actions / Nix)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions