aboutsummaryrefslogtreecommitdiff
path: root/src/context
diff options
context:
space:
mode:
authorSean Liao <sean@liao.dev>2024-07-09 20:24:34 +0100
committerGopher Robot <gobot@golang.org>2024-07-10 02:44:20 +0000
commit70e453b4361b80a85e6ebb37e4d43ec02db9a50a (patch)
tree438f60c247049ec48cf57ff06686f11dd15a2595 /src/context
parent183a40db6def2780478053b3cd14f3f5c355d999 (diff)
downloadgo-70e453b4361b80a85e6ebb37e4d43ec02db9a50a.tar.xz
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 <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Damien Neil <dneil@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/context')
-rw-r--r--src/context/context.go2
-rw-r--r--src/context/x_test.go4
2 files changed, 6 insertions, 0 deletions
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 "<nil>"
}
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, <nil>)`; got != want {
+ t.Errorf("c.String() = %q want %q", got, want)
+ }
+
o0 := otherContext{Background()}
check(o0, "o0", "", "", "")