Skip to content

slog: TextHandler crash with nil TextMarshaller #64034

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

Closed
holiman opened this issue Nov 9, 2023 · 9 comments
Closed

slog: TextHandler crash with nil TextMarshaller #64034

holiman opened this issue Nov 9, 2023 · 9 comments
Assignees
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.

Comments

@holiman
Copy link

holiman commented Nov 9, 2023

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

$ go version

1.21 (play.golang.org)

Does this issue reproduce with the latest release?

Yes

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

triggers on play.golang.org

What did you do?

The slog package texthandler panics for certain cases of nil, specifically if the object being nil is a TextMarshaller. Other types of nil values are handled, as far as I can tell, and the json handler handles this case fine, so it seems to be that it's a bug.

Repro

package main

import (
	"log/slog"
	"os"
	"time"
)

type custom struct{}

func (c *custom) String() string {
	return "test"
}

func main() {
	logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
	logger.Info("hello", "stringer", (*custom)(nil))
	logger.Info("hello", "marshaller", (*time.Time)(nil))

	logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
	logger.Info("hello", "stringer", (*custom)(nil))
	logger.Info("hello", "marshaller", (*time.Time)(nil))
}

Output

{"time":"2009-11-10T23:00:00Z","level":"INFO","msg":"hello","stringer":null}
{"time":"2009-11-10T23:00:00Z","level":"INFO","msg":"hello","marshaller":null}
time=2009-11-10T23:00:00.000Z level=INFO msg=hello stringer=test
panic: value method time.Time.MarshalText called using nil *Time pointer

goroutine 1 [running]:
time.(*Time).MarshalText(0xc000117888?)
	<autogenerated>:1 +0x3a
log/slog.appendTextValue(0xc000117888, {{}, 0x4cdee0?, {0x4cdee0?, 0x0?}})
	/usr/local/go-faketime/src/log/slog/text_handler.go:106 +0x28e
log/slog.(*handleState).appendValue(0xc000117888, {{}, 0x4d0fff?, {0x4cdee0?, 0x0?}})
	/usr/local/go-faketime/src/log/slog/handler.go:519 +0x31

Crashing code

func appendTextValue(s *handleState, v Value) error {
	switch v.Kind() {
	case KindString:
		s.appendString(v.str())
	case KindTime:
		s.appendTime(v.time())
	case KindAny:
		if tm, ok := v.any.(encoding.TextMarshaler); ok {
			data, err := tm.MarshalText() <-- boom
@holiman
Copy link
Author

holiman commented Nov 9, 2023

It can also be noted that the two handlers handle a fmt.Stringer differently, the json not evaluating it

{"time":"2009-11-10T23:00:00Z","level":"INFO","msg":"hello","stringer":null}

And the text handler evaluating it:

time=2009-11-10T23:00:00.000Z level=INFO msg=hello stringer=test

@callthingsoff
Copy link
Contributor

https://go.dev/play/p/9XV1HnSDSUa?v=gotip

Could try dev-branch, seems work fine.

@mateusz834
Copy link
Member

This was fixed by: #61648.

@mateusz834 mateusz834 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 9, 2023
@holiman
Copy link
Author

holiman commented Nov 9, 2023 via email

@mateusz834
Copy link
Member

https://go.dev/play/p/8Z9QN5CU5yq?v=gotip

It is not fixed there.

@holiman
Copy link
Author

holiman commented Nov 9, 2023

Yeah, just saw.
https://cs.opensource.google/go/x/exp/+/master:slog/handler.go;bpv=1 -- last slog: sync with log/slog was in May 2023, fix is from August

@holiman
Copy link
Author

holiman commented Nov 10, 2023

We (go-ethereum) are trying to move over to slog, but we have a policy of supporting the two latest go-versions. Hence, we were planning to use x/exp/slog for a couple of releases, but this bug is a bit of a blocker for that to happen.

I'm not quite sure how the x/exp is managed, if it's tracked on this repository or somewhere else. I would like to issue a request for the x/exp/slog package to be updated with the fix. Apologies if this is not the correct place to raise it, cc @jba

@jba
Copy link
Contributor

jba commented Nov 10, 2023

Normally exp is unsupported, but we'll keep exp/slog working until the next release.

@jba jba reopened this Nov 10, 2023
@jba jba self-assigned this Nov 10, 2023
@jba jba added the NeedsFix The path to resolution is known, but the work has not been done. label Nov 10, 2023
@holiman
Copy link
Author

holiman commented Nov 13, 2023

Seems fixed as of v0.0.0-20231110203233-9a3e6036ecaa
Thanks!

@holiman holiman closed this as completed Nov 13, 2023
@golang golang locked and limited conversation to collaborators Nov 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants