Skip to content

Clamp LSP position conversions#3365

Merged
jakebailey merged 4 commits intomainfrom
jabaile/clamp-pos-conversion
Apr 9, 2026
Merged

Clamp LSP position conversions#3365
jakebailey merged 4 commits intomainfrom
jabaile/clamp-pos-conversion

Conversation

@jakebailey
Copy link
Copy Markdown
Member

As much as I hate it, we probably have to do this to deal with clients that are sending out of range requests.

Hopefully this isn't masking any lingering unicode bugs. I don't think so, though...

var utf16Char core.TextPos

for i, r := range script.Text()[start:] {
for i, r := range script.Text()[start:lineEnd] {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was actually a preexisting bug; we were not stopping at the end of lines. Oops...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you do this you may technically break the fuzzer. 😅

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how so?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +157 to 160
if int(line) >= len(lineMap.LineStarts) {
return textLen
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we errored in debug builds?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have that concept anymore. Only "debug builds" that are deoptimized for better delve debugging.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR makes LSP position conversions more defensive by clamping out-of-range inputs instead of panicking, improving interoperability with clients that send invalid positions.

Changes:

  • Clamp line/character inputs in LineAndCharacterToPosition to valid text bounds.
  • Clamp position and computed line in PositionToLineAndCharacter to prevent out-of-range behavior.
  • Avoid iterating beyond the current line when converting UTF-16 character offsets.

@jakebailey
Copy link
Copy Markdown
Member Author

Uh oh, that test failure is fun, I swear I ran tests locally before!

@Andarist
Copy link
Copy Markdown
Contributor

Andarist commented Apr 9, 2026

I had a similar one here: #2966 - maybe you could recycle some of its tests or something

@jakebailey
Copy link
Copy Markdown
Member Author

Ah, yeah... I truthfully forgot

@jakebailey jakebailey added this pull request to the merge queue Apr 9, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Apr 9, 2026
@jakebailey jakebailey added this pull request to the merge queue Apr 9, 2026
Merged via the queue into main with commit c25de70 Apr 9, 2026
37 of 39 checks passed
@jakebailey jakebailey deleted the jabaile/clamp-pos-conversion branch April 9, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants