Skip to content

fix(editor): stop the editor crashing when it redraws an emphasis whose text is gone - #2338

Merged
datlechin merged 1 commit into
mainfrom
fix/emphasis-empty-path-crash
Aug 21, 2026
Merged

datlechin merged 1 commit into
mainfrom
fix/emphasis-empty-path-crash

Conversation

@datlechin

Copy link
Copy Markdown
Member

A crash report from 0.67.0 (build 120) on macOS 27.0: EXC_BREAKPOINT through +[NSApplication _crashOnException:], with an uncaught NSGenericException raised by -[NSBezierPath controlPointBounds] inside a view's drawRect.

What happened

TextView.draw(_:) calls emphasisManager?.updateLayerBackgrounds() on every draw, with no dirty-rect or validity filter. For a .underline emphasis, makeShapePath built its path from layoutManager.rectsFor(range:) and returned that path even when the array was empty, so the caller got a path with zero elements. updateLayerBackgrounds then read shapePath.bounds to position the emphasis's text layer, and bounds raises NSGenericException ("No current point for control point bounds") on a path with no elements.

rectsFor(range:) returns an empty array whenever the emphasis range outruns the string or its line has no laid-out fragments, which is routine after an edit. TablePro reaches it far more often than upstream does: QueryDiagnosticsController keeps .underline emphases on the document across a 500 ms debounce, so their ranges are regularly stale relative to what is laid out. The guard added in #1835 (range.upperBound <= textStorage.length) is what turns a stale range into an empty rect list, so that fix moved the failure one frame later instead of removing it.

Two things made this fatal rather than cosmetic:

  • On macOS 26 and later, AppKit no longer lets an exception escape drawRect as a logged non-fatal. It catches it inside _NSViewDrawRect and calls +[NSApplication _crashOnException:]. Measured here: NSApplicationCrashOnExceptions NO does not help, and neither does linking against an older SDK (checked at deployment targets 13.0, 15.0 and 26.0).
  • The bug is live on upstream CodeEditTextView main, so it will need re-applying if the vendored fork is rebased.

The fix

  • makeShapePath returns nil when there is nothing to draw, instead of a path with no elements. updateLayerBackgrounds already skips a nil shape.
  • New NSBezierPath.drawableBounds returns nil for an empty path. Both reads of bounds in EmphasisManager go through it, so drawing code can no longer reach the accessor that raises.
  • smoothPath returns an optional and answers nil for fewer than two points, so roundedPathForRange propagates nil rather than an empty path. It also guards the one division the function did not already guard: identical first two points produced NaN coordinates, and a NaN point raises on move(to:), on .bounds and on .cgPath.
  • createTextLayer checks the emphasis range against the text storage before slicing it. Two more exception routes sat on this same path, both from the same stale range: attributedSubstring(from:) raises NSRangeException for a range past the end, and attribute(.font, at: 0) raises for a zero-length range. The second one fired first in the reproduction run, so it ships here.

Verification

  • New test testEmphasisOutlivingTheTextItMarksSurvivesRedraw adds an underline emphasis, deletes the text under it, relays out and redraws. Against the unfixed code it dies with NSGenericException: No current point for control point bounds at -[NSBezierPath controlPointBounds] + 60 and -[NSBezierPath bounds] + 68, the same two frames and offsets as the crash report.
  • testUnderlineEmphasisWithoutRectsDrawsNothing fails cleanly against the unfixed code (it leaves two layers where there should be none).
  • testUnderlineEmphasisWithRectsKeepsDrawing is the control: an emphasis with real rects still draws both layers.
  • NSBezierPathSmoothPathTests covers the nil returns and checks a normal rectangle still yields a measurable path.
  • swift test --package-path LocalPackages/CodeEditTextView: 151 tests in 15 suites pass. xcodebuild -scheme TablePro build passes.

Left for follow-ups

Found while verifying, all reported as issues rather than fixed here:

  • TextView+NSTextInput.swift:241/243/263. The method's own doc comment says the range will be out of bounds, and both calls raise instead of clamping. Reachable by any IME, dictation or character viewer user.
  • TextLine.prepareForDisplay slices the text storage with a line range that transiently outruns it, 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. Plus Highlighter.setAttributes, the missing lower bound in TextView+Accessibility, and the minimap's unguarded attribute reads.
  • Behaviour, not a crash: an emphasis whose range stops laying out keeps painting its last path. That is pre-existing for .standard and this change extends it to .underline. Clearing the layer instead would flicker on every attribute edit, so it wants its own change.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant