Skip to content

Commit 363d297

Browse files
rozelemeta-codesync[bot]
authored andcommitted
Mitigate int overflow in findNextFocusableElement (facebook#54291)
Summary: Pull Request resolved: facebook#54291 We are computing a weighted distance by squaring the major and minor axis distance. For particularly long lists, this means that items that are quite far away have a tendency to overflow the int value when squared, producing a negative number that becomes the smallest weighted distance. Switching the computed value to double should mitigate the issue, since MAX_DOUBLE >>> MAX_INT ^ 2. ## Changelog [Android][Fixed] - Resolves an int overflow in findNextFocusableElement Reviewed By: martinbooth Differential Revision: D85598005 fbshipit-source-id: 2a5de4e10c21f3d42ae5872d311e714b3f92efad
1 parent d9019a0 commit 363d297

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

packages/react-native/ReactAndroid/src/main/jni/react/fabric/FocusOrderingHelper.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ int minorAxisDistance(FocusDirection direction, Rect source, Rect dest) {
5555
// 13 is a magic number that comes from Android's implementation. We opt to use
5656
// this to get the same focus ordering as Android. See:
5757
// https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/java/android/view/FocusFinder.java;l=547
58-
int getWeightedDistanceFor(int majorAxisDistance, int minorAxisDistance) {
59-
return 13 * majorAxisDistance * majorAxisDistance +
58+
double getWeightedDistanceFor(
59+
double majorAxisDistance,
60+
double minorAxisDistance) {
61+
return 13.0 * majorAxisDistance * majorAxisDistance +
6062
minorAxisDistance * minorAxisDistance;
6163
}
6264

@@ -113,8 +115,8 @@ bool isBetterCandidate(
113115
return false;
114116
}
115117

116-
int candidateWeightedDistance = 0;
117-
int currentWeightedDistance = 0;
118+
double candidateWeightedDistance = 0;
119+
double currentWeightedDistance = 0;
118120

119121
switch (focusDirection) {
120122
case FocusDirection::FocusLeft:

0 commit comments

Comments
 (0)