Skip to content

fix: using integer max value as line end indicator - #1169

Merged
angelozerr merged 1 commit into
redhat-developer:mainfrom
mpeltonen:fix/integer-max-value-pattern
Jul 4, 2025
Merged

angelozerr merged 1 commit into
redhat-developer:mainfrom
mpeltonen:fix/integer-max-value-pattern

Conversation

@mpeltonen

@mpeltonen mpeltonen commented Jul 3, 2025 •

Copy link
Copy Markdown
Contributor

Some LSP servers use Integer.MAX_VALUE as a special character position value to indicate "end of line" in position coordinates. LSPIJUtils.toOffset was not correctly handling this, which lead to for example received diagnostics notifications to be silently ignored.

@angelozerr

Copy link
Copy Markdown
Contributor

@mpeltonen thanks for your contribution.

Your PR looks good but could you give some links about language server which uses that?

@rcjsuen

rcjsuen commented Jul 3, 2025

Copy link
Copy Markdown
Contributor

This sounds like #584 and microsoft/vscode-languageserver-node#1574...? 🤔

@angelozerr

Copy link
Copy Markdown
Contributor

This sounds like #584 and microsoft/vscode-languageserver-node#1574...? 🤔

Do you think this PR will fix the issue?

@rcjsuen

rcjsuen commented Jul 3, 2025

Copy link
Copy Markdown
Contributor

Depends on where the fix is I guess.

As you said previously in #584, LSP4J is choking on something so if the data is being "corrected" before sending it to LSP4J then it might fix it? Will have to see what @mpeltonen thinks after having a chance to review that issue maybe.

@mpeltonen

Copy link
Copy Markdown
Contributor Author

I came across this when implementing IntelliJ plugin for Garmin's MonkeyC language server which is included in their ConnectIQ SDK and this is the only one I know for sure: https://developer.garmin.com/connect-iq/sdk/

Started wondering why some of the diagnostic notifications were not visible in IntelliJ's "Problems" view, and further investigation led to this.

The "common pattern" is probably an overstatement and came from my discussions with ChatGPT about the issue.

From ChatGPT:

  1. Eclipse JDT Language Server (Java)
    • Uses Integer.MAX_VALUE as a sentinel value to indicate end-of-document in various LSP-related operations, particularly when mapping positions beyond the last line.
    • Source example: org.eclipse.lsp4j.Position objects being constructed with line = Integer.MAX_VALUE for EOF operations.
  2. VSCode TypeScript Language Server
    • Not in the core typescript-language-server, but in extensions or middleware handling, Number.MAX_SAFE_INTEGER (JS equivalent) is used to indicate an EOF-style position, which gets translated to Integer.MAX_VALUE in some language-bridging layers (e.g., Theia, Java-based LSP clients).
  3. Theia (Eclipse project)
    • Some Java backends using LSP for Theia encode Integer.MAX_VALUE when calculating document ranges that are meant to represent an open-ended range, especially for formatting or text replacement.
  4. Google Java Format Language Server
    • When applying formatting edits, uses Integer.MAX_VALUE to indicate edits that extend to the end of the file, especially in its TextEdit or Range wrappers for LSP compliance.
  5. SonarLint Language Server
    • Uses Integer.MAX_VALUE as part of range construction for whole-file diagnostics or fixes, especially when the file length is unknown at the time of range creation.

All cases exploit Integer.MAX_VALUE to bypass or denote special behavior in line/character-based document models of LSP. The claim is a synthesis from training data encompassing community discussions, partial implementations, and tooling integration layers rather than direct canonical usage in primary language server codebases.

@mpeltonen

Copy link
Copy Markdown
Contributor Author

Hmm, this might very well fix #584, but cannot be 100% sure.

The crux of the problem/fix is that if the end position is Integer.MAX_VALUE, then in LSPIJUtils.toTextRange this can cause null return (because of an overflow, start > end):

int start = LSPIJUtils.toOffset(range.getStart(), document);
int end = LSPIJUtils.toOffset(range.getEnd(), document);
if (start > end || end > docLength) {
    // Language server reports invalid range, ignore it.
    return null;
}

And there are more than one places like this in code:

TextRange range = LSPIJUtils.toTextRange(...)
if (range == null) {
    // Language server reports invalid diagnostic, ignore it.
    return;
}

@angelozerr
angelozerr merged commit 54f4a85 into redhat-developer:main Jul 4, 2025
@angelozerr

Copy link
Copy Markdown
Contributor

Thanks @mpeltonen !

Could you please:

@angelozerr angelozerr added the bug Something isn't working label Jul 4, 2025
@angelozerr angelozerr added this to the 0.15.0 milestone Jul 4, 2025
@mpeltonen

Copy link
Copy Markdown
Contributor Author

Thanks for a great plugin!

Will do those things after my plugin is in releasable state (and released)! 😎

@rcjsuen

rcjsuen commented Jul 7, 2025

Copy link
Copy Markdown
Contributor

Just to close the loop here, I updated from 0.14.0 to 0.14.3-20250705-112934 but I don't think it helped with #584. The server is sending this notification but I don't see the diagnostics in LSP4IJ.

And to be clear again as aforementioned in #584 (comment), I recognize that servers should not be sending this value (as it is greater than the defined uinteger).

{
    "uri": "file:///tmp/compose.yaml",
    "version": 2,
    "diagnostics": [
        {
            "range": {
                "start": {
                    "line": 3,
                    "character": 6
                },
                "end": {
                    "line": 3,
                    "character": 4294967295
                }
            },
            "severity": 1,
            "source": "docker-language-server",
            "message": "unexpected key name"
        }
    ]
}

@mpeltonen

Copy link
Copy Markdown
Contributor Author

Yeah, that is definitely a different case and I agree that that big of a value should not be sent by the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants