fix(macOS): prevent Replace crash when search highlight ranges are stale after body edits#247
Draft
cursor[bot] wants to merge 1 commit into
Draft
fix(macOS): prevent Replace crash when search highlight ranges are stale after body edits#247cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
Find bar only reposts highlights when the query changes, not on every body edit. Debounced restyle skips out-of-bounds cached ranges in reapplySearchHighlights but leaves lastSearchHighlightRanges unchanged, so Replace could call replaceCharacters with an invalid NSRange and abort the app. Resync highlights before replace when the cached range no longer fits the storage, and add a regression test. Co-authored-by: Danny Peck <dannypeck@gmail.com>
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.
Bug and impact
With the find bar open, editing the note body (without changing the search query) leaves
lastSearchHighlightRangespopulated from the lastscrollToSearchMatch. Debounced markdown restyle callsreapplySearchHighlights(), which skips out-of-bounds ranges but does not shrink or refresh that array. Replace then calledNSTextStorage.replaceCharacters(in:with:)with a staleNSRange, raisingNSRangeExceptionand terminating the app.Root cause
Highlight bookkeeping assumed ranges stay valid until the next explicit highlight pass. Body edits can invalidate ranges without updating
lastSearchHighlightRanges;reapplySearchHighlightsonly guardsaddAttributeand does not reconcile the cached array.Fix
Before replacing, if the cached range no longer fits the storage length, call
applySearchHighlights(query:focusIndex:)to recompute matches from the live document, then bail if there is still no valid range at the requested index.Validation
test_replaceCurrentMatch_doesNotCrashWhenCachedHighlightRangeIsStaleAfterBodyEditinEditorViewPendingStateConsumptionTests.swift(repro: search → full-body replace via storage → Replace notification).Synapsetests on macOS.