From f0ff6d4a67ec9a956aa655d487543da034cf576b Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 25 Nov 2020 13:47:42 -0800 Subject: [PATCH] reflect: fix Value.Convert for int-to-string conversions (regression) The bug was introduced by https://golang.org/cl/220844. Updates #42792. Fixes #42835. Change-Id: I03065c7526488aded35ef2f800b7162e1606877a Reviewed-on: https://go-review.googlesource.com/c/go/+/273326 Trust: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor --- src/reflect/all_test.go | 3 +++ src/reflect/value.go | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go index a12712d2540..b01158635f1 100644 --- a/src/reflect/all_test.go +++ b/src/reflect/all_test.go @@ -4007,9 +4007,12 @@ var convertTests = []struct { {V(int16(-3)), V(string("\uFFFD"))}, {V(int32(-4)), V(string("\uFFFD"))}, {V(int64(-5)), V(string("\uFFFD"))}, + {V(int64(-1 << 32)), V(string("\uFFFD"))}, + {V(int64(1 << 32)), V(string("\uFFFD"))}, {V(uint(0x110001)), V(string("\uFFFD"))}, {V(uint32(0x110002)), V(string("\uFFFD"))}, {V(uint64(0x110003)), V(string("\uFFFD"))}, + {V(uint64(1 << 32)), V(string("\uFFFD"))}, {V(uintptr(0x110004)), V(string("\uFFFD"))}, // named string diff --git a/src/reflect/value.go b/src/reflect/value.go index bf926a7453b..1f185b52e49 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -2681,12 +2681,20 @@ func cvtComplex(v Value, t Type) Value { // convertOp: intXX -> string func cvtIntString(v Value, t Type) Value { - return makeString(v.flag.ro(), string(rune(v.Int())), t) + s := "\uFFFD" + if x := v.Int(); int64(rune(x)) == x { + s = string(rune(x)) + } + return makeString(v.flag.ro(), s, t) } // convertOp: uintXX -> string func cvtUintString(v Value, t Type) Value { - return makeString(v.flag.ro(), string(rune(v.Uint())), t) + s := "\uFFFD" + if x := v.Uint(); uint64(rune(x)) == x { + s = string(rune(x)) + } + return makeString(v.flag.ro(), s, t) } // convertOp: []byte -> string