Skip to content

context: valueCtx's String panics if value is nil #68356

Closed
@tchung1118

Description

@tchung1118

Go version

go version go1.23rc1 X:nocoverageredesign linux/amd64

Output of go env in your module/workspace:

GO111MODULE='off'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/user/.cache/go-build'
GOENV='/home/user/.config/go/env'
GOEXE=''
GOEXPERIMENT='nocoverageredesign'
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/user/go-code2/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/user/go-code2'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/user/.cache/bazel/_bazel_taiwon/a4cfce9b81ac8aa474cbfb97d6c4bc91/external/go_sdk'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/user/.cache/bazel/_bazel_taiwon/a4cfce9b81ac8aa474cbfb97d6c4bc91/external/go_sdk/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23rc1 X:nocoverageredesign'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/user/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD=''
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 -ffile-prefix-map=/tmp/go-build4074946335=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Created a context via calling context.WithValue and passed nil to the value and tried printing:

ctx := context.WithValue(context.Background(), contextKey{}, nil)
fmt.Printf("%v\n", ctx)

Playground:
https://go.dev/play/p/vtF3t_JWxIC?v=gotip

What did you see happen?

Printed the following:

%!v(PANIC=String method: runtime error: invalid memory address or nil pointer dereference)

What did you expect to see?

In 1.22, the above program prints:

context.Background.WithValue(type main.contextKey, val <not Stringer>)

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions