Random PDFKit crash on text selection

At this line of code (SketchTextSelectionManager.swift:378), sometimes there will be crashes based on crashlytics reports. In the reports, it seems like this only happens for RTL text range.

let selection = pdfPage.selection(
            from: CGPoint(x: fromStart.x + 1, y: fromStart.y - 1),
            to: CGPoint(x: toEnd.x - 1, y: toEnd.y + 1)
          )

This is directly calling into PDFKit's PDFPage#selection method: https://developer.apple.com/documentation/pdfkit/pdfpage/selection(from:to:)

Attached the full stacktrace:

  Crashed: com.apple.root.user-initiated-qos.cooperative
0  CoreGraphics                   0x30598c PageLayout::convertRTLTextRangeIndexToStringRangeIndex(long) const + 156
1  CoreGraphics                   0x44c3f0 CGPDFSelectionCreateBetweenPointsWithOptions + 224
2  PDFKit                         0x91d00 -[PDFPage selectionFromPoint:toPoint:type:] + 168
3  MyApp                       0x841044 closure #1 in SketchTextSelectionManager.handleUserTouchMoved(_:) + 378 (SketchTextSelectionManager.swift:378)
4  MyApp                       0x840cb0 SketchTextSelectionManager.handleUserTouchMoved(_:) + 205 (CurrentNoteManager.swift:205)
5  libswift_Concurrency.dylib     0x60f5c swift::runJobInEstablishedExecutorContext(swift::Job*) + 252
6  libswift_Concurrency.dylib     0x63a28 (anonymous namespace)::ProcessOutOfLineJob::process(swift::Job*) + 480
7  libswift_Concurrency.dylib     0x6101c swift::runJobInEstablishedExecutorContext(swift::Job*) + 444
8  libswift_Concurrency.dylib     0x62514 swift_job_runImpl(swift::Job*, swift::SerialExecutorRef) + 144
9  libdispatch.dylib              0x15ec0 _dispatch_root_queue_drain + 392
10 libdispatch.dylib              0x166c4 _dispatch_worker_thread2 + 156
11 libsystem_pthread.dylib        0x3644 _pthread_wqthread + 228
12 libsystem_pthread.dylib        0x1474 start_wqthread + 8
Random PDFKit crash on text selection
 
 
Q