From 7c83eaba80da1e0b608a348663f5493b5d11bc69 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Sun, 4 Oct 2020 19:10:05 -0700 Subject: [PATCH] Fabric: Fixed crash in `[RCTParagraphComponentView accessibilityElements]` Summary: Now we check for `_state` being nullptr before dereferencing this. `accessibilityElements` (as a bunch of other methods that have the same check) can be called by OS any moment (we cannot predict when), so at some very rare moments, it can be called when the view is not fully initialized yet. To prevent crashing we just need to return some default value indicating that the view is empty. Changelog: [Internal] Fabric-specific internal change. Reviewed By: sammy-SC Differential Revision: D24095654 fbshipit-source-id: d8a37f269c5bf7718acb0b512e7a9313e199398e --- .../Text/RCTParagraphComponentView.mm | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm b/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm index 7756a776154..824a140c7c7 100644 --- a/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm @@ -141,16 +141,21 @@ using namespace facebook::react; - (NSArray *)accessibilityElements { - if (![_accessibilityProvider isUpToDate:_state->getData().attributedString]) { + if (!_state) { + return [NSArray new]; + } + + auto &data = _state->getData(); + + if (![_accessibilityProvider isUpToDate:data.attributedString]) { RCTTextLayoutManager *textLayoutManager = - (RCTTextLayoutManager *)unwrapManagedObject(_state->getData().layoutManager->getNativeTextLayoutManager()); + (RCTTextLayoutManager *)unwrapManagedObject(data.layoutManager->getNativeTextLayoutManager()); CGRect frame = RCTCGRectFromRect(_layoutMetrics.getContentFrame()); - _accessibilityProvider = - [[RCTParagraphComponentAccessibilityProvider alloc] initWithString:_state->getData().attributedString - layoutManager:textLayoutManager - paragraphAttributes:_state->getData().paragraphAttributes - frame:frame - view:self]; + _accessibilityProvider = [[RCTParagraphComponentAccessibilityProvider alloc] initWithString:data.attributedString + layoutManager:textLayoutManager + paragraphAttributes:data.paragraphAttributes + frame:frame + view:self]; } self.isAccessibilityElement = NO;