mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
strings,bytes: improve Repeat panic messages
The Repeat("-", maxInt) call should produce
panic: runtime error: makeslice: len out of range
instead of
panic: strings: Repeat output length overflow
This PR is only for theory perfection.
Change-Id: If67d87b147d666fbbb7238656f2a0cb6cf1dbb5b
GitHub-Last-Rev: 29dc0cb9c9
GitHub-Pull-Request: golang/go#67068
Reviewed-on: https://go-review.googlesource.com/c/go/+/581936
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
2f5b420fb5
commit
44b54b99c9
4 changed files with 64 additions and 34 deletions
|
|
@ -583,7 +583,7 @@ func Repeat(b []byte, count int) []byte {
|
|||
if count < 0 {
|
||||
panic("bytes: negative Repeat count")
|
||||
}
|
||||
if len(b) >= maxInt/count {
|
||||
if len(b) > maxInt/count {
|
||||
panic("bytes: Repeat output length overflow")
|
||||
}
|
||||
n := len(b) * count
|
||||
|
|
|
|||
|
|
@ -1242,20 +1242,13 @@ func repeat(b []byte, count int) (err error) {
|
|||
|
||||
// See Issue golang.org/issue/16237
|
||||
func TestRepeatCatchesOverflow(t *testing.T) {
|
||||
tests := [...]struct {
|
||||
type testCase struct {
|
||||
s string
|
||||
count int
|
||||
errStr string
|
||||
}{
|
||||
0: {"--", -2147483647, "negative"},
|
||||
1: {"", int(^uint(0) >> 1), ""},
|
||||
2: {"-", 10, ""},
|
||||
3: {"gopher", 0, ""},
|
||||
4: {"-", -1, "negative"},
|
||||
5: {"--", -102, "negative"},
|
||||
6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"},
|
||||
}
|
||||
|
||||
runTestCases := func(prefix string, tests []testCase) {
|
||||
for i, tt := range tests {
|
||||
err := repeat([]byte(tt.s), tt.count)
|
||||
if tt.errStr == "" {
|
||||
|
|
@ -1266,9 +1259,31 @@ func TestRepeatCatchesOverflow(t *testing.T) {
|
|||
}
|
||||
|
||||
if err == nil || !strings.Contains(err.Error(), tt.errStr) {
|
||||
t.Errorf("#%d expected %q got %q", i, tt.errStr, err)
|
||||
t.Errorf("%s#%d got %q want %q", prefix, i, err, tt.errStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const maxInt = int(^uint(0) >> 1)
|
||||
|
||||
runTestCases("", []testCase{
|
||||
0: {"--", -2147483647, "negative"},
|
||||
1: {"", maxInt, ""},
|
||||
2: {"-", 10, ""},
|
||||
3: {"gopher", 0, ""},
|
||||
4: {"-", -1, "negative"},
|
||||
5: {"--", -102, "negative"},
|
||||
6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"},
|
||||
})
|
||||
|
||||
const is64Bit = 1<<(^uintptr(0)>>63)/2 != 0
|
||||
if !is64Bit {
|
||||
return
|
||||
}
|
||||
|
||||
runTestCases("64-bit", []testCase{
|
||||
0: {"-", maxInt, "out of range"},
|
||||
})
|
||||
}
|
||||
|
||||
func runesEqual(a, b []rune) bool {
|
||||
|
|
|
|||
|
|
@ -570,7 +570,7 @@ func Repeat(s string, count int) string {
|
|||
if count < 0 {
|
||||
panic("strings: negative Repeat count")
|
||||
}
|
||||
if len(s) >= maxInt/count {
|
||||
if len(s) > maxInt/count {
|
||||
panic("strings: Repeat output length overflow")
|
||||
}
|
||||
n := len(s) * count
|
||||
|
|
|
|||
|
|
@ -1170,20 +1170,13 @@ func repeat(s string, count int) (err error) {
|
|||
|
||||
// See Issue golang.org/issue/16237
|
||||
func TestRepeatCatchesOverflow(t *testing.T) {
|
||||
tests := [...]struct {
|
||||
type testCase struct {
|
||||
s string
|
||||
count int
|
||||
errStr string
|
||||
}{
|
||||
0: {"--", -2147483647, "negative"},
|
||||
1: {"", int(^uint(0) >> 1), ""},
|
||||
2: {"-", 10, ""},
|
||||
3: {"gopher", 0, ""},
|
||||
4: {"-", -1, "negative"},
|
||||
5: {"--", -102, "negative"},
|
||||
6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"},
|
||||
}
|
||||
|
||||
runTestCases := func(prefix string, tests []testCase) {
|
||||
for i, tt := range tests {
|
||||
err := repeat(tt.s, tt.count)
|
||||
if tt.errStr == "" {
|
||||
|
|
@ -1194,9 +1187,31 @@ func TestRepeatCatchesOverflow(t *testing.T) {
|
|||
}
|
||||
|
||||
if err == nil || !Contains(err.Error(), tt.errStr) {
|
||||
t.Errorf("#%d expected %q got %q", i, tt.errStr, err)
|
||||
t.Errorf("%s#%d got %q want %q", prefix, i, err, tt.errStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const maxInt = int(^uint(0) >> 1)
|
||||
|
||||
runTestCases("", []testCase{
|
||||
0: {"--", -2147483647, "negative"},
|
||||
1: {"", maxInt, ""},
|
||||
2: {"-", 10, ""},
|
||||
3: {"gopher", 0, ""},
|
||||
4: {"-", -1, "negative"},
|
||||
5: {"--", -102, "negative"},
|
||||
6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"},
|
||||
})
|
||||
|
||||
const is64Bit = 1<<(^uintptr(0)>>63)/2 != 0
|
||||
if !is64Bit {
|
||||
return
|
||||
}
|
||||
|
||||
runTestCases("64-bit", []testCase{
|
||||
0: {"-", maxInt, "out of range"},
|
||||
})
|
||||
}
|
||||
|
||||
func runesEqual(a, b []rune) bool {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue