]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: fix panic with large precision
authorRob Pike <r@golang.org>
Thu, 7 May 2015 18:22:43 +0000 (11:22 -0700)
committerRob Pike <r@golang.org>
Thu, 7 May 2015 20:12:39 +0000 (20:12 +0000)
The code already handled high widths but not high precisions.
Also make sure it handles the harder cases of %U.

Fixes #10745.

Change-Id: Ib4d394d49a9941eeeaff866dc59d80483e312a98
Reviewed-on: https://go-review.googlesource.com/9769
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/fmt/fmt_test.go
src/fmt/format.go

index ab3ffaea6ee7d86fd39bf197d8930e3b7304eb3b..ba99cb0f6a662fe1e2acfb938defc3c6e89a20ee 100644 (file)
@@ -557,6 +557,11 @@ var fmtTests = []struct {
        {"%0.100f", 1.0, zeroFill("1.", 100, "")},
        {"%0.100f", -1.0, zeroFill("-1.", 100, "")},
 
+       // Used to panic: integer function didn't look at f.prec or f.unicode.
+       {"%#.80x", 42, "0x0000000000000000000000000000000000000000000000000000000000000000000000000000002a"},
+       {"%.80U", 42, "U+0000000000000000000000000000000000000000000000000000000000000000000000000000002A"},
+       {"%#.80U", '日', "U+000000000000000000000000000000000000000000000000000000000000000000000000000065E5 '日'"},
+
        // Comparison of padding rules with C printf.
        /*
                C program:
index 86673aba6ae183ffececcd5cbd6f06067fad35d1..099f8a5e00ace20b31e55df311d7d78839074a64 100644 (file)
@@ -163,12 +163,20 @@ func (f *fmt) integer(a int64, base uint64, signedness bool, digits string) {
        }
 
        var buf []byte = f.intbuf[0:]
-       if f.widPresent {
-               width := f.wid
+       if f.widPresent || f.precPresent {
+               width := f.wid + f.prec // Only one will be set, both are positive; this provides the maximum.
                if base == 16 && f.sharp {
                        // Also adds "0x".
                        width += 2
                }
+               if f.unicode {
+                       // Also adds "U+".
+                       width += 2
+                       if f.uniQuote {
+                               // Also adds " 'x'".
+                               width += 1 + 1 + utf8.UTFMax + 1
+                       }
+               }
                if width > nByte {
                        // We're going to need a bigger boat.
                        buf = make([]byte, width)