1
0
mirror of https://github.com/golang/go synced 2024-11-23 04:00:03 -07:00

fmt: one bad index shouldn't spoil them all

In an indexed verb such as %[3]d, if the index is out of range, don't
skip processing the rest of the verbs. The bug was that the bad
index set a bit for the whole format instead of just the verb.

Ok for 1.2 because this is a bug in a 1.2 feature.

Fixes #6434

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/13632058
This commit is contained in:
Rob Pike 2013-09-23 16:03:57 +10:00
parent d00fb0a1ea
commit 3e3ed5d978
2 changed files with 5 additions and 3 deletions

View File

@ -589,6 +589,8 @@ var reorderTests = []struct {
{"%3.[2]d", SE{7}, "%!d(BADINDEX)"},
{"%.[2]d", SE{7}, "%!d(BADINDEX)"},
{"%d %d %d %#[1]o %#o %#o %#o", SE{11, 12, 13}, "11 12 13 013 014 015 %!o(MISSING)"},
{"%[5]d %[2]d %d", SE{1, 2, 3}, "%!d(BADINDEX) 2 3"},
{"%d %[3]d %d", SE{1, 2}, "1 %!d(BADINDEX) 2"}, // Erroneous index does not affect sequence.
}
func TestReorder(t *testing.T) {

View File

@ -118,7 +118,7 @@ type pp struct {
value reflect.Value
// reordered records whether the format string used argument reordering.
reordered bool
// goodArgNum records whether all reordering directives were valid.
// goodArgNum records whether the most recent reordering directive was valid.
goodArgNum bool
runeBuf [utf8.UTFMax]byte
fmt fmt
@ -1036,7 +1036,7 @@ func intFromArg(a []interface{}, argNum int) (num int, isInt bool, newArgNum int
// up to the closing paren, if present, and whether the number parsed
// ok. The bytes to consume will be 1 if no closing paren is present.
func parseArgNumber(format string) (index int, wid int, ok bool) {
// Find closing parenthesis
// Find closing bracket.
for i := 1; i < len(format); i++ {
if format[i] == ']' {
width, ok, newi := parsenum(format, 1, i)
@ -1070,8 +1070,8 @@ func (p *pp) doPrintf(format string, a []interface{}) {
argNum := 0 // we process one argument per non-trivial format
afterIndex := false // previous item in format was an index like [3].
p.reordered = false
p.goodArgNum = true
for i := 0; i < end; {
p.goodArgNum = true
lasti := i
for i < end && format[i] != '%' {
i++