Skip to content

Commit 50289e3

Browse files
hannojgmeta-codesync[bot]
authored andcommitted
fix(android): fix crash with props 2.0 diffing when setting text props to null (facebook#55265)
Summary: When enabling props 2.0 diffing mechanism I noticed crashes happening when setting certain props back to `undefined` for `<Text>` components, as reported here: - facebook#55264 The reason seems to be with how we use ternaries/conditional operator in `BaseTextProps.cpp`: https://github.com/facebook/react-native/blob/b4da323c8ec5ab7fe0171196dbe8ea50db49b96e/packages/react-native/ReactCommon/react/renderer/components/text/BaseTextProps.cpp#L386-L390 The problem being that c++ tries to evaluate our expression to a common type, which here is `std::string`/`const char*`, so when we return nullptr it tries to implicitly convert to it to that, which leads to the crash. The fix is to explicitly use `folly::dynamic(nullptr)` ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [ANDROID] [FIXED] - Fix crash with setting certain `<Text>` props back to `undefined` when using `enablePropsUpdateReconciliationAndroid` feature flag Pull Request resolved: facebook#55265 Test Plan: Run reproduction code provided in issue and verify its not crashing any longer: https://github.com/user-attachments/assets/c2096c01-14a6-451a-8fc6-fe53d746e5b3 Reviewed By: lenaic Differential Revision: D91126015 Pulled By: cipolleschi fbshipit-source-id: e3a621bf188a18c46f3375e27d4d4259e72d7780
1 parent 62d89cb commit 50289e3

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

packages/react-native/ReactCommon/react/renderer/components/text/BaseTextProps.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -380,19 +380,19 @@ void BaseTextProps::appendTextAttributesProps(
380380
if (textAttributes.fontWeight != oldProps->textAttributes.fontWeight) {
381381
result["fontWeight"] = textAttributes.fontWeight.has_value()
382382
? toString(textAttributes.fontWeight.value())
383-
: nullptr;
383+
: folly::dynamic(nullptr);
384384
}
385385

386386
if (textAttributes.fontStyle != oldProps->textAttributes.fontStyle) {
387387
result["fontStyle"] = textAttributes.fontStyle.has_value()
388388
? toString(textAttributes.fontStyle.value())
389-
: nullptr;
389+
: folly::dynamic(nullptr);
390390
}
391391

392392
if (textAttributes.fontVariant != oldProps->textAttributes.fontVariant) {
393393
result["fontVariant"] = textAttributes.fontVariant.has_value()
394394
? toString(textAttributes.fontVariant.value())
395-
: nullptr;
395+
: folly::dynamic(nullptr);
396396
}
397397

398398
if (textAttributes.allowFontScaling !=
@@ -412,7 +412,7 @@ void BaseTextProps::appendTextAttributesProps(
412412
oldProps->textAttributes.dynamicTypeRamp) {
413413
result["dynamicTypeRamp"] = textAttributes.dynamicTypeRamp.has_value()
414414
? toString(textAttributes.dynamicTypeRamp.value())
415-
: nullptr;
415+
: folly::dynamic(nullptr);
416416
}
417417

418418
if (!floatEquality(
@@ -424,7 +424,7 @@ void BaseTextProps::appendTextAttributesProps(
424424
if (textAttributes.textTransform != oldProps->textAttributes.textTransform) {
425425
result["textTransform"] = textAttributes.textTransform.has_value()
426426
? toString(textAttributes.textTransform.value())
427-
: nullptr;
427+
: folly::dynamic(nullptr);
428428
}
429429

430430
if (!floatEquality(
@@ -435,29 +435,29 @@ void BaseTextProps::appendTextAttributesProps(
435435
if (textAttributes.alignment != oldProps->textAttributes.alignment) {
436436
result["textAlign"] = textAttributes.alignment.has_value()
437437
? toString(textAttributes.alignment.value())
438-
: nullptr;
438+
: folly::dynamic(nullptr);
439439
}
440440

441441
if (textAttributes.baseWritingDirection !=
442442
oldProps->textAttributes.baseWritingDirection) {
443443
result["baseWritingDirection"] =
444444
textAttributes.baseWritingDirection.has_value()
445445
? toString(textAttributes.baseWritingDirection.value())
446-
: nullptr;
446+
: folly::dynamic(nullptr);
447447
}
448448

449449
if (textAttributes.lineBreakStrategy !=
450450
oldProps->textAttributes.lineBreakStrategy) {
451451
result["lineBreakStrategyIOS"] =
452452
textAttributes.lineBreakStrategy.has_value()
453453
? toString(textAttributes.lineBreakStrategy.value())
454-
: nullptr;
454+
: folly::dynamic(nullptr);
455455
}
456456

457457
if (textAttributes.lineBreakMode != oldProps->textAttributes.lineBreakMode) {
458458
result["lineBreakModeIOS"] = textAttributes.lineBreakMode.has_value()
459459
? toString(textAttributes.lineBreakMode.value())
460-
: nullptr;
460+
: folly::dynamic(nullptr);
461461
}
462462

463463
if (textAttributes.textDecorationColor !=
@@ -470,22 +470,22 @@ void BaseTextProps::appendTextAttributesProps(
470470
result["textDecorationLine"] =
471471
textAttributes.textDecorationLineType.has_value()
472472
? toString(textAttributes.textDecorationLineType.value())
473-
: nullptr;
473+
: folly::dynamic(nullptr);
474474
}
475475

476476
if (textAttributes.textDecorationStyle !=
477477
oldProps->textAttributes.textDecorationStyle) {
478478
result["textDecorationStyle"] =
479479
textAttributes.textDecorationStyle.has_value()
480480
? toString(textAttributes.textDecorationStyle.value())
481-
: nullptr;
481+
: folly::dynamic(nullptr);
482482
}
483483

484484
if (textAttributes.textShadowOffset !=
485485
oldProps->textAttributes.textShadowOffset) {
486486
result["textShadowOffset"] = textAttributes.textShadowOffset.has_value()
487487
? toDynamic(textAttributes.textShadowOffset.value())
488-
: nullptr;
488+
: folly::dynamic(nullptr);
489489
}
490490

491491
if (!floatEquality(
@@ -515,13 +515,13 @@ void BaseTextProps::appendTextAttributesProps(
515515
oldProps->textAttributes.accessibilityRole) {
516516
result["accessibilityRole"] = textAttributes.accessibilityRole.has_value()
517517
? toString(textAttributes.accessibilityRole.value())
518-
: nullptr;
518+
: folly::dynamic(nullptr);
519519
}
520520

521521
if (textAttributes.role != oldProps->textAttributes.role) {
522522
result["role"] = textAttributes.role.has_value()
523523
? toString(textAttributes.role.value())
524-
: nullptr;
524+
: folly::dynamic(nullptr);
525525
}
526526

527527
if (!floatEquality(

packages/react-native/ReactCommon/react/renderer/components/text/platform/android/react/renderer/components/text/HostPlatformParagraphProps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ folly::dynamic HostPlatformParagraphProps::getDiffProps(
160160
result["textAlignVertical"] =
161161
paragraphAttributes.textAlignVertical.has_value()
162162
? toString(paragraphAttributes.textAlignVertical.value())
163-
: nullptr;
163+
: folly::dynamic(nullptr);
164164
}
165165

166166
if (isSelectable != oldProps->isSelectable) {

0 commit comments

Comments
 (0)