]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: change the overflow test for large numbers in verbs
authorRob Pike <r@golang.org>
Tue, 5 May 2015 15:59:18 +0000 (08:59 -0700)
committerRob Pike <r@golang.org>
Tue, 5 May 2015 18:17:59 +0000 (18:17 +0000)
The old one was inferior.

Fixes #10695.

Change-Id: Ia7fb88c9ceb1b10197b77a54f729865385288d98
Reviewed-on: https://go-review.googlesource.com/9709
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
src/fmt/fmt_test.go
src/fmt/print.go

index 5d3357004f85da8894efe3b1c707cbd9c7b79192..90112bb2a3d93ee0631868695a80cf6f42ca7474 100644 (file)
@@ -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
index 9c373145dde76ef1c26d84acea2a342e589d33e0..1d8db0aac41dcb5a29f67475b897508671c7193f 100644 (file)
@@ -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