From 8ab46b249b8fae2c13b1fbe29ee89e8a60b144a6 Mon Sep 17 00:00:00 2001 From: Rajdeep Kwatra Date: Fri, 14 Feb 2025 15:52:55 +1100 Subject: [PATCH] Fixed a crash that may result if layoutManager and textStorage happens to be out of sync --- Proton/Sources/Swift/Core/RichTextView.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Proton/Sources/Swift/Core/RichTextView.swift b/Proton/Sources/Swift/Core/RichTextView.swift index b5503dde..dd046f69 100644 --- a/Proton/Sources/Swift/Core/RichTextView.swift +++ b/Proton/Sources/Swift/Core/RichTextView.swift @@ -829,7 +829,11 @@ class RichTextView: AutogrowingTextView { } override func caretRect(for position: UITextPosition) -> CGRect { - guard isEditable else { + // layoutManager.numberOfGlyphs check is required to prevent cases where selectedRange is accessed + // while TextProcessor is processing and contents are changed to prevent a crash that may result when + // this is not same as textStorage.length + guard isEditable, + layoutManager.numberOfGlyphs == textStorage.length else { return super.caretRect(for: position) }