Fix TextBox IME replacement range crash

Closed
LA
1mo ago
+62-1

Summary

  • Clamp TextBox NSTextInput replacement ranges to current UTF-16 storage bounds before calling NSTextView.insertText/setMarkedText.
  • Add a regression test for a stale IME replacement range at the Japanese text UTF-16 end boundary.

Testing

  • scripts/lint-pbxproj-test-wiring.sh: pass
  • Remote AWS cmux-unit red/green attempt: invalid, runner ran out of disk during app compilation before executing the focused test.

Issues


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.


[!NOTE]
Low Risk
Localized defensive bounds checks on text input paths; regression test added; no auth or data-layer changes.

Overview
Fixes a crash when IME (e.g. Japanese input) passes replacement or marked-text ranges that extend past the current UTF-16 text storage—such as a stale range at the end of 日本語.

TextBoxInputTextView now clamps replacementRange and selectedRange to valid bounds before calling insertText / setMarkedText, using the attributed string length (or the incoming marked string length for selection). Attachment cleanup and marked-text callbacks use the same clamped ranges.

A unit test covers inserting with an out-of-range replacement at the UTF-16 end boundary.

Reviewed by Cursor Bugbot for commit 1c8139954deb3c0e1c0dbe5acec63b5ee18b1532. Bugbot is set up for automated code reviews on this repo. Configure here.


Summary by cubic

Clamp and normalize IME replacement and selection ranges in TextBoxInputTextView to valid UTF-16 bounds to prevent NSRange crashes and ensure correct caret placement. Fixes Sentry CMUXTERM-MACOS-VD0.

  • Bug Fixes
    • Clamp insertText/setMarkedText replacementRange to the current text length; sanitize selectedRange within marked text, defaulting to the end when NSNotFound.
    • Add tests for clamping a stale IME range at the Japanese end boundary and for normalizing a missing selectedRange.

Summary by CodeRabbit

  • Bug Fixes

    • Prevented out-of-bounds ranges during input-method (IME) interactions by sanitizing and clamping replacement/selection ranges, improving stability for international input.
    • Ensured marked-text handling no longer propagates invalid ranges, fixing cursor/selection anomalies.
  • Tests

    • Added tests covering insertion and marked-text scenarios with international characters and IME edge cases.