From 70e453b4361b80a85e6ebb37e4d43ec02db9a50a Mon Sep 17 00:00:00 2001 From: Sean Liao Date: Tue, 9 Jul 2024 20:24:34 +0100 Subject: [PATCH] context: handle nil values for valueCtx.String() Fixes #68356 Change-Id: I57dc089a99f545e29a6759a8db5e28fabb6d1a61 Reviewed-on: https://go-review.googlesource.com/c/go/+/597415 Commit-Queue: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Reviewed-by: Ian Lance Taylor Reviewed-by: Joseph Tsai Reviewed-by: Damien Neil Auto-Submit: Ian Lance Taylor --- src/context/context.go | 2 ++ src/context/x_test.go | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/context/context.go b/src/context/context.go index 30adfe987d..763d4f777f 100644 --- a/src/context/context.go +++ b/src/context/context.go @@ -739,6 +739,8 @@ func stringify(v any) string { return s.String() case string: return s + case nil: + return "" } return reflectlite.TypeOf(v).String() } diff --git a/src/context/x_test.go b/src/context/x_test.go index 2c66ed42b2..ab3c2757cf 100644 --- a/src/context/x_test.go +++ b/src/context/x_test.go @@ -243,6 +243,10 @@ func TestValues(t *testing.T) { c4 := WithValue(c3, k1, nil) check(c4, "c4", "", "c2k2", "c3k3") + if got, want := fmt.Sprint(c4), `context.Background.WithValue(context_test.key1, c1k1).WithValue(context_test.key2(1), c2k2).WithValue(context_test.key2(3), c3k3).WithValue(context_test.key1, )`; got != want { + t.Errorf("c.String() = %q want %q", got, want) + } + o0 := otherContext{Background()} check(o0, "o0", "", "", "") -- 2.48.1