]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: one bad index shouldn't spoil them all
authorRob Pike <r@golang.org>
Mon, 23 Sep 2013 06:03:57 +0000 (16:03 +1000)
committerRob Pike <r@golang.org>
Mon, 23 Sep 2013 06:03:57 +0000 (16:03 +1000)
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

src/pkg/fmt/fmt_test.go
src/pkg/fmt/print.go

index f32c05dc062b2aa3fa4ff743b60cdee1dfb4ab60..bf50675f54359ff12d05179d15c8e15077aba98f 100644 (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) {
index 14cda03b9e5d0edc9b4291ac3b44c7d587ff719d..1ea816d6d5f26c37a2c360e40845f298d497ed6e 100644 (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++