Skip to content

Commit

Permalink
fmt: change the overflow test for large numbers in verbs
Browse files Browse the repository at this point in the history
The old one was inferior.

Fixes #10695.

Change-Id: Ia7fb88c9ceb1b10197b77a54f729865385288d98
Reviewed-on: https://go-review.googlesource.com/9709
Reviewed-by: Dmitry Vyukov <[email protected]>
  • Loading branch information
robpike committed May 5, 2015
1 parent 64c39a3 commit 92715d7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/fmt/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/fmt/print.go
Original file line number Diff line number Diff line change
Expand 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
Expand Down

0 comments on commit 92715d7

Please sign in to comment.