Skip to content

fix: resolve crashes in DiffStreamHandler — unsafe cast and negative line numbers#11893

Merged
RomneyDa merged 2 commits into
mainfrom
diff-stream-crashes
Mar 26, 2026
Merged

fix: resolve crashes in DiffStreamHandler — unsafe cast and negative line numbers#11893
RomneyDa merged 2 commits into
mainfrom
diff-stream-crashes

Conversation

@RomneyDa

@RomneyDa RomneyDa commented Mar 26, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fix A: Use safe cast (as?) for TextEditor in undoChanges() to prevent ClassCastException/NullPointerException when the selected editor is not a TextEditor
  • Fix B: Add early return for empty documents and clamp line numbers with coerceAtLeast(0) in initUnfinishedRangeHighlights() to prevent IndexOutOfBoundsException
  • Fix C: Remove redundant .toList() call in rejectAll() — the list was already copied on the previous line

Fixes #4267

Test plan

  • Open a diff stream on an empty file — verify no crash in initUnfinishedRangeHighlights()
  • Trigger undoChanges() when the active editor is not a TextEditor (e.g., image viewer) — verify graceful no-op instead of crash
  • Reject all diff blocks after accepting/rejecting one — verify behavior unchanged

Summary by cubic

Fixes two crashes in DiffStreamHandler by safely handling non-TextEditor cases and clamping line numbers for empty or short documents. Prevents crashes when opening diffs on empty files and when undo is triggered in non-text editors. Fixes #4267.

  • Bug Fixes

    • Use safe cast in undoChanges() and no-op if not a TextEditor to avoid ClassCastException/NullPointerException.
    • Early return for empty documents and clamp line numbers in initUnfinishedRangeHighlights() to avoid IndexOutOfBoundsException.
  • Refactors

    • Remove redundant .toList() in rejectAll() to avoid an extra allocation.
    • Inline lineNum in initUnfinishedRangeHighlights() for clarity.

Written for commit 324f1a1. Summary will update on new commits.

…line numbers

- Use safe cast (as?) for TextEditor in undoChanges() to prevent ClassCastException
- Add early return for empty documents and clamp line numbers to prevent IndexOutOfBoundsException in initUnfinishedRangeHighlights()
- Remove redundant .toList() call in rejectAll()

Fixes #4267
@RomneyDa
RomneyDa requested a review from a team as a code owner March 26, 2026 18:48
@RomneyDa
RomneyDa requested review from Patrick-Erichsen and removed request for a team March 26, 2026 18:48
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 26, 2026
@continue

continue Bot commented Mar 26, 2026

Copy link
Copy Markdown
Contributor

Docs Review

No documentation updates needed for this PR.

Reasoning: These changes are internal defensive programming fixes to the JetBrains IntelliJ extension's DiffStreamHandler. The PR addresses edge-case crashes (safe casting, bounds checking, code cleanup) without introducing any new features, configuration options, or user-facing behavior changes. The existing documentation remains accurate and complete.

@continue

continue Bot commented Mar 26, 2026

Copy link
Copy Markdown
Contributor

Test Coverage Review

This PR fixes crash conditions in DiffStreamHandler.kt with defensive guards:

  1. Empty document guard - Early return when lineCount == 0
  2. Safe cast - as? TextEditor ?: return instead of unsafe cast
  3. Negative line bounds - .coerceAtLeast(0) to prevent negative line numbers

Missing Regression Tests

Per the test coverage guidelines, bug fixes should include regression tests that reproduce the original bug condition and verify the fix. Consider adding tests for:

  • Empty document case: Verify initUnfinishedRangeHighlights() doesn't crash on empty documents
  • Non-TextEditor scenario: Verify undoChanges() gracefully handles when getSelectedEditor() returns a non-TextEditor
  • Line number boundary: Verify no negative line numbers are passed to addLineHighlighter() when startLine > lineCount

The IntelliJ test infrastructure (BasePlatformTestCase) is already used in VerticalDiffBlockTest.kt, so similar patterns could be applied here.


Automated test coverage check

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 1 file

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Mar 26, 2026
@RomneyDa
RomneyDa merged commit 5a8d10f into main Mar 26, 2026
65 of 66 checks passed
@RomneyDa
RomneyDa deleted the diff-stream-crashes branch March 26, 2026 21:28
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extension Crash in JetBrains commit message interface

2 participants