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

log/slog: nil pointer dereference when printing nil regexp #61648

Closed
romaindoumenc opened this issue Jul 29, 2023 · 4 comments
Closed

log/slog: nil pointer dereference when printing nil regexp #61648

romaindoumenc opened this issue Jul 29, 2023 · 4 comments
Assignees
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@romaindoumenc
Copy link

romaindoumenc commented Jul 29, 2023

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

$ go1.21rc3 linux/amd64

Does this issue reproduce with the latest release?

Yes – this is indeed a regression from using go1.20 + exp/slog

What did you do?

Log in a simple nil value using slog
https://go.dev/play/p/jAg4xDbs4sn?v=gotip

What did you expect to see?

A formatted nil line, as per https://go.dev/play/p/b7Vjf1L09sC

2009/11/10 23:00:00 INFO priting re=<nil>

What did you see instead?

Crash, with nil pointer deref

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x4afbee]

goroutine 1 [running]:
regexp.(*Regexp).String(...)
	/usr/local/go-faketime/src/regexp/regexp.go:110
regexp.(*Regexp).MarshalText(0x0?)
	/usr/local/go-faketime/src/regexp/regexp.go:1294 +0xe
log/slog.appendTextValue(0xc000051a60, {{}, 0x4cf000?, {0x4cf000?, 0x0?}})
	/usr/local/go-faketime/src/log/slog/text_handler.go:106 +0x28e
log/slog.(*handleState).appendValue(0xc000051a60, {{}, 0x4d2164?, {0x4cf000?, 0x0?}})
	/usr/local/go-faketime/src/log/slog/handler.go:519 +0x31
log/slog.(*handleState).appendAttr(0xc000051a60, {{0x4d2164, 0x2}, {{}, 0x0, {0x4cf000, 0x0}}})
	/usr/local/go-faketime/src/log/slog/handler.go:475 +0x7ea
log/slog.(*handleState).appendNonBuiltIns.func1({{0x4d2164, 0x2}, {{}, 0x0, {0x4cf000, 0x0}}})
	/usr/local/go-faketime/src/log/slog/handler.go:326 +0x49
log/slog.Record.Attrs({{0xbab699fc00000000, 0x1, 0x586660}, {0x4d2586, 0x7}, 0x0, 0x4afd1c, {{{0x4d2164, 0x2}, {{...}, ...}}, ...}, ...}, ...)
	/usr/local/go-faketime/src/log/slog/record.go:84 +0xc4
log/slog.(*handleState).appendNonBuiltIns(_, {{0xbab699fc00000000, 0x1, 0x586660}, {0x4d2586, 0x7}, 0x0, 0x4afd1c, {{{0x4d2164, 0x2}, ...}, ...}, ...})
	/usr/local/go-faketime/src/log/slog/handler.go:325 +0x465
log/slog.(*defaultHandler).Handle(_, {_, _}, {{0xbab699fc00000000, 0x1, 0x586660}, {0x4d2586, 0x7}, 0x0, 0x4afd1c, ...})
	/usr/local/go-faketime/src/log/slog/handler.go:115 +0x385
log/slog.(*Logger).log(0xc000014090, {0x4f75f0, 0x5b3f80}, 0x0, {0x4d2586, 0x7}, {0xc00007af10, 0x2, 0x2})
	/usr/local/go-faketime/src/log/slog/logger.go:225 +0x1e4
log/slog.Info({0x4d2586?, 0xc00003e730?}, {0xc00003e710?, 0x60?, 0x0?})
	/usr/local/go-faketime/src/log/slog/logger.go:260 +0x91
main.main()
	/tmp/sandbox2138239418/prog.go:10 +0x5c

Program exited.
@romaindoumenc
Copy link
Author

I’m assuming the crash is linked to the new regexp implementation of MarshalText:

+
+// MarshalText implements [encoding.TextMarshaler]. The output
+// matches that of calling the [Regexp.String] method.
+//
+// Note that the output is lossy in some cases: This method does not indicate
+// POSIX regular expressions (i.e. those compiled by calling [CompilePOSIX]), or
+// those for which the [Regexp.Longest] method has been called.
+func (re *Regexp) MarshalText() ([]byte, error) {
+       return []byte(re.String()), nil
+}
+
+// UnmarshalText implements [encoding.TextUnmarshaler] by calling
+// [Compile] on the encoded value.
+func (re *Regexp) UnmarshalText(text []byte) error {
+       newRE, err := Compile(string(text))
+       if err != nil {
+               return err
+       }
+       *re = *newRE
+       return nil
+}

Logging is a place where we really want to have nil values printed (indeed, this can be valuable for program debugging), so my sense is that log/slog should be robust against nil values.

@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 29, 2023
@seankhliao
Copy link
Member

cc @jba

@jba
Copy link
Contributor

jba commented Jul 29, 2023

Agreed, slog should catch panics when formatting, like the fmt functions do.

I changed the title of this issue to reflect that.

@jba jba self-assigned this Jul 29, 2023
@jba jba added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jul 29, 2023
@gopherbot
Copy link

Change https://go.dev/cl/514135 mentions this issue: log/slog: print nil values instead of panic

@panjf2000 panjf2000 added this to the Backlog milestone Jul 30, 2023
romaindoumenc pushed a commit to TroutSoftware/go that referenced this issue Aug 9, 2023
Fixes golang#61648

Change-Id: I6b7f4948ca89142a358d74624607daf42ea8b304
Reviewed-on: https://go-review.googlesource.com/c/go/+/514135
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Andy Pan <panjf2000@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
searKing added a commit to searKing/golang that referenced this issue Aug 17, 2023
eric pushed a commit to fancybits/go that referenced this issue Sep 7, 2023
Fixes golang#61648

Change-Id: I6b7f4948ca89142a358d74624607daf42ea8b304
Reviewed-on: https://go-review.googlesource.com/c/go/+/514135
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Andy Pan <panjf2000@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
eric pushed a commit to fancybits/go that referenced this issue Sep 7, 2023
Fixes golang#61648

Change-Id: I6b7f4948ca89142a358d74624607daf42ea8b304
Reviewed-on: https://go-review.googlesource.com/c/go/+/514135
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Andy Pan <panjf2000@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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