proton icon indicating copy to clipboard operation
proton copied to clipboard

NSRangeException when using lists

Open YoX89 opened this issue 3 years ago • 5 comments

Hi, this is a crash that is 100% reproducible in the sample app in the "Commands" example.

  1. Tap "List" command.
  2. Tap enter.
  3. Tap "List" command again
  4. Tap enter again
  5. Crash
2021-05-14 08:34:40.949847+0200 ExampleApp[4044:79319] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSBigMutableString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range {4, 0} out of bounds; string length 3'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff20422fba __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x00007fff20193ff5 objc_exception_throw + 48
	2   Foundation                          0x00007fff2084c066 -[NSString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:] + 900
	3   Foundation                          0x00007fff2084c66a -[NSString paragraphRangeForRange:] + 75
	4   UIKitCore                           0x00007fff24a224c0 __58-[UITextInputController setBaseWritingDirection:forRange:]_block_invoke + 86
	5   UIFoundation                        0x00007fff23a5584c -[NSTextStorage coordinateEditing:] + 35
	6   UIKitCore                           0x00007fff24a22361 -[UITextInputController setBaseWritingDirection:forRange:] + 152
	7   UIKitCore                           0x00007fff24a42023 -[UITextView setBaseWritingDirection:forRange:] + 68
	8   UIKitCore                           0x00007fff244bab9b -[UIKeyboardImpl(UIKitInternal) setInitialDirection] + 294
	9   UIKitCore                           0x00007fff244a5ffd -[UIKeyboardImpl completeAddInputString:generateCandidates:] + 124
	10  UIKitCore                           0x00007fff244a5f44 __100-[UIKeyboardImpl addWordTerminator:afterSpace:afterAcceptingCandidate:elapsedTime:executionContext:]_block_invoke + 177
	11  UIKitCore                           0x00007fff244cba5d -[UIKeyboardTaskExecutionContext returnExecutionToParentWithInfo:] + 109
	12  UIKitCore                           0x00007fff2449a41c __55-[UIKeyboardImpl handleKeyboardInput:executionContext:]_block_invoke_2 + 799
	13  UIKitCore                           0x00007fff244cd2a1 -[UIKeyboardTaskEntry execute:] + 147
	14  UIKitCore                           0x00007fff244cbf35 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 310
	15  Foundation                          0x00007fff2085cd08 __NSThreadPerformPerform + 204
	16  CoreFoundation                      0x00007fff20390ede __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	17  CoreFoundation                      0x00007fff20390dd6 __CFRunLoopDoSource0 + 180
	18  CoreFoundation                      0x00007fff2039029e __CFRunLoopDoSources0 + 242
	19  CoreFoundation                      0x00007fff2038a9f7 __CFRunLoopRun + 875
	20  CoreFoundation                      0x00007fff2038a1a7 CFRunLoopRunSpecific + 567
	21  GraphicsServices                    0x00007fff2b874d85 GSEventRunModal + 139
	22  UIKitCore                           0x00007fff246c14df -[UIApplication _run] + 912
	23  UIKitCore                           0x00007fff246c639c UIApplicationMain + 101
	24  ExampleApp                          0x000000010e19c8cb main + 75
	25  libdyld.dylib                       0x00007fff2025abbd start + 1
	26  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi: terminating with uncaught exception of type NSException

YoX89 avatar May 14 '21 06:05 YoX89

I've traced it to func exitListsIfRequired(editor: EditorView, editedRange: NSRange) in ListTextProcessor.swift.

In the end of that method we set the executeOnDidUpdate closure to remove the blank line filler. editor.replaceCharacters(in: NSRange(location: currentLine.range.location + 1, length: 1), with: "")

If I update the selected range in func didProcess(editor: EditorView) to a valid range in the guard it doesn't crash anymore. But I don't know if that has any other implications.

guard editor.selectedRange.endLocation < editor.contentLength else {
    editor.selectedRange = NSRange(location: editor.contentLength, length: 0)
    return
}

YoX89 avatar May 14 '21 08:05 YoX89

thank you for reporting the issue and likely cause, @YoX89. I'll have the fix soon.

rajdeep avatar May 16 '21 08:05 rajdeep

@rajdeep - thank you for all the work you have put into this project. do you have any update on this issue?

mattli avatar Sep 09 '22 22:09 mattli

Thank you @mattli. Unfortunately, I've not been able to get to fixing this yet. I've been thinking about revising some internal details and that might help fix this issue. Thanks for highlighting this again. I'll close this issue as soon as it is fixed.

rajdeep avatar Sep 09 '22 22:09 rajdeep

Appreciate it. Thanks again.

mattli avatar Sep 09 '22 23:09 mattli

fixed as part of #166

rajdeep avatar Apr 13 '23 00:04 rajdeep