]> Cypherpunks.ru repositories - gostls13.git/commitdiff
reflect: avoid panic in reflect.Kind.String for negative Kind
authorRuss Cox <rsc@golang.org>
Tue, 15 Mar 2022 17:36:10 +0000 (13:36 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 15 Mar 2022 20:06:05 +0000 (20:06 +0000)
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 <rsc@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/reflect/all_test.go
src/reflect/type.go

index 5364166eab436ccf05510842898144f9ec392c41..06026232eea0fc443749b13b7e98f0ce0e4df72c 100644 (file)
@@ -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)
+       }
+}
index 8ba63bcad050074aec7de3cd1ef313cb18e55a79..83047062bdb97c1566235609e2a06e95b5aa9de5 100644 (file)
@@ -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))
 }