PDF Text Selection Crashes When Cursor Moves Over QR Code Only on iOS 18

We are experiencing a crash when selecting text in a PDF and moving the cursor over a QR code. This happens when the QR code at the end of the PDF file with no text following it. [Only on iOS 18]

Steps to Reproduce:

  • Clone the repository:
 git clone https://github.com/aliakhtar49/ios18-pdf-crash/tree/main
cd ios18-pdf-crash
  • Run the application to load the provided PDF
  • Select some text and move the cursor over the QR code several times
  • Observe the crash. You can see the gif in the repo
  • Expected Behavior: The application should not crash

Actual Behavior: The application crashes:

Environment:

  • iOS version: 18.0
  • Device: iPhone 15 Pro, iPhone 12 Pro Max

Additional Context: This issue occurs when the QR code is at the end of the PDF file with no text following it

StackTrace

`thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x19f3e6ec0)
  * frame #0: 0x000000019f3e6ec0 CoreGraphics`void PageLayout::ConvertTextRangesToStringRanges<std::__1::span<CFRange, 18446744073709551615ul>, std::__1::back_insert_iterator<std::__1::vector<CFRange, std::__1::allocator<CFRange>>>>(std::__1::span<CFRange, 18446744073709551615ul>&&, std::__1::back_insert_iterator<std::__1::vector<CFRange, std::__1::allocator<CFRange>>>&&) const + 664
    frame #1: 0x000000019f424c5c CoreGraphics`CGPDFPageLayoutGetStringRangeIndexNearestPoint + 100
    frame #2: 0x000000022b4c096c PDFKit`-[PDFPage characterIndexNearestPoint:] + 128
    frame #3: 0x000000022b450950 PDFKit`-[PDFTextInputView _closestPositionToPoint:withinRange:] + 420
    frame #4: 0x00000001a0bd02f0 UIKitCore`-[UITextSelectionInteraction _hasTextAlternativesAtLocation:] + 120
    frame #5: 0x00000001a03e3834 UIKitCore`-[_UIRemoteKeyboardsEventObserver _hasTextAlternativesForTouch:] + 256
    frame #6: 0x000000019fa4e0f8 UIKitCore`-[_UIRemoteKeyboardsEventObserver _endTrackingForTouch:] + 212
    frame #7: 0x000000019fa4ca38 UIKitCore`-[_UIRemoteKeyboardsEventObserver _trackTouch:] + 132
    frame #8: 0x000000019fa4c94c UIKitCore`-[_UIRemoteKeyboardsEventObserver peekApplicationEvent:] + 220
    frame #9: 0x000000019fa4c304 UIKitCore`-[_UIRemoteKeyboards peekApplicationEvent:] + 84
    frame #10: 0x000000019fa35dc4 UIKitCore`__dispatchPreprocessedEventFromEventQueue + 4180
    frame #11: 0x000000019fa3ef7c UIKitCore`__processEventQueue + 5696
    frame #12: 0x000000019f936df4 UIKitCore`updateCycleEntry + 160
    frame #13: 0x000000019f934d28 UIKitCore`_UIUpdateSequenceRun + 84
    frame #14: 0x000000019f934978 UIKitCore`schedulerStepScheduledMainSection + 172
    frame #15: 0x000000019f93580c UIKitCore`runloopSourceCallback + 92
    frame #16: 0x000000019d126f9c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
    frame #17: 0x000000019d126f30 CoreFoundation`__CFRunLoopDoSource0 + 176
    frame #18: 0x000000019d124a08 CoreFoundation`__CFRunLoopDoSources0 + 244
    frame #19: 0x000000019d123c14 CoreFoundation`__CFRunLoopRun + 856
    frame #20: 0x000000019d123424 CoreFoundation`CFRunLoopRunSpecific + 608
    frame #21: 0x00000001e93211c4 GraphicsServices`GSEventRunModal + 164
    frame #22: 0x000000019fc6a130 UIKitCore`-[UIApplication _run] + 816
    frame #23: 0x000000019fd1855c UIKitCore`UIApplicationMain + 340
    frame #24: 0x0000000105e77c38 MyApp.debug.dylib`main at HSAppDelegate.swift:19:20
    frame #25: 0x00000001c3058a74 dyld`start + 2724`

Experiencing similar crash on ios 17 aswell

CGPDFPageLayoutGetStringRangeIndexNearestPoint

0 CoreGraphics 0x314bb4 CGPDFPageLayoutGetStringRangeIndexNearestPoint + 104 1 PDFKit 0x8c99c -[PDFPage characterIndexNearestPoint:] + 128 2 PDFKit 0x1cb6c -[PDFTextInputView _closestPositionToPoint:withinRange:] + 420 3 UIKitCore 0xd979e8 -[_UIKeyboardTextSelectionController updateSelectionWithExtentPoint:withBoundary:executionContext:] + 88 4 UIKitCore 0x10efeac -[UITextSelectionInteraction tapAndAHalf:] + 1356 5 UIKitCore 0x42ae7c -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 128

PDF Text Selection Crashes When Cursor Moves Over QR Code Only on iOS 18
 
 
Q