From aeb020dfa34d06b63fa0151be302287d0a3fb84f Mon Sep 17 00:00:00 2001 From: Peter Abbondanzo Date: Fri, 19 Jul 2024 15:45:20 -0700 Subject: [PATCH] Add null check for instance manager (#45549) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45549 ## Summary Calling `getCurrentReactContext` on an unmounted `ReactRootView` could lead to a NPE. The method currently returns a nullable value so return type is the exact same. This change checks if the react instance manager is present and returns null early if not. ## Changelog: [Android] [Fixed] - Adds a null check in react context getter Reviewed By: zeyap Differential Revision: D59982179 fbshipit-source-id: bac5c12e7dc4ee3296991063eb3746141b8446bc --- .../src/main/java/com/facebook/react/ReactRootView.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java index 58570c0bcfc..9b52b1bea3d 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java @@ -817,6 +817,9 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot { @Nullable public ReactContext getCurrentReactContext() { + if (mReactInstanceManager == null) { + return null; + } return mReactInstanceManager.getCurrentReactContext(); }