Commit 92715d77 authored by Rob Pike's avatar Rob Pike

fmt: change the overflow test for large numbers in verbs

The old one was inferior.

Fixes #10695.

Change-Id: Ia7fb88c9ceb1b10197b77a54f729865385288d98
Reviewed-on: https://go-review.googlesource.com/9709Reviewed-by: default avatarDmitry Vyukov <dvyukov@google.com>
parent 64c39a30
......@@ -538,6 +538,7 @@ var fmtTests = []struct {
{"%T", nil, "<nil>"},
{"%-1", 100, "%!(NOVERB)%!(EXTRA int=100)"},
{"%017091901790959340919092959340919017929593813360", 0, "%!(NOVERB)%!(EXTRA int=0)"},
{"%184467440737095516170v", 0, "%!(NOVERB)%!(EXTRA int=0)"},
// The "<nil>" show up because maps are printed by
// first obtaining a list of keys and then looking up
......
......@@ -291,10 +291,12 @@ func parsenum(s string, start, end int) (num int, isnum bool, newi int) {
return 0, false, end
}
for newi = start; newi < end && '0' <= s[newi] && s[newi] <= '9'; newi++ {
num = num*10 + int(s[newi]-'0')
if num < 0 {
const maxInt32 = 1<<31 - 1 // 31 bits is plenty for a width.
max := maxInt32/10 - 1
if num > max {
return 0, false, end // Overflow; crazy long number most likely.
}
num = num*10 + int(s[newi]-'0')
isnum = true
}
return
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment