Repository navigation
fix(editor): stop the editor crashing when it redraws an emphasis whose text is gone - #2338
Merged
Merged
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This was referenced Aug 21, 2026
Closed
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.
A crash report from 0.67.0 (build 120) on macOS 27.0:
EXC_BREAKPOINTthrough+[NSApplication _crashOnException:], with an uncaughtNSGenericExceptionraised by-[NSBezierPath controlPointBounds]inside a view'sdrawRect.What happened
TextView.draw(_:)callsemphasisManager?.updateLayerBackgrounds()on every draw, with no dirty-rect or validity filter. For a.underlineemphasis,makeShapePathbuilt its path fromlayoutManager.rectsFor(range:)and returned that path even when the array was empty, so the caller got a path with zero elements.updateLayerBackgroundsthen readshapePath.boundsto position the emphasis's text layer, andboundsraisesNSGenericException("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:QueryDiagnosticsControllerkeeps.underlineemphases 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:
drawRectas a logged non-fatal. It catches it inside_NSViewDrawRectand calls+[NSApplication _crashOnException:]. Measured here:NSApplicationCrashOnExceptions NOdoes not help, and neither does linking against an older SDK (checked at deployment targets 13.0, 15.0 and 26.0).main, so it will need re-applying if the vendored fork is rebased.The fix
makeShapePathreturnsnilwhen there is nothing to draw, instead of a path with no elements.updateLayerBackgroundsalready skips anilshape.NSBezierPath.drawableBoundsreturnsnilfor an empty path. Both reads ofboundsinEmphasisManagergo through it, so drawing code can no longer reach the accessor that raises.smoothPathreturns an optional and answersnilfor fewer than two points, soroundedPathForRangepropagatesnilrather than an empty path. It also guards the one division the function did not already guard: identical first two points producedNaNcoordinates, and aNaNpoint raises onmove(to:), on.boundsand on.cgPath.createTextLayerchecks 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:)raisesNSRangeExceptionfor a range past the end, andattribute(.font, at: 0)raises for a zero-length range. The second one fired first in the reproduction run, so it ships here.Verification
testEmphasisOutlivingTheTextItMarksSurvivesRedrawadds an underline emphasis, deletes the text under it, relays out and redraws. Against the unfixed code it dies withNSGenericException: No current point for control point boundsat-[NSBezierPath controlPointBounds] + 60and-[NSBezierPath bounds] + 68, the same two frames and offsets as the crash report.testUnderlineEmphasisWithoutRectsDrawsNothingfails cleanly against the unfixed code (it leaves two layers where there should be none).testUnderlineEmphasisWithRectsKeepsDrawingis the control: an emphasis with real rects still draws both layers.NSBezierPathSmoothPathTestscovers thenilreturns 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 buildpasses.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.prepareForDisplayslices the text storage with a line range that transiently outruns it, the layout-pass twin of therectForOffsetcrash fixed in fix(editor): stop the rectForOffset crash and keep the autocomplete popup below the caret #1835. PlusHighlighter.setAttributes, the missing lower bound inTextView+Accessibility, and the minimap's unguarded attribute reads..standardand this change extends it to.underline. Clearing the layer instead would flicker on every attribute edit, so it wants its own change.