Skip to content

Commit 4ade5d1

Browse files
committed
fix(ios): prevent crash in SQL syntax highlighter with stale range
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
1 parent ecda976 commit 4ade5d1

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

TableProMobile/TableProMobile/Views/Components/SQLSyntaxHighlighter.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,22 @@ enum SQLSyntaxHighlighter {
6464
static func highlight(_ textStorage: NSTextStorage, in editedRange: NSRange) {
6565
let fullLength = textStorage.length
6666
guard fullLength > 0 else { return }
67+
guard editedRange.location < fullLength else { return }
6768

6869
let cappedLength = min(fullLength, maxHighlightLength)
6970
let nsString = textStorage.string as NSString
7071

72+
let safeEditedRange = NSRange(
73+
location: editedRange.location,
74+
length: min(editedRange.length, fullLength - editedRange.location)
75+
)
76+
7177
let highlightRange: NSRange
72-
if editedRange.location == 0 && editedRange.length >= cappedLength {
78+
if safeEditedRange.location == 0 && safeEditedRange.length >= cappedLength {
7379
highlightRange = NSRange(location: 0, length: cappedLength)
7480
} else {
75-
let lineStart = nsString.lineRange(for: NSRange(location: editedRange.location, length: 0)).location
76-
let editEnd = min(NSMaxRange(editedRange), cappedLength)
81+
let lineStart = nsString.lineRange(for: NSRange(location: safeEditedRange.location, length: 0)).location
82+
let editEnd = min(NSMaxRange(safeEditedRange), cappedLength)
7783
let lineEnd = NSMaxRange(nsString.lineRange(for: NSRange(location: max(editEnd - 1, 0), length: 0)))
7884
highlightRange = NSRange(location: lineStart, length: min(lineEnd - lineStart, cappedLength - lineStart))
7985
}

0 commit comments

Comments
 (0)