Description
The app crashes with NSRangeException when:
- Deleting a mixed selection (normal text + formatted elements like bold, italic, etc.)
- Using Chinese keyboard (or other IME keyboards) and deleting text
The crash occurs in ENRMWordsUtils getAffectedWordsFromText:modificationRange: when the modificationRange exceeds the current text length, creating an invalid NSRange for rangeOfCharacterFromSet:.
Error
NSRangeException: *** -[NSTaggedPointerString rangeOfCharacterFromSet:options:range:]: Range {0, 5} out of bounds; string length 3
3 Hopick.debug.dylib 0x0000000109b8bce0 +[ENRMWordsUtils getAffectedWordsFromText:modificationRange:] + 344
4 Hopick.debug.dylib 0x0000000109b71f18 -[ENRMDetectorPipeline processTextChange:modificationRange:] + 112
5 Hopick.debug.dylib 0x0000000109b5e330 -[EnrichedMarkdownTextInput handleTextChanged] + 1872
Steps to Reproduce
- Open markdown input to write text
- Select text that includes both normal text and formatted elements (but not the whole input text)
- Press the delete key
- App crashes
Or with Chinese keyboard:
- Open markdown input
- Type text using Chinese keyboard
- Try to delete text
- App crashes
Root Cause
In EnrichedMarkdownTextInput.mm, the editLocation (from _preEditSelectedRange) can exceed the new text length after deletion. This out-of-bounds modificationRange is then passed to ENRMWordsUtils, which creates an invalid NSRange for rangeOfCharacterFromSet:.
Additionally, in ENRMWordsUtils.mm, the getAffectedWordsFromText:modificationRange: method doesn't validate the input range bounds before processing.
Proposed Fix
Two changes needed:
- In
ENRMWordsUtils.mm: Add bounds checking at the start of getAffectedWordsFromText:modificationRange: to clamp the modification range to valid text bounds:
// Clamp modification range to valid text bounds
if (range.location > textLength) {
range.location = textLength;
}
NSUInteger maxLength = textLength - range.location;
if (range.length > maxLength) {
range.length = maxLength;
}
if (range.length == 0) {
return @[];
}
- In
EnrichedMarkdownTextInput.mm: Clamp editLocation to the new text length in handleTextChanged before it's used:
NSRange preEditSelection = _preEditSelectedRange;
NSUInteger editLocation = preEditSelection.location;
// Clamp editLocation to valid bounds for the new text
if (editLocation > newLength) {
editLocation = newLength;
}
Related Issues
Related to #273
Environment
- react-native-enriched-markdown: 0.5.0
- Platform: iOS
- React Native: Latest
Description
The app crashes with
NSRangeExceptionwhen:The crash occurs in
ENRMWordsUtils getAffectedWordsFromText:modificationRange:when themodificationRangeexceeds the current text length, creating an invalidNSRangeforrangeOfCharacterFromSet:.Error
Steps to Reproduce
Or with Chinese keyboard:
Root Cause
In
EnrichedMarkdownTextInput.mm, theeditLocation(from_preEditSelectedRange) can exceed the new text length after deletion. This out-of-boundsmodificationRangeis then passed toENRMWordsUtils, which creates an invalidNSRangeforrangeOfCharacterFromSet:.Additionally, in
ENRMWordsUtils.mm, thegetAffectedWordsFromText:modificationRange:method doesn't validate the input range bounds before processing.Proposed Fix
Two changes needed:
ENRMWordsUtils.mm: Add bounds checking at the start ofgetAffectedWordsFromText:modificationRange:to clamp the modification range to valid text bounds:EnrichedMarkdownTextInput.mm: ClampeditLocationto the new text length inhandleTextChangedbefore it's used:Related Issues
Related to #273
Environment