Skip to content

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

Closed
@adonovan

Description

@adonovan

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

Activity

added
goplsIssues related to the Go language server, gopls.
on Nov 12, 2023
added
goplsIssues related to the Go language server, gopls.
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
and removed
goplsIssues related to the Go language server, gopls.
on Nov 12, 2023
adonovan

adonovan commented on Nov 12, 2023

@adonovan
MemberAuthor

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{}
changed the title [-]gopls: out-of-bounds index in "stub methods" code action[/-] [+]x/tools/gopls: out-of-bounds index in "stub methods" code action[/+] on Nov 12, 2023
added this to the Unreleased milestone on Nov 12, 2023
gopherbot

gopherbot commented on Nov 16, 2023

@gopherbot
Contributor

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

self-assigned this
on Nov 16, 2023
changed the title [-]x/tools/gopls: out-of-bounds index in "stub methods" code action[/-] [+]x/tools/gopls: out-of-bounds index in "stub methods" code action [w2Y-6g][/+] on Nov 17, 2023
changed the title [-]x/tools/gopls: out-of-bounds index in "stub methods" code action [w2Y-6g][/-] [+]x/tools/gopls: out-of-bounds index in "stub methods" code action[/+] on Nov 17, 2023
added
ToolsThis label describes issues relating to any tools in the x/tools repository.
on Nov 17, 2023
gopherbot

gopherbot commented on Dec 11, 2023

@gopherbot
Contributor

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

added a commit that references this issue on Dec 11, 2023
f40889d
added a commit that references this issue on Feb 16, 2024
daa4aa5
adonovan

adonovan commented on Feb 27, 2024

@adonovan
MemberAuthor

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.

adonovan

adonovan commented on Feb 27, 2024

@adonovan
MemberAuthor

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.

adonovan

adonovan commented on Feb 27, 2024

@adonovan
MemberAuthor

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.

locked and limited conversation to collaborators on Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.gopls/telemetry-wins

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @adonovan@gopherbot@findleyr

      Issue actions

        x/tools/gopls: out-of-bounds index in "stub methods" code action · Issue #64087 · golang/go