Skip to content

Commit 344e482

Browse files
xzbdmwgopherbot
authored andcommitted
golang/internal/highlight: check idx < len before indexing
Change-Id: I2d36c2b64e3af00861ce9913f733247877b2239e GitHub-Last-Rev: f6b4312 GitHub-Pull-Request: #556 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642756 Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Findley <[email protected]>
1 parent 4828981 commit 344e482

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

gopls/internal/golang/highlight.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ func formatStringAndIndex(info *types.Info, call *ast.CallExpr) (*ast.BasicLit,
166166
return nil, -1
167167
}
168168
idx := sig.Params().Len() - 2
169-
if idx < 0 {
170-
// Skip checking variadic functions without
171-
// fixed arguments.
169+
if !(0 <= idx && idx < len(call.Args)) {
170+
// Skip checking functions without a format string parameter, or
171+
// missing the corresponding format argument.
172172
return nil, -1
173173
}
174174
// We only care about literal format strings, so fmt.Sprint("a"+"b%s", "bar") won't be highlighted.

gopls/internal/test/marker/testdata/highlight/highlight_printf.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,10 @@ func Indexed() {
5353
func MultipleIndexed() {
5454
fmt.Printf("%[1]d %[1].2d", 3) //@hiloc(m1, "%[1]d", write),hiloc(m2, "3", read),hiloc(m3, "%[1].2d", write),highlightall(m1, m2, m3)
5555
}
56+
57+
// This test checks that gopls doesn't crash (index out of bounds)
58+
// while haven't fill the last non-variadic argument.
59+
func NoEffectOnUnfinishedArg() {
60+
var s string //@hiloc(var, "s", write)
61+
fmt.Fprintf(s) //@hiloc(firstArg, "s", read),highlightall(var, firstArg)
62+
}

0 commit comments

Comments
 (0)