golang/go

x/tools/gopls: out-of-bounds index in "stub methods" code action

Closed this issue · 6 comments

Another crash w2Y-6g, H0UbWA reported by telemetry:

golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods fromReturnStmt:18
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods GetStubInfo:10
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods DiagnosticForError:6
golang.org/x/tools/gopls/internal/lsp (*Server).codeAction
golang.org/x/tools/gopls/internal/lsp (*Server).codeAction:210
golang.org/x/tools/gopls/internal/lsp (*Server).CodeAction:1
- golang.org/x/tools/gopls@v0.14.1 go1.19.1 darwin/amd64 (1)
func fromReturnStmt(fset *token.FileSet, ti *types.Info, pos token.Pos, path []ast.Node, rs *ast.ReturnStmt) (*StubInfo, error) {
	returnIdx := -1
	for i, r := range rs.Results {
		if pos >= r.Pos() && pos <= r.End() {
			returnIdx = i
		}
	}
	if returnIdx == -1 {
		return nil, fmt.Errorf("pos %d not within return statement bounds: [%d-%d]", pos, rs.Pos(), rs.End())
	}
	concObj, pointer := concreteType(rs.Results[returnIdx], ti)
	if concObj == nil || concObj.Obj().Pkg() == nil {
		return nil, nil
	}
	ef := enclosingFunction(path, ti)
	if ef == nil {
		return nil, fmt.Errorf("could not find the enclosing function of the return statement")
	}
	iface := ifaceType(ef.Results.List[returnIdx].Type, ti) /// <--- this line fails, presumably because the index is OOB
	if iface == nil {
		return nil, nil
	}
...

Duplicates:

This stack R0oUQw was reported by telemetry:

gopls/bug
golang.org/x/tools/gopls/internal/bug.report:35
golang.org/x/tools/gopls/internal/bug.Errorf:2
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction.func1.1:2
runtime.gopanic:88
runtime.panicmem:?261
runtime.sigpanic:9
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods.fromReturnStmt:18
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods.GetStubInfo:10
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods.DiagnosticForError:6
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction.func1:11
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction:210
golang.org/x/tools/gopls/internal/lsp.(*Server).CodeAction:1
golang.org/x/tools/gopls/internal/lsp/protocol.serverDispatch:142
golang.org/x/tools/gopls/internal/lsp/lsprpc.(*StreamServer).ServeStream.ServerHandler.func3:5
golang.org/x/tools/gopls/internal/lsp/lsprpc.(*StreamServer).ServeStream.handshaker.func4:52
golang.org/x/tools/gopls/internal/lsp/protocol.Handlers.MustReplyHandler.func1:2
golang.org/x/tools/gopls@v0.14.2 go1.21.4 windows/amd64 vscode (1)

Issue created by golang.org/x/tools/gopls/internal/telemetry/cmd/stacks.

Dups: NNo9gw 8eUSuQ Mw6nJQ

This program reproduces the panic. The int("") triggers a "cannot convert" error, which causes GetStubInfo to assume the problem is because of a mismatched return operand and result type, but in fact there is no corresponding result type.

package p

func f() error {
	return nil, myerror{int("")}
}

type myerror struct{}

Change https://go.dev/cl/543018 mentions this issue: gopls/internal/lsp/source: stubmethods: fix out-of-bounds index

Change https://go.dev/cl/548737 mentions this issue: gopls/internal/analysis/stubmethods: fix OOB panic in fromValueSpec

This stack Mw6nJQ was reported by telemetry:

gopls/bug
golang.org/x/tools/gopls/internal/bug.report:35
golang.org/x/tools/gopls/internal/bug.Errorf:2
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction.func1.1:2
runtime.gopanic:80
runtime.panicmem:?260
runtime.sigpanic:9
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods.fromReturnStmt:18
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods.GetStubInfo:10
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods.DiagnosticForError:6
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction.func1:11
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction:210
golang.org/x/tools/gopls/internal/lsp.(*Server).CodeAction:1
golang.org/x/tools/gopls/internal/lsp/protocol.serverDispatch:142
golang.org/x/tools/gopls/internal/lsp/protocol.ServerHandler.func1:5
golang.org/x/tools/gopls/internal/lsp/lsprpc.handshaker.func1:52
golang.org/x/tools/internal/jsonrpc2.MustReplyHandler.func1:2
golang.org/x/tools/gopls@v0.14.2 go1.19.6 windows/amd64 vscode (1)

Issue created by golang.org/x/tools/gopls/internal/telemetry/cmd/stacks.

This stack 8eUSuQ was reported by telemetry:

gopls/bug
golang.org/x/tools/gopls/internal/bug.report:35
golang.org/x/tools/gopls/internal/bug.Errorf:2
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction.func1.1:2
runtime.gopanic:80
runtime.goPanicIndex:2
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods.fromReturnStmt:18
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods.GetStubInfo:10
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods.DiagnosticForError:6
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction.func1:11
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction:210
golang.org/x/tools/gopls/internal/lsp.(*Server).CodeAction:1
golang.org/x/tools/gopls/internal/lsp/protocol.serverDispatch:142
golang.org/x/tools/gopls/internal/lsp/protocol.ServerHandler.func1:5
golang.org/x/tools/gopls/internal/lsp/lsprpc.handshaker.func1:52
golang.org/x/tools/internal/jsonrpc2.MustReplyHandler.func1:2
golang.org/x/tools/internal/jsonrpc2.AsyncHandler.func1.2:3
golang.org/x/tools/gopls@v0.14.2 go1.20.4 linux/amd64 vscode (1)

Issue created by golang.org/x/tools/gopls/internal/telemetry/cmd/stacks.

This stack NNo9gw was reported by telemetry:

gopls/bug
golang.org/x/tools/gopls/internal/bug.report:35
golang.org/x/tools/gopls/internal/bug.Errorf:2
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction.func1.1:2
runtime.gopanic:50
runtime.panicmem:?261
runtime.sigpanic:19
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods.fromReturnStmt:18
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods.GetStubInfo:10
golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods.DiagnosticForError:6
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction.func1:11
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction:210
golang.org/x/tools/gopls/internal/lsp.(*Server).CodeAction:1
golang.org/x/tools/gopls/internal/lsp/protocol.serverDispatch:142
golang.org/x/tools/gopls/internal/lsp/lsprpc.(*StreamServer).ServeStream.ServerHandler.func3:5
golang.org/x/tools/gopls/internal/lsp/lsprpc.(*StreamServer).ServeStream.handshaker.func4:52
golang.org/x/tools/gopls/internal/lsp/protocol.Handlers.MustReplyHandler.func1:2
golang.org/x/tools/gopls@v0.14.2 go1.22.0 darwin/arm64 vscode (2)
golang.org/x/tools/gopls@v0.14.2 devel darwin/arm64 vscode (1)

Issue created by golang.org/x/tools/gopls/internal/telemetry/cmd/stacks.