]> Cypherpunks repositories - gostls13.git/commitdiff
context: handle nil values for valueCtx.String()
authorSean Liao <sean@liao.dev>
Tue, 9 Jul 2024 19:24:34 +0000 (20:24 +0100)
committerGopher Robot <gobot@golang.org>
Wed, 10 Jul 2024 02:44:20 +0000 (02:44 +0000)
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>

src/context/context.go
src/context/x_test.go

index 30adfe987d53cf2f08be331a18d4f4d399f78df3..763d4f777ffb867379fd88699c54a4b7285ec63a 100644 (file)
@@ -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()
 }
index 2c66ed42b2f1510142d4ca845e3717e57848f119..ab3c2757cf5834de0f9b78c502af9f55222dbe30 100644 (file)
@@ -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", "", "", "")