Skip to content

Commit

Permalink
go/analysis/passes/printf: fix panic parsing argument index
Browse files Browse the repository at this point in the history
Fixes golang/go#54828

Change-Id: I516dc83230f6bc96b0ff21f3bbae702f1511e5b0
GitHub-Last-Rev: b26f46a
GitHub-Pull-Request: #395
Reviewed-on: https://go-review.googlesource.com/c/tools/+/426875
Auto-Submit: Tim King <taking@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Tim King <taking@google.com>
Reviewed-by: Tim King <taking@google.com>
  • Loading branch information
timonwong authored and gopherbot committed Sep 7, 2022
1 parent bac5507 commit dd1bab2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions go/analysis/passes/printf/printf.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,12 +672,13 @@ func (s *formatState) parseIndex() bool {
s.scanNum()
ok := true
if s.nbytes == len(s.format) || s.nbytes == start || s.format[s.nbytes] != ']' {
ok = false
s.nbytes = strings.Index(s.format, "]")
ok = false // syntax error is either missing "]" or invalid index.
s.nbytes = strings.Index(s.format[start:], "]")
if s.nbytes < 0 {
s.pass.ReportRangef(s.call, "%s format %s is missing closing ]", s.name, s.format)
return false
}
s.nbytes = s.nbytes + start
}
arg32, err := strconv.ParseInt(s.format[start:s.nbytes], 10, 32)
if err != nil || !ok || arg32 <= 0 || arg32 > int64(len(s.call.Args)-s.firstArg) {
Expand Down
1 change: 1 addition & 0 deletions go/analysis/passes/printf/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func PrintfTests() {
Printf("%[2]*.[1]*[3]d x", 2, "hi", 4) // want `a.Printf format %\[2]\*\.\[1\]\*\[3\]d uses non-int \x22hi\x22 as argument of \*`
Printf("%[0]s x", "arg1") // want `a.Printf format has invalid argument index \[0\]`
Printf("%[0]d x", 1) // want `a.Printf format has invalid argument index \[0\]`
Printf("%[3]*.[2*[1]f", 1, 2, 3) // want `a.Printf format has invalid argument index \[2\*\[1\]`
// Something that satisfies the error interface.
var e error
fmt.Println(e.Error()) // ok
Expand Down

0 comments on commit dd1bab2

Please sign in to comment.