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

x/exp/slog: TextHandler panics on nil arg #58652

Closed
suiriass opened this issue Feb 23, 2023 · 3 comments
Closed

x/exp/slog: TextHandler panics on nil arg #58652

suiriass opened this issue Feb 23, 2023 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@suiriass
Copy link

suiriass commented Feb 23, 2023

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

$ go version
go version go1.19.2 darwin/amd64

Does this issue reproduce with the latest release?

yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/shimingjun/Library/Caches/go-build"
GOENV="/Users/shimingjun/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE="xxxxxx"
GOMODCACHE="/Users/shimingjun/go/pkg/mod"
GONOPROXY="xxxxxx"
GONOSUMDB="xxxx"
GOOS="darwin"
GOPATH="/Users/shimingjun/go"
GOPRIVATE="xxxxxxx"
GOPROXY="xxxxxxx"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.19.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/5z/6g3r5wdj70d88pljjvlhk0f80000gn/T/go-build81508862=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

import (
	"golang.org/x/exp/slog"
	"os"
	"testing"
)

func TestTextHandler(t *testing.T) {
	handler := slog.New(slog.NewJSONHandler(os.Stderr))
	var err error // err is nil
	handler.Info("json msg1", "key", "str", "err", err)
	handler.Info("json msg2", "key", "str", slog.Any("err", err))

	handler = slog.New(slog.NewTextHandler(os.Stderr))
	handler.Info("text msg1", "key", "str", "err", err)
	handler.Info("text msg2", "key", "str", slog.Any("err", err))
}


What did you expect to see?

When using attr to output nil, the text handler gets panic, while the json handler does not, expecting the correct null output

What did you see instead?

=== RUN TestTextHandler
{"time":"2023-02-23T10:51:38.906392+08:00","level":"INFO","msg":"json msg1","key":"str","err":null}
{"time":"2023-02-23T10:51:38.906612+08:00","level":"INFO","msg":"json msg2","key":"str","err":null}
--- FAIL: TestTextHandler (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x98 pc=0x11539a0]

goroutine 4 [running]:
testing.tRunner.func1.2({0x11725e0, 0x12c28e0})
/usr/local/go/src/testing/testing.go:1396 +0x24e
testing.tRunner.func1()
/usr/local/go/src/testing/testing.go:1399 +0x39f
panic({0x11725e0, 0x12c28e0})
/usr/local/go/src/runtime/panic.go:884 +0x212
golang.org/x/exp/slog.byteSlice({0x0?, 0x0})
/Users/gowork/gx/vendor/golang.org/x/exp/slog/text_handler.go:130 +0x80
golang.org/x/exp/slog.appendTextValue(0xc000135708, {0x5?, {0x0?, 0x0?}})
/Users/gowork/gx/vendor/golang.org/x/exp/slog/text_handler.go:109 +0x305
golang.org/x/exp/slog.(*handleState).appendValue(0xc000135708, {0x1196f12?, {0x0?, 0x0?}})
/Users/gowork/gx/vendor/golang.org/x/exp/slog/handler.go:532 +0x37
golang.org/x/exp/slog.(*handleState).appendAttr(0xc000135708, {{0x1196f12, 0x3}, {0x0, {0x0, 0x0}}})
/Users/gowork/gx/vendor/golang.org/x/exp/slog/handler.go:468 +0x725
golang.org/x/exp/slog.(*handleState).appendNonBuiltIns.func1({{0x1196f12, 0x3}, {0x0, {0x0, 0x0}}})
/Users/gowork/gx/vendor/golang.org/x/exp/slog/handler.go:327 +0x4f
golang.org/x/exp/slog.Record.Attrs({{0xc0f5d1d36f9de160, 0xf6a35, 0x12cc380}, {0x1197c45, 0x8}, 0x0, {0x0, 0x0}, 0x1158807, {{{0x1196f2d, ...}, ...}, ...}, ...}, ...)
/Users/gowork/gx/vendor/golang.org/x/exp/slog/record.go:100 +0x9e
golang.org/x/exp/slog.(*handleState).appendNonBuiltIns(, {{0xc0f5d1d36f9de160, 0xf6a35, 0x12cc380}, {0x1197c45, 0x8}, 0x0, {0x0, 0x0}, 0x1158807, ...})
/Users/gowork/gx/vendor/golang.org/x/exp/slog/handler.go:326 +0x465
golang.org/x/exp/slog.(*commonHandler).handle(
, {{0xc0f5d1d36f9de160, 0xf6a35, 0x12cc380}, {0x1197c45, 0x8}, 0x0, {0x0, 0x0}, 0x1158807, ...})
/Users/gowork/gx/vendor/golang.org/x/exp/slog/handler.go:304 +0x8c8
golang.org/x/exp/slog.(*TextHandler).Handle(_, {{0xc0f5d1d36f9de160, 0xf6a35, 0x12cc380}, {0x1197c45, 0x8}, 0x0, {0x0, 0x0}, 0x1158807, ...})
/Users/gowork/gx/vendor/golang.org/x/exp/slog/text_handler.go:90 +0x58
golang.org/x/exp/slog.(*Logger).logPC(0xc000135dc8, {0x0, 0x0}, 0x1158807, 0x0, {0x1197c45, 0x8}, {0xc000135ee0, 0x4, 0x4})
/Users/gowork/gx/vendor/golang.org/x/exp/slog/logger.go:178 +0x255
golang.org/x/exp/slog.(*Logger).LogDepth(0xc000135dc8, 0x1, 0x100d9ff?, {0x1197c45, 0x8}, {0xc000135ee0, 0x4, 0x4})
/Users/gowork/gx/vendor/golang.org/x/exp/slog/pc.go:26 +0xe8
golang.org/x/exp/slog.(*Logger).Info(...)
/Users/gowork/gx/vendor/golang.org/x/exp/slog/logger.go:193
git.woa.com/sase/gx/glibs/slogx.TestTextHandler(0x0?)
/Users/gowork/gx/glibs/slogx/slog_core_test.go:47 +0x42d
testing.tRunner(0xc0001501a0, 0x11ba030)
/usr/local/go/src/testing/testing.go:1446 +0x10b
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:1493 +0x35f
Process finished with the exit code 1

@seankhliao seankhliao changed the title exp/slog: When using attr to output nil, the text handler gets panic. x/exp/slog: When using attr to output nil, the text handler gets panic. Feb 23, 2023
@gopherbot gopherbot added this to the Unreleased milestone Feb 23, 2023
@seankhliao seankhliao changed the title x/exp/slog: When using attr to output nil, the text handler gets panic. x/exp/slog: TextHandler panics on nil arg Feb 23, 2023
@seankhliao
Copy link
Member

cc @jba

@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 23, 2023
@jba
Copy link
Contributor

jba commented Feb 23, 2023

@gopherbot
Copy link

Change https://go.dev/cl/470635 mentions this issue: slog: fix TextHandler panic on nil

@golang golang locked and limited conversation to collaborators Feb 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants