Commit cf08eadf authored by Martin Möhrmann's avatar Martin Möhrmann Committed by Rob Pike

fmt: clear flags before printing extra argument errors

Do a reset of the fmt flags before printing the extra argument
error message to prevent a malformed printing of extra arguments.

Regroup tests for extra argument error strings.

Change-Id: Ifd97f5ca36f6c97ed5a380d975cf154d17997d3f
Reviewed-on: https://go-review.googlesource.com/20571
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarRob Pike <r@golang.org>
parent 790053b2
...@@ -426,10 +426,6 @@ var fmtTests = []struct { ...@@ -426,10 +426,6 @@ var fmtTests = []struct {
{"%-08g", complex(negInf, negInf), "(-Inf -Inf i)"}, {"%-08g", complex(negInf, negInf), "(-Inf -Inf i)"},
{"%-08G", complex(NaN, NaN), "(NaN +NaN i)"}, {"%-08G", complex(NaN, NaN), "(NaN +NaN i)"},
// erroneous formats
{"", 2, "%!(EXTRA int=2)"},
{"%d", "hello", "%!d(string=hello)"},
// old test/fmt_test.go // old test/fmt_test.go
{"%d", 1234, "1234"}, {"%d", 1234, "1234"},
{"%d", -1234, "-1234"}, {"%d", -1234, "-1234"},
...@@ -774,15 +770,15 @@ var fmtTests = []struct { ...@@ -774,15 +770,15 @@ var fmtTests = []struct {
{"%d", time.Time{}.Month(), "1"}, {"%d", time.Time{}.Month(), "1"},
// erroneous things // erroneous things
{"", nil, "%!(EXTRA <nil>)"},
{"", 2, "%!(EXTRA int=2)"},
{"no args", "hello", "no args%!(EXTRA string=hello)"},
{"%s %", "hello", "hello %!(NOVERB)"}, {"%s %", "hello", "hello %!(NOVERB)"},
{"%s %.2", "hello", "hello %!(NOVERB)"}, {"%s %.2", "hello", "hello %!(NOVERB)"},
{"%d", "hello", "%!d(string=hello)"},
{"no args", "hello", "no args%!(EXTRA string=hello)"},
{"%s", nil, "%!s(<nil>)"},
{"%T", nil, "<nil>"},
{"%-1", 100, "%!(NOVERB)%!(EXTRA int=100)"},
{"%017091901790959340919092959340919017929593813360", 0, "%!(NOVERB)%!(EXTRA int=0)"}, {"%017091901790959340919092959340919017929593813360", 0, "%!(NOVERB)%!(EXTRA int=0)"},
{"%184467440737095516170v", 0, "%!(NOVERB)%!(EXTRA int=0)"}, {"%184467440737095516170v", 0, "%!(NOVERB)%!(EXTRA int=0)"},
// Extra argument errors should format without flags set.
{"%010.2", "12345", "%!(NOVERB)%!(EXTRA string=12345)"},
// The "<nil>" show up because maps are printed by // The "<nil>" show up because maps are printed by
// first obtaining a list of keys and then looking up // first obtaining a list of keys and then looking up
...@@ -973,6 +969,7 @@ var fmtTests = []struct { ...@@ -973,6 +969,7 @@ var fmtTests = []struct {
{"%☠", []uint8{0}, "%!☠([]uint8=[0])"}, {"%☠", []uint8{0}, "%!☠([]uint8=[0])"},
{"%☠", [1]byte{0}, "%!☠([1]uint8=[0])"}, {"%☠", [1]byte{0}, "%!☠([1]uint8=[0])"},
{"%☠", [1]uint8{0}, "%!☠([1]uint8=[0])"}, {"%☠", [1]uint8{0}, "%!☠([1]uint8=[0])"},
{"%☠", "hello", "%!☠(string=hello)"},
{"%☠", 1.2345678, "%!☠(float64=1.2345678)"}, {"%☠", 1.2345678, "%!☠(float64=1.2345678)"},
{"%☠", float32(1.2345678), "%!☠(float32=1.2345678)"}, {"%☠", float32(1.2345678), "%!☠(float32=1.2345678)"},
{"%☠", 1.2345678 + 1.2345678i, "%!☠(complex128=(1.2345678+1.2345678i))"}, {"%☠", 1.2345678 + 1.2345678i, "%!☠(complex128=(1.2345678+1.2345678i))"},
......
...@@ -1164,8 +1164,6 @@ func (p *pp) doPrintf(format string, a []interface{}) { ...@@ -1164,8 +1164,6 @@ func (p *pp) doPrintf(format string, a []interface{}) {
p.buf.WriteString(missingString) p.buf.WriteString(missingString)
continue continue
} }
arg := a[argNum]
argNum++
if c == 'v' { if c == 'v' {
if p.fmt.sharp { if p.fmt.sharp {
...@@ -1185,23 +1183,26 @@ func (p *pp) doPrintf(format string, a []interface{}) { ...@@ -1185,23 +1183,26 @@ func (p *pp) doPrintf(format string, a []interface{}) {
p.fmt.zero = false p.fmt.zero = false
} }
p.printArg(arg, c, 0) p.printArg(a[argNum], c, 0)
argNum++
} }
// Check for extra arguments unless the call accessed the arguments // Check for extra arguments unless the call accessed the arguments
// out of order, in which case it's too expensive to detect if they've all // out of order, in which case it's too expensive to detect if they've all
// been used and arguably OK if they're not. // been used and arguably OK if they're not.
if !p.reordered && argNum < len(a) { if !p.reordered && argNum < len(a) {
p.fmt.clearflags()
p.buf.WriteString(extraString) p.buf.WriteString(extraString)
for ; argNum < len(a); argNum++ { for i, arg := range a[argNum:] {
arg := a[argNum] if i > 0 {
if arg != nil { p.buf.WriteString(commaSpaceString)
}
if arg == nil {
p.buf.WriteString(nilAngleString)
} else {
p.buf.WriteString(reflect.TypeOf(arg).String()) p.buf.WriteString(reflect.TypeOf(arg).String())
p.buf.WriteByte('=') p.buf.WriteByte('=')
} p.printArg(arg, 'v', 0)
p.printArg(arg, 'v', 0)
if argNum+1 < len(a) {
p.buf.WriteString(commaSpaceString)
} }
} }
p.buf.WriteByte(')') p.buf.WriteByte(')')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment