diff options
author | Rob Pike <r@golang.org> | 2013-09-23 16:03:57 +1000 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2013-09-23 16:03:57 +1000 |
commit | 55539f279f78198504e33201332d8327ab9ef9af (patch) | |
tree | 481c09926c5a6ae392eab802c88cdade708125ff | |
parent | ef4ec087bada13f40f1310101dc989d04fd77de8 (diff) | |
download | go-55539f279f78198504e33201332d8327ab9ef9af.tar.gz |
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 issue 6434
R=golang-dev, adg
CC=golang-dev
https://codereview.appspot.com/13632058
-rw-r--r-- | src/pkg/fmt/fmt_test.go | 2 | ||||
-rw-r--r-- | src/pkg/fmt/print.go | 6 |
2 files changed, 5 insertions, 3 deletions
diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go index f32c05dc0..bf50675f5 100644 --- a/src/pkg/fmt/fmt_test.go +++ b/src/pkg/fmt/fmt_test.go @@ -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) { diff --git a/src/pkg/fmt/print.go b/src/pkg/fmt/print.go index 14cda03b9..1ea816d6d 100644 --- a/src/pkg/fmt/print.go +++ b/src/pkg/fmt/print.go @@ -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++ |