Skip to content

Commit

Permalink
gopls/internal/golang/completion: fix crash with extra results
Browse files Browse the repository at this point in the history
Fix an incorrect condition when extracting expected result types: we
must check the length of signature, not number of returns, before
accessing the expected result type.

Fixes golang/go#70636

Change-Id: I59d84283073c99117c40de584db6f576f2484206
Reviewed-on: https://go-review.googlesource.com/c/tools/+/632936
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
findleyr committed Dec 2, 2024
1 parent 8ffeaba commit ef3d603
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gopls/internal/golang/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2624,7 +2624,7 @@ func expectedAssignStmtTypes(pkg *cache.Package, node *ast.AssignStmt, pos token
// Returns nil if enclosingSig is nil.
func expectedReturnStmtType(enclosingSig *types.Signature, node *ast.ReturnStmt, pos token.Pos) types.Type {
if enclosingSig != nil {
if resultIdx := exprAtPos(pos, node.Results); resultIdx < len(node.Results) {
if resultIdx := exprAtPos(pos, node.Results); resultIdx < enclosingSig.Results().Len() {
return enclosingSig.Results().At(resultIdx).Type()
}
}
Expand Down
23 changes: 23 additions & 0 deletions gopls/internal/test/marker/testdata/completion/issue70636.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
This test reproduces the crash of golang/go#70636, an out of bounds error when
analyzing a return statement with more results than the signature expects.

-- flags --
-ignore_extra_diags

-- go.mod --
module example.com

go 1.21

-- p.go --
package p

var xx int
var xy string


func _() {
return Foo(x) //@ rank(re"x()", "xx", "xy")
}

func Foo[T any](t T) T {}

0 comments on commit ef3d603

Please sign in to comment.