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/internal/lsp/template: panic - slice bounds out of range (crash) #57621

Open
kleinnic74 opened this issue Dec 20, 2022 · 4 comments
Assignees
Labels
gopls/completion Issues related to auto-completion in gopls. gopls Issues related to the Go language server, gopls. NeedsFix The path to resolution is known, but the work has not been done. okay-after-beta1 Used by release team to mark a release-blocker issue as okay to resolve either before or after beta1 Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@kleinnic74
Copy link

gopls version: v0.11.0 (go1.19.3)
gopls flags:
update flags: proxy
extension version: 0.37.0
go version: 1.19.3
environment: Visual Studio Code win32
initialization error: undefined
issue timestamp: Tue, 20 Dec 2022 12:17:00 GMT
restart history:
Tue, 20 Dec 2022 09:27:58 GMT: activation (enabled: true)

ATTENTION: PLEASE PROVIDE THE DETAILS REQUESTED BELOW.

Describe what you observed.

panic: runtime error: slice bounds out of range [39:37]

goroutine 163 [running]:
golang.org/x/tools/gopls/internal/lsp/template.(*completer).complete(0xc001ea7350)
	  completion.go:116  0xd05
golang.org/x/tools/gopls/internal/lsp/template.Completion({0xc0016cd740%3F, 0xc001a4b048%3F}, {0x164d118%3F, 0xc0009a0160%3F}, {0x1644920, 0xc0004b7ce0}, {0x0%3F, 0x0%3F}, {0x1, {0x0, ...}})
	  completion.go:55  0x30e
golang.org/x/tools/gopls/internal/lsp.(*Server).completion(0x1257ec0%3F, {0x1640c78, 0xc000192300}, 0xc0018d5d10)
	  completion.go:43  0x31d
golang.org/x/tools/gopls/internal/lsp.(*Server).Completion(0xc000196320%3F, {0x1640c78%3F, 0xc000192300%3F}, 0x1257ec0%3F)
	  server_gen.go:28  0x25
golang.org/x/tools/gopls/internal/lsp/protocol.serverDispatch({0x1640c78, 0xc000192300}, {0x1650ae0, 0xc00037a100}, 0xc0018d2270, {0x1641068, 0xc000192280})
	  tsserver.go:269  0x20c2
golang.org/x/tools/gopls/internal/lsp/protocol.ServerHandler.func1({0x1640c78, 0xc000192300}, 0xc0018d2270, {0x1641068, 0xc000192280})
	  protocol.go:157  0x90
golang.org/x/tools/gopls/internal/lsp/lsprpc.handshaker.func1({0x1640c78, 0xc000192300}, 0xc0018d2270, {0x1641068%3F, 0xc000192280%3F})
	  lsprpc.go:519  0xa39
golang.org/x/tools/internal/jsonrpc2.MustReplyHandler.func1({0x1640c78, 0xc000192300}, 0xc00028e378, {0x1641068%3F, 0xc000192280%3F})
	  handler.go:35  0xf6
golang.org/x/tools/internal/jsonrpc2.AsyncHandler.func1.2()
	  handler.go:103  0xa3
created by golang.org/x/tools/internal/jsonrpc2.AsyncHandler.func1
	  handler.go:100  0x20a
[Error - 13:16:48] 

OPTIONAL: If you would like to share more information, you can attach your complete gopls logs.

NOTE: THESE MAY CONTAIN SENSITIVE INFORMATION ABOUT YOUR CODEBASE.
DO NOT SHARE LOGS IF YOU ARE WORKING IN A PRIVATE REPOSITORY.

<OPTIONAL: ATTACH LOGS HERE>

@findleyr
Copy link
Contributor

CC @pjweinb

@hyangah hyangah added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 21, 2022
@hyangah hyangah changed the title gopls: automated issue report (crash) x/tools/gopls/internal/lsp/template: panic - slice bounds out of range (crash) Jan 5, 2023
@hyangah hyangah transferred this issue from golang/vscode-go Jan 5, 2023
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Jan 5, 2023
@gopherbot gopherbot added this to the Unreleased milestone Jan 5, 2023
@findleyr findleyr modified the milestones: Unreleased, gopls/v0.12.0 Jan 5, 2023
@findleyr
Copy link
Contributor

findleyr commented Jan 5, 2023

CC @pjweinb

@findleyr findleyr added release-blocker okay-after-beta1 Used by release team to mark a release-blocker issue as okay to resolve either before or after beta1 labels Apr 17, 2023
@adonovan adonovan closed this as completed May 9, 2023
@adonovan adonovan reopened this May 9, 2023
@adonovan
Copy link
Member

adonovan commented May 9, 2023

I'm not at all familiar with the internal/lsp/template package, but from the crash information we can tell that the failure is below in completion.go, and that the cause is that c.offset is greater than start (39 > 37) but start itself is within bounds (since the error didn't mention "with capacity").

func (c *completer) complete() (*protocol.CompletionList, error) {
	ans := &protocol.CompletionList{IsIncomplete: true, Items: []protocol.CompletionItem{}}
	start := c.p.FromPosition(c.pos)
	sofar := c.p.buf[c.offset:start] // panic (39:37)

Reading a little more, we can tell that:

  • c.offset is 39, computed from start (37) + len("{{") (2).
  • inTemplate must have returned 37.
  • c.p.FromPosition(c.pos) must also be 37, since it provides the upper bound of the slice.
  • offset within inTemplate must be 37, since it comes from the same expression.
  • the first loop within inTemplate does not return, because it can only return a value less than offset, but the function returns 37, which is equal to offset.
  • therefore the second loop must have returned, since the final return is negative.
  • thus 37 must have come from fc.elided, and the region being tested for {{ and }} is empty.
  • so elideAt(37) must have been called. This means there was "{{" at 37. In short, the completion point was before a "{{".

I just tried creating a new template file in VSCode, inserting {{, backing up by two characters, and inserting a, and I got a panic similar to this one. Yay.

@adonovan adonovan added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels May 9, 2023
@findleyr
Copy link
Contributor

This seems like it's not getting fixed for v0.12.0. That's ok. It was marked as a release blocker because it is a crasher, but I don't think the template feature should block the release. Moving to v0.13.0.

@findleyr findleyr modified the milestones: gopls/v0.12.0, gopls/v0.13.0 May 21, 2023
@findleyr findleyr modified the milestones: gopls/v0.14.0, gopls/v0.15.0 Oct 9, 2023
@adonovan adonovan added the gopls/completion Issues related to auto-completion in gopls. label Dec 11, 2023
@findleyr findleyr modified the milestones: gopls/v0.15.0, gopls/v0.16.0 Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/completion Issues related to auto-completion in gopls. gopls Issues related to the Go language server, gopls. NeedsFix The path to resolution is known, but the work has not been done. okay-after-beta1 Used by release team to mark a release-blocker issue as okay to resolve either before or after beta1 Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

6 participants