Skip to content

Commit 783675c

Browse files
authored
Guard ReactEditText.onTouchEvent against framework IndexOutOfBoundsException
Summary: The Android framework `Editor` can throw `IndexOutOfBoundsException` from `TextView.onTouchEvent` while a cursor or selection handle is being dragged and the text or layout shrinks underneath it (`Editor.performCursorDrag` -> `positionCursorDuringDrag` -> `DynamicLayout.getLineStart` on a line that no longer exists). `ReactEditText.onTouchEvent` only delegates to `super`, so the exception crashes the app. Catch the exception around `super.onTouchEvent`, report it as a soft exception, and consume the event. This matches the existing guard for the same class of framework exception in `updateCachedSpannable`. Changelog: [Android][Fixed] - Prevent crash in `TextInput` when the framework throws `IndexOutOfBoundsException` while dragging the cursor handle Differential Revision: D134404457 Pull Request resolved: #60286
1 parent 9d6a249 commit 783675c

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

  • packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,14 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
361361
}
362362
}
363363
}
364-
return super.onTouchEvent(ev)
364+
return try {
365+
super.onTouchEvent(ev)
366+
} catch (e: IndexOutOfBoundsException) {
367+
// AOSP Editor can query a stale Layout line when the text shrinks during a cursor or
368+
// selection handle drag (e.g. Editor.performCursorDrag -> DynamicLayout.getLineStart).
369+
logSoftException(TAG, e)
370+
true
371+
}
365372
}
366373

367374
private fun canScrollInGestureDirection(deltaX: Float, deltaY: Float): Boolean {

0 commit comments

Comments
 (0)