Skip to content

Commit 2f9711d

Browse files
alanleedevmeta-codesync[bot]
authored andcommitted
Fix race causing NPE in AccessibilityInfoModule event emission (#55792)
Summary: Pull Request resolved: #55792 Fixing this reported crash: ``` android:java.lang.NullPointerException:com.facebook.react.modules.accessibilityinfo.AccessibilityInfoModule.updateAndSendTouchExplorationChangeEvent:unknown ``` Prevent `NullPointerException` crashes by removing a TOCTOU (Time-Of-Check to Time-Of-Use) race when emitting accessibility state change events. The React context can become invalid between check and use; using a safe call on `getReactApplicationContextIfActiveOrWarn` ensures events are only sent when the context is active. Changelog: [Internal] --- Reviewed By: mdvacca Differential Revision: D94558522 fbshipit-source-id: 46edbf745b482e7d212ae5f0ed7a9fa3db7c7ba2
1 parent 703fe8b commit 2f9711d

1 file changed

Lines changed: 4 additions & 10 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/accessibilityinfo/AccessibilityInfoModule.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,22 +211,16 @@ internal class AccessibilityInfoModule(context: ReactApplicationContext) :
211211
private fun updateAndSendTouchExplorationChangeEvent(enabled: Boolean) {
212212
if (touchExplorationEnabled != enabled) {
213213
touchExplorationEnabled = enabled
214-
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
215-
if (reactApplicationContext != null) {
216-
getReactApplicationContext()
217-
.emitDeviceEvent(TOUCH_EXPLORATION_EVENT_NAME, touchExplorationEnabled)
218-
}
214+
getReactApplicationContextIfActiveOrWarn()
215+
?.emitDeviceEvent(TOUCH_EXPLORATION_EVENT_NAME, touchExplorationEnabled)
219216
}
220217
}
221218

222219
private fun updateAndSendAccessibilityServiceChangeEvent(enabled: Boolean) {
223220
if (accessibilityServiceEnabled != enabled) {
224221
accessibilityServiceEnabled = enabled
225-
val reactApplicationContext = getReactApplicationContextIfActiveOrWarn()
226-
if (reactApplicationContext != null) {
227-
getReactApplicationContext()
228-
.emitDeviceEvent(ACCESSIBILITY_SERVICE_EVENT_NAME, accessibilityServiceEnabled)
229-
}
222+
getReactApplicationContextIfActiveOrWarn()
223+
?.emitDeviceEvent(ACCESSIBILITY_SERVICE_EVENT_NAME, accessibilityServiceEnabled)
230224
}
231225
}
232226

0 commit comments

Comments
 (0)