From 81810f4c626fe01dc8d6da1353686bbf2b602e64 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Wed, 10 Feb 2021 16:00:18 -0800 Subject: [PATCH] Add invariant to SurfaceMountingManager: when removing a view, the parent View must be a ViewGroup Summary: In addViewAt, we have this check. Add this same check to removeViewAt. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D26381475 fbshipit-source-id: 1050377aa4e528668446fd561ff09c61f27c700f --- .../mounting/SurfaceMountingManager.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java index 36d69b2dfce..a01798d0bfd 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java @@ -318,10 +318,10 @@ public class SurfaceMountingManager { } UiThreadUtil.assertOnUiThread(); - ViewState viewState = getNullableViewState(parentTag); + ViewState parentViewState = getNullableViewState(parentTag); // TODO: throw exception here? - if (viewState == null) { + if (parentViewState == null) { ReactSoftException.logSoftException( MountingManager.TAG, new IllegalStateException( @@ -329,7 +329,19 @@ public class SurfaceMountingManager { return; } - final ViewGroup parentView = (ViewGroup) viewState.mView; + if (!(parentViewState.mView instanceof ViewGroup)) { + String message = + "Unable to remove a view from a view that is not a ViewGroup. ParentTag: " + + parentTag + + " - Tag: " + + tag + + " - Index: " + + index; + FLog.e(TAG, message); + throw new IllegalStateException(message); + } + + final ViewGroup parentView = (ViewGroup) parentViewState.mView; if (parentView == null) { throw new IllegalStateException("Unable to find view for tag [" + parentTag + "]"); @@ -341,7 +353,7 @@ public class SurfaceMountingManager { logViewHierarchy(parentView, false); } - ViewGroupManager viewGroupManager = getViewGroupManager(viewState); + ViewGroupManager viewGroupManager = getViewGroupManager(parentViewState); // Verify that the view we're about to remove has the same tag we expect View view = viewGroupManager.getChildAt(parentView, index);