From e5c81e1c1bebd0cd6bcc9ac2ed453eec353312f8 Mon Sep 17 00:00:00 2001 From: Ahmed El-Helw Date: Thu, 23 Jun 2016 13:17:09 -0700 Subject: [PATCH] Fix a Nodes crash when removing children Summary: In manageChildren, we were assuming that the indices that were passed in to be removed were sorted, however, they weren't. This patch sorts the children to be removed. Note that it doesn't explicitly sort move, since these are sorted by the MoveProxy class. Reviewed By: astreet Differential Revision: D3474639 --- .../react/flat/FlatUIImplementation.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/flat/FlatUIImplementation.java b/ReactAndroid/src/main/java/com/facebook/react/flat/FlatUIImplementation.java index a45ddca63..a38dbdac8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/flat/FlatUIImplementation.java +++ b/ReactAndroid/src/main/java/com/facebook/react/flat/FlatUIImplementation.java @@ -12,6 +12,7 @@ package com.facebook.react.flat; import javax.annotation.Nullable; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import com.facebook.infer.annotation.Assertions; @@ -286,14 +287,27 @@ public class FlatUIImplementation extends UIImplementation { int moveFromIndex = mMoveProxy.size() - 1; int moveFromChildIndex = (moveFromIndex == -1) ? -1 : mMoveProxy.getMoveFrom(moveFromIndex); + int numToRemove = removeFrom == null ? 0 : removeFrom.size(); + int[] indicesToRemove = new int[numToRemove]; + if (numToRemove > 0) { + Assertions.assertNotNull(removeFrom); + for (int i = 0; i < numToRemove; i++) { + int indexToRemove = removeFrom.getInt(i); + indicesToRemove[i] = indexToRemove; + } + } + + // this isn't guaranteed to be sorted actually + Arrays.sort(indicesToRemove); + int removeFromIndex; int removeFromChildIndex; if (removeFrom == null) { removeFromIndex = -1; removeFromChildIndex = -1; } else { - removeFromIndex = removeFrom.size() - 1; - removeFromChildIndex = removeFrom.getInt(removeFromIndex); + removeFromIndex = indicesToRemove.length - 1; + removeFromChildIndex = indicesToRemove[removeFromIndex]; } // both moveFrom and removeFrom are already sorted, but combined order is not sorted. Use @@ -311,7 +325,7 @@ public class FlatUIImplementation extends UIImplementation { prevIndex = removeFromChildIndex; --removeFromIndex; - removeFromChildIndex = (removeFromIndex == -1) ? -1 : removeFrom.getInt(removeFromIndex); + removeFromChildIndex = (removeFromIndex == -1) ? -1 : indicesToRemove[removeFromIndex]; } else { // moveFromChildIndex == removeFromChildIndex can only be if both are equal to -1 // which means that we exhausted both arrays, and all children are removed.