Skip to content

fix(macOS): guard Replace against stale find highlight ranges#248

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/recent-commits-critical-bugs-5de1
Draft

fix(macOS): guard Replace against stale find highlight ranges#248
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/recent-commits-critical-bugs-5de1

Conversation

@cursor
Copy link
Copy Markdown

@cursor cursor Bot commented May 14, 2026

Bug and impact

Replace (single-match replace in the in-note find bar) could crash the macOS app with NSRangeException when the cached search highlight ranges were no longer valid for the current document length.

Root cause

reapplySearchHighlights() skips out-of-bounds ranges when repainting after markdown restyle, but it does not update lastSearchHighlightRanges. After the user edits the note (e.g. deletes text before a match), Replace still used the stale NSRange from lastSearchHighlightRanges in replaceCharacters(in:with:), which throws when the range exceeds the storage length.

Fix

  • Before replacing, require NSMaxRange(range) <= storage.length; if stale, call applySearchHighlights to resync from the live document instead of mutating text.
  • Add FindReplaceStaleHighlightTests and wire it into the Xcode project.

Validation

  • Added unit test test_replaceCurrentMatch_afterEditInvalidatesCachedRange_doesNotCrash (AppKit / XCTest; run in Xcode on macOS).
  • macOS rebuild not run in this Linux agent environment (see repo REPO_RULES.md).
Open in Web View Automation 

Replace All previously fixed stale ranges before full-document replace;
Replace Current could still call replaceCharacters with out-of-bounds
NSRanges after a manual edit left cached highlight ranges invalid.
Recompute highlights instead of crashing. Add regression test.

Co-authored-by: Danny Peck <dannypeck@gmail.com>
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