Skip to content

Commit

Permalink
fmt: prevent panic from %.[]
Browse files Browse the repository at this point in the history
Fixes #10675

Change-Id: Ia057427ce3e81d35f1ba6c354868a0ad6cc9abf2
Reviewed-on: https://go-review.googlesource.com/9636
Reviewed-by: Rob Pike <[email protected]>
  • Loading branch information
Mistobaan authored and robpike committed May 5, 2015
1 parent e8c0d0f commit 325642e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/fmt/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ var reorderTests = []struct {
{"%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.
{"%.[]", SE{}, "%!](BADINDEX)"}, // Issue 10675
}

func TestReorder(t *testing.T) {
Expand Down
9 changes: 7 additions & 2 deletions src/fmt/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,11 @@ 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) {
// There must be at least 3 bytes: [n].
if len(format) < 3 {
return 0, 1, false
}

// Find closing bracket.
for i := 1; i < len(format); i++ {
if format[i] == ']' {
Expand All @@ -1062,7 +1067,7 @@ func (p *pp) argNumber(argNum int, format string, i int, numArgs int) (newArgNum
return index, i + wid, true
}
p.goodArgNum = false
return argNum, i + wid, true
return argNum, i + wid, ok
}

func (p *pp) doPrintf(format string, a []interface{}) {
Expand Down Expand Up @@ -1132,7 +1137,7 @@ func (p *pp) doPrintf(format string, a []interface{}) {
p.goodArgNum = false
}
argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
if format[i] == '*' {
if i < end && format[i] == '*' {
i++
p.fmt.prec, p.fmt.precPresent, argNum = intFromArg(a, argNum)
if !p.fmt.precPresent {
Expand Down

0 comments on commit 325642e

Please sign in to comment.