From: Russ Cox Date: Tue, 15 Mar 2022 17:36:10 +0000 (-0400) Subject: reflect: avoid panic in reflect.Kind.String for negative Kind X-Git-Tag: go1.19beta1~1060 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=0d71234ee4cfcac4a6664d8fef4be575cca1d7c7;p=gostls13.git reflect: avoid panic in reflect.Kind.String for negative Kind Kind(-1).String() used to panic; let's not. Change-Id: I1dfc0e3298beb37d77713d8327579bbde90dd156 Reviewed-on: https://go-review.googlesource.com/c/go/+/393015 Trust: Russ Cox Reviewed-by: Ian Lance Taylor --- diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go index 5364166eab..06026232ee 100644 --- a/src/reflect/all_test.go +++ b/src/reflect/all_test.go @@ -7807,3 +7807,12 @@ func TestIssue50208(t *testing.T) { t.Errorf("name of type parameter mismatched, want:%s, got:%s", want2, got) } } + +func TestNegativeKindString(t *testing.T) { + x := -1 + s := Kind(x).String() + want := "kind-1" + if s != want { + t.Fatalf("Kind(-1).String() = %q, want %q", s, want) + } +} diff --git a/src/reflect/type.go b/src/reflect/type.go index 8ba63bcad0..83047062bd 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -632,8 +632,8 @@ const ( // String returns the name of k. func (k Kind) String() string { - if int(k) < len(kindNames) { - return kindNames[k] + if uint(k) < uint(len(kindNames)) { + return kindNames[uint(k)] } return "kind" + strconv.Itoa(int(k)) }