Skip to content

go/parser: StructType.End() can underflow the File #66683

Open

Description

@adonovan

A struct type with a missing close brace has an StructType.End pos of zero. That means the enclosing nodes such as ReturnStmt may also have a computed End that is zero, or perhaps slightly larger:

Screenshot 2024-04-04 at 2 43 08 PM

I believe this is the root cause of at least some of the many gopls crashes recorded in #64547.

Ultimately this is a problem of parser error recovery. We need to establish the invariants that:

  • all tree nodes have valid Pos/End positions; and
  • all tree nodes have nested Pos/End subranges (with a possible exception for the func keyword in FuncDecl/FuncType).

See also:

Activity

  1. added
    gopls/parsingIssues related to parsing / poor parser recovery.
    RefactoringIssues related to refactoring tools
    on Apr 4, 2024
  2. changed the title [-]go/parser: StructType.End() can overflow the File[/-] [+]go/parser: StructType.End() can underflow the File[/+] on Apr 4, 2024
  3. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Apr 4, 2024
  4. gopherbot commented on Apr 4, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/576655 mentions this issue: gopls/internal/cache: analysis: repair start/end and refine bug report

  5. findleyr commented on Apr 8, 2024

    @findleyr
    Contributor

    Let's try to fix this for Go 1.23. Adding to the milestone.

  6. added this to the Go1.23 milestone on Apr 8, 2024
  7. gopherbot commented on Apr 9, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/577595 mentions this issue: [gopls-release-branch.0.15] gopls/internal/cache: analysis: repair start/end and refine bug report

  8. adonovan commented on Apr 11, 2024

    @adonovan
    MemberAuthor

    @findleyr suggests that a fuzzer connected to an invariant checker would be a good source of regression test cases.

  9. findleyr commented on Apr 12, 2024

    @findleyr
    Contributor

    Shall we expand this issue to introducing and enforcing our stated invariant?

  10. adonovan commented on Apr 12, 2024

    @adonovan
    MemberAuthor

    Shall we expand this issue to introducing and enforcing our stated invariant?

    Perhaps we need an umbrella issue. See also #66790 (comment), which is the beginning of a theory of pos/end in ill-formed trees.

  11. modified the milestones: Go1.23, Go1.24 on Aug 13, 2024
  12. gopherbot commented on Nov 21, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/630675 mentions this issue: gopls/internal/crash: don't crash in xrefs on out of bound nodes

  13. modified the milestones: Go1.24, Go1.25 on Feb 11, 2025
  14. gopherbot commented on Apr 25, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/668238 mentions this issue: go/ast: enforce FileStart <= Node.Pos <= Node.End <= FileEnd

  15. gopherbot commented on Apr 28, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/668677 mentions this issue: x/tools: prepare for go1.25 parser changes

  16. gopherbot commented on May 12, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/672055 mentions this issue: x/tools: various cleanups related to planned parser changes

  17. modified the milestones: Go1.25, Go1.26 on Aug 12, 2025
  18. modified the milestones: Go1.26, Go1.27 on Feb 10, 2026
  19. modified the milestones: Go1.27, Go1.28 on Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.RefactoringIssues related to refactoring toolsgopls/parsingIssues related to parsing / poor parser recovery.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions