Skip to content

fix(ios): prevent crash in SQL syntax highlighter with stale range#689

Merged
datlechin merged 7 commits intomainfrom
fix/ios-crash-highlighter
Apr 11, 2026
Merged

fix(ios): prevent crash in SQL syntax highlighter with stale range#689
datlechin merged 7 commits intomainfrom
fix/ios-crash-highlighter

Conversation

@datlechin
Copy link
Copy Markdown
Collaborator

Summary

Fixes a crash in SQLSyntaxHighlighter.highlight(_:in:) that affected 5 TestFlight users.

Root cause: The highlight method is called via DispatchQueue.main.async from the NSTextStorageDelegate. By the time it executes, the text may have changed, making the editedRange parameter stale (location beyond current string length). This causes -[NSString lineRangeForRange:] to throw an out-of-bounds exception.

Fix:

  • Add guard editedRange.location < fullLength early return
  • Clamp editedRange.length to not exceed string bounds
  • Use the safe range for all subsequent line range calculations

Crash log

Exception Type: EXC_CRASH (SIGABRT)
-[NSString lineRangeForRange:] — out of bounds
SQLSyntaxHighlighter.highlight(_:in:) + 132 (SQLSyntaxHighlighter.swift:75)

5 occurrences on iPhone 15, iOS 26.4.1, Build 6.

The highlight method is called asynchronously from the text storage
delegate. By the time it executes, the text may have changed, making
the editedRange invalid (location beyond string length). Add bounds
check and clamp the range before calling lineRangeForRange.

Fixes crash: -[NSString lineRangeForRange:] out-of-bounds exception
@datlechin datlechin merged commit 9213e60 into main Apr 11, 2026
2 checks passed
@datlechin datlechin deleted the fix/ios-crash-highlighter branch April 11, 2026 18:30
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