Skip to content

Editor: remaining unguarded text-storage ranges on the layout and highlight paths #2340

Closed

Description

@datlechin

Found while fixing #2338. Each of these reads or writes a text-storage range that can transiently outrun the storage, and each raises rather than clamping. On macOS 26 and later an exception raised inside a draw or layout pass terminates the app.

Ranked by reachability:

  1. LocalPackages/CodeEditTextView/Sources/CodeEditTextView/TextLine/TextLine.swift:57, stringRef.attributedSubstring(from: range) in prepareForDisplay, reached from the layout pass at TextLayoutManager+Layout.swift:241. range is position.range from lineStorage, which is known to transiently outrun the string during an edit. This is the layout-pass twin of the rectForOffset crash fixed in fix(editor): stop the rectForOffset crash and keep the autocomplete popup below the caret #1835. Every other consumer of that invariant got a guard in that commit; this one did not.

  2. LocalPackages/CodeEditSourceEditor/Sources/CodeEditSourceEditor/Highlighting/Highlighter.swift:279, storage?.setAttributes(_, range: range) raises NSRangeException if the style container's runs outrun the storage. Lower confidence: styleContainer.updateStorageLength tracks edits, so the stale window could not be constructed by reading alone.

  3. LocalPackages/CodeEditTextView/Sources/CodeEditTextView/TextView/TextView+Accessibility.swift:141, the guard is index < documentRange.length with no lower bound, so a negative index from an accessibility client reaches rangeOfComposedCharacterSequence(at:) and raises "The index -1 is invalid". One-line fix.

  4. LocalPackages/CodeEditSourceEditor/Sources/CodeEditSourceEditor/Minimap/MinimapLineFragmentView.swift:82-95, textStorage.attribute(.foregroundColor, at:longestEffectiveRange:in:) and character(at:) with positions from newFragment.documentRange and no bounds check. Currently unreachable: EditorPeripherals.swift:62 is the only construction site and hardcodes showMinimap: false. It is a landmine for whoever turns the minimap on, since the minimap's standalone layout manager is exactly the one that goes stale.

Checked and clean, so nobody re-hunts them: all of TablePro's own drawing code, every NSColor component read (all guarded by usingColorSpace), and the NaN sinks in NSRectFill, NSString.draw(in:), NSCell.draw(withFrame:) and CGContext.fill, none of which raise.

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