Skip to content

fix: StringIndexOutOfBoundsException in presentation compiler's hasColon method #23498

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

Merged
merged 1 commit into from
Jul 10, 2025

Conversation

m-gris
Copy link
Contributor

@m-gris m-gris commented Jul 10, 2025

This PR fixes a StringIndexOutOfBoundsException that could occur in the presentation compiler when checking for colons in override completions.

Problem

The dotty.tools.pc.completions.OverrideCompletions.hasColon method was accessing characters at specific indices without first verifying that the indices were within the text bounds. This could cause crashes when the span end positions were at or beyond the text length.

Solution

Added bounds checking before accessing characters:

  • For TypeDef cases: verify td.rhs.span.end < text.length before text.charAt(td.rhs.span.end)
  • For Template parent cases: filter to ensure text.length > idx before text.charAt(idx)

Testing

The existing test suite passes, and a new test case no-new-line was added to cover the edge case.

resolves scalameta/metals#7575

Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

LGTM!

@tgodzik tgodzik merged commit 01447df into scala:main Jul 10, 2025
29 checks passed
tgodzik pushed a commit to scala/scala3-lts that referenced this pull request Jul 23, 2025
…lon method (scala#23498)

This PR fixes a `StringIndexOutOfBoundsException` that could occur in
the presentation compiler when checking for colons in override
completions.

## Problem
The `dotty.tools.pc.completions.OverrideCompletions.hasColon` method was
accessing characters at specific indices without first verifying that
the indices were within the text bounds. This could cause crashes when
the span end positions were at or beyond the text length.

## Solution
Added bounds checking before accessing characters:
- For `TypeDef` cases: verify `td.rhs.span.end < text.length` before
`text.charAt(td.rhs.span.end)`
- For `Template` parent cases: filter to ensure `text.length > idx`
before `text.charAt(idx)`

## Testing
The existing test suite passes, and a new test case `no-new-line` was
added to cover the edge case.

[resolves
scalameta/metals#7575](scalameta/metals#7575)
[Cherry-picked 01447df]
tgodzik pushed a commit to scala/scala3-lts that referenced this pull request Jul 24, 2025
…lon method (scala#23498)

This PR fixes a `StringIndexOutOfBoundsException` that could occur in
the presentation compiler when checking for colons in override
completions.

## Problem
The `dotty.tools.pc.completions.OverrideCompletions.hasColon` method was
accessing characters at specific indices without first verifying that
the indices were within the text bounds. This could cause crashes when
the span end positions were at or beyond the text length.

## Solution
Added bounds checking before accessing characters:
- For `TypeDef` cases: verify `td.rhs.span.end < text.length` before
`text.charAt(td.rhs.span.end)`
- For `Template` parent cases: filter to ensure `text.length > idx`
before `text.charAt(idx)`

## Testing
The existing test suite passes, and a new test case `no-new-line` was
added to cover the edge case.

[resolves
scalameta/metals#7575](scalameta/metals#7575)
[Cherry-picked 01447df]
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.

2 participants