fix: prevent two OOB crashes in updateAnnotatedString (Samsung soft keyboard + large text shrink)#721
Open
tfkci wants to merge 1 commit into
Open
fix: prevent two OOB crashes in updateAnnotatedString (Samsung soft keyboard + large text shrink)#721tfkci wants to merge 1 commit into
tfkci wants to merge 1 commit into
Conversation
**Bug 1 — IndexOutOfBoundsException in updateAnnotatedString** `richParagraphList.removeAt(i)` was called inside `fastForEachIndexed`, which captures the list size once at loop entry. After the first removal the list shrinks but the loop index keeps incrementing, eventually accessing an index beyond the new size and throwing. Fix: collect stale paragraph indices during the loop and remove them in reverse order after `buildAnnotatedString` completes. **Bug 2 — StringIndexOutOfBoundsException in AnnotatedStringExt** `text.substring(index, index + richSpan.text.length)` assumed the span's stored length never exceeds the available text. Samsung soft keyboards on budget devices (Galaxy A04, A57, and similar) route certain input events — autocomplete, autocorrect, word suggestions — through `TextFieldKeyInput` (dispatchKeyEvent) rather than the standard IME protocol. This delivers a new `TextFieldValue` atomically before span reconciliation runs, so a span's stale stored length can exceed the incoming text length → crash. Physical keyboards connected to Android trigger the same code path. Fix: clamp `safeStart` and `safeEnd` to `text.length` before the `substring` call so stale span lengths are handled gracefully. Adds `RichTextStateParagraphRemovalTest` with three regression cases covering both crash paths. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two
OutOfBoundsExceptioncrashes inupdateAnnotatedStringand its call chain, both reproducible on Samsung budget/mid-range devices (Galaxy A04, A57, M-series) and physical keyboards.Bug 1 —
IndexOutOfBoundsException: index: N, size: NLocation:
RichTextState.updateAnnotatedStringrichParagraphList.removeAt(i)was called insidefastForEachIndexed, which captures the list size once at loop entry. After the first removal the list shrinks but the loop index keeps incrementing, eventually accessing an index beyond the new size.Fix: Collect stale paragraph indices during the loop (
paragraphIndicesToRemove) and remove them in reverse order afterbuildAnnotatedStringcompletes.Bug 2 —
StringIndexOutOfBoundsException: begin N, end M, length L(M > L)Location:
AnnotatedStringExt.kt—text.substring(index, index + richSpan.text.length)Samsung soft keyboards on budget devices route certain operations (autocomplete, autocorrect, word suggestions) through
TextFieldKeyInput(dispatchKeyEvent) rather than the standard IME protocol. This delivers a newTextFieldValueatomically before span reconciliation runs, so a span's stale stored length can exceed the incoming text length. Physical keyboards connected to Android hit the same code path.Typical stack:
Fix: Clamp
safeStartandsafeEndtotext.lengthbefore thesubstringcall so stale span lengths are handled gracefully.Test plan
RichTextStateParagraphRemovalTest— three new regression cases covering both crash paths./gradlew :richeditor-compose:desktopTest)Devices known to trigger Bug 2
Samsung Galaxy A04, A57 (and likely other Samsung A/M-series budget phones using Samsung Keyboard with word suggestions enabled).