Skip to content

Editor: IME and dictation can crash the app through unguarded ranges in TextView+NSTextInput #2339

Closed

Description

@datlechin

Found while fixing #2338 (an empty NSBezierPath reaching bounds inside drawRect). Same crash class: an AppKit call raises an ObjC exception, and on macOS 26 and later that terminates the app.

LocalPackages/CodeEditTextView/Sources/CodeEditTextView/TextView/TextView+NSTextInput.swift:

// line 241
let realRange = (textStorage.string as NSString).rangeOfComposedCharacterSequences(for: range)
// line 243
return textStorage.attributedSubstring(from: realRange)

The doc comment directly above says the implementation "should be prepared for aRange to be out of bounds", should return the intersection, and should return nil when the location is completely outside. Neither call does that. Both raise: rangeOfComposedCharacterSequences(for:) with NSInvalidArgumentException ("The index N is invalid", the same reason string as the crash fixed in #1835), attributedSubstring(from:) with NSRangeException. Both measured on macOS 27 with an Obj-C @try/@catch probe.

firstRect(forCharacterRange:actualRange:) at line 263 has the same shape, and the input context polls it while a candidate window is open.

The callers are outside our control: any IME (CJK candidate windows), dictation, the character viewer and Ink all call these with ranges they computed earlier. A stale range is normal traffic here, not an edge case.

Fix: clamp to the storage length and return nil when the range does not intersect it, which is what the doc comment already specifies.

Activity

  1. added
    bug
    on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions