1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:14:45 -07:00

fmt: fix panic with large precision

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>
This commit is contained in:
Rob Pike 2015-05-07 11:22:43 -07:00
parent e45aebd6dd
commit c6688b7b1f
2 changed files with 15 additions and 2 deletions

View 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:

View 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)