diff options
author | Russ Cox <rsc@golang.org> | 2022-03-15 13:36:10 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2022-03-15 20:06:05 +0000 |
commit | 0d71234ee4cfcac4a6664d8fef4be575cca1d7c7 (patch) | |
tree | 6aded4e5227664e5997c144209ac5a08292d0b47 /src | |
parent | db3045b4be5b91cd42c3387dc550c89bbc2f7fb4 (diff) | |
download | go-git-0d71234ee4cfcac4a6664d8fef4be575cca1d7c7.tar.gz |
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 <rsc@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/reflect/all_test.go | 9 | ||||
-rw-r--r-- | src/reflect/type.go | 4 |
2 files changed, 11 insertions, 2 deletions
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)) } |