Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
adonovan opened this issue Nov 12, 2023 · 2 comments
Closed

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

adonovan opened this issue Nov 12, 2023 · 2 comments
Assignees
Labels
gopls/telemetry-wins gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Nov 12, 2023

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
	}
...
@gopherbot gopherbot added the gopls Issues related to the Go language server, gopls. label Nov 12, 2023
@adonovan adonovan added gopls/telemetry-wins gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed gopls Issues related to the Go language server, gopls. labels Nov 12, 2023
@adonovan
Copy link
Member Author

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{}

@seankhliao seankhliao 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 Nov 12, 2023
@gopherbot gopherbot added this to the Unreleased milestone Nov 12, 2023
@findleyr findleyr modified the milestones: Unreleased, gopls/v0.15.0 Nov 13, 2023
@gopherbot
Copy link

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

@adonovan adonovan self-assigned this Nov 16, 2023
@adonovan adonovan 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] Nov 17, 2023
@adonovan adonovan 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 Nov 17, 2023
@adonovan adonovan added the Tools This label describes issues relating to any tools in the x/tools repository. label Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/telemetry-wins gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants