Skip to content

Panic "slice bounds out of range" when using a helper function with log/slog #108

@kristoffer-ingemansson

Description

@kristoffer-ingemansson

I'm getting an unexpected panic when executing loggercheck via golangci-lint.

$ golangci-lint run
ERRO [runner] Panic: loggercheck: package "main" (isInitialPkg: true, needAnalyzeSource: true): runtime error: slice bounds out of range [2:1]: goroutine 2179 [running]:
runtime/debug.Stack()
        runtime/debug/stack.go:26 +0x5e
github.com/golangci/golangci-lint/v2/pkg/goanalysis.(*action).analyzeSafe.func1()
        github.com/golangci/golangci-lint/v2/pkg/goanalysis/runner_action.go:55 +0x257
panic({0x159ed80?, 0xc0001347e0?})
        runtime/panic.go:792 +0x132
github.com/timonwong/loggercheck/internal/checkers.ExecuteChecker({0x1b7ce20, 0x281f100}, 0xc001e60380, {0xc000552980?, 0xc0024bca20?, 0xc0005a9340?}, {0x8?, 0x0?})
        github.com/timonwong/[email protected]/internal/checkers/checker.go:37 +0x36f
github.com/timonwong/loggercheck.(*loggercheck).checkLoggerArguments(0xc000ee32d0, 0xc001e60380, 0xc000552980)
        github.com/timonwong/[email protected]/loggercheck.go:138 +0xb6
github.com/timonwong/loggercheck.(*loggercheck).run.func1({0x1b757b8?, 0xc000552980})
        github.com/timonwong/[email protected]/loggercheck.go:201 +0x76
golang.org/x/tools/go/ast/inspector.(*Inspector).Preorder(0xc00090f2d8, {0xc0004bcb58?, 0xc000bf1590?, 0x40c06b?}, 0xc001321b40)
        golang.org/x/[email protected]/go/ast/inspector/inspector.go:122 +0x91
github.com/timonwong/loggercheck.(*loggercheck).run(0xc000ee32d0, 0xc001e60380)
        github.com/timonwong/[email protected]/loggercheck.go:192 +0xac
github.com/golangci/golangci-lint/v2/pkg/goanalysis.(*action).analyze.func3(...)
        github.com/golangci/golangci-lint/v2/pkg/goanalysis/runner_checker.go:182
github.com/golangci/golangci-lint/v2/pkg/goanalysis.(*action).analyze(0xc001a0d7e0)
        github.com/golangci/golangci-lint/v2/pkg/goanalysis/runner_checker.go:208 +0xb62
github.com/golangci/golangci-lint/v2/pkg/timeutils.(*Stopwatch).TrackStage(0xc003682960, {0x1676c75, 0xb}, 0xc001d92710)
        github.com/golangci/golangci-lint/v2/pkg/timeutils/stopwatch.go:111 +0x44
github.com/golangci/golangci-lint/v2/pkg/goanalysis.(*action).analyzeSafe(0xc001a0d7e0?)
        github.com/golangci/golangci-lint/v2/pkg/goanalysis/runner_action.go:59 +0x71
github.com/golangci/golangci-lint/v2/pkg/goanalysis.(*loadingPackage).analyze.func2()
        github.com/golangci/golangci-lint/v2/pkg/goanalysis/runner_loadingpackage.go:106 +0x65
golang.org/x/sync/errgroup.(*Group).Go.func1()
        golang.org/x/[email protected]/errgroup/errgroup.go:93 +0x50
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 517
        golang.org/x/[email protected]/errgroup/errgroup.go:78 +0x93
WARN [runner] Can't run linter goanalysis_metalinter: goanalysis_metalinter: loggercheck: package "main" (isInitialPkg: true, needAnalyzeSource: true): runtime error: slice bounds out of range [2:1]
ERRO Running error: can't run linter goanalysis_metalinter
goanalysis_metalinter: loggercheck: package "main" (isInitialPkg: true, needAnalyzeSource: true): runtime error: slice bounds out of range [2:1]

Here is the minimum required to replicate:
main.go

package main

import (
	"context"
	"log/slog"
)

func main() {
	slog.ErrorContext(fn())
}

func fn() (context.Context, string) {
	return nil, "test"
}

.golangci.yaml

version: "2"
linters:
  default: all

My environment:

$ golangci-lint --version
golangci-lint has version 2.3.0 built with go1.24.4 from 364a4bbe on 2025-07-21T14:49:16Z

Same issue also exists in sloglint.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions