mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Fix crash in cloneMultiple when families have no path to root (#55491)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55491 Fixes crash in `ShadowNode::cloneMultipleRecursive` which tried to access a missing root's family from the `childrenCount` map. The main issue is that in some cases there is no path between the given families to update and the root shadow node. The solution is to check after building the `childrenCount` map if it contains the root family and return `nullptr` otherwise. This behavior is closer to the `getAncestors` method which returns an empty array if there is no ancestor-descendant relationship. Due to this change, an additional check in `AnimationBackendCommitHook` is also required. ## Changelog: [General][Fixed] - Fixed crash in `cloneMultiple` when families have no path to root Reviewed By: zeyap Differential Revision: D92838809 fbshipit-source-id: a87de2c171e4f0f71e566806f5c012ab0bac69fd
This commit is contained in:
1 parent
cccc180f9d
commit
54b4cb5e89
3 files changed
+64
-3
No files matched your search
+8
-2
@@ -35,8 +35,8 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit(
|
||||
if (surfaceFamilies.empty()) {
|
||||
return newRootShadowNode;
|
||||
}
|
||||
return std::static_pointer_cast<RootShadowNode>(
|
||||
newRootShadowNode->cloneMultiple(
|
||||
auto clonedRootShadowNode =
|
||||
std::static_pointer_cast<RootShadowNode>(newRootShadowNode->cloneMultiple(
|
||||
surfaceFamilies,
|
||||
[&surfaceFamilies, &updates](
|
||||
const ShadowNode& shadowNode,
|
||||
@@ -73,6 +73,12 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit(
|
||||
.state = shadowNode.getState(),
|
||||
.runtimeShadowNodeReference = true});
|
||||
}));
|
||||
|
||||
if (clonedRootShadowNode == nullptr) {
|
||||
return newRootShadowNode;
|
||||
}
|
||||
|
||||
return clonedRootShadowNode;
|
||||
}
|
||||
|
||||
} // namespace facebook::react
|
||||
@@ -471,7 +471,7 @@ std::shared_ptr<ShadowNode> ShadowNode::cloneMultiple(
|
||||
}
|
||||
}
|
||||
|
||||
if (childrenCount.empty()) {
|
||||
if (!childrenCount.contains(&this->getFamily())) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -374,3 +374,58 @@ TEST_F(ShadowNodeTest, cloneMultipleWithSingleFamily) {
|
||||
EXPECT_EQ(newNodeABA->getTag(), nodeABA_->getTag());
|
||||
EXPECT_EQ(newNodeABA.get(), nodeABA_.get());
|
||||
}
|
||||
|
||||
TEST_F(ShadowNodeTest, cloneMultipleReturnsNullptrWhenFamilyHasNoPathToRoot) {
|
||||
auto newProps = std::make_shared<const TestProps>();
|
||||
// nodeZ_ is not part of nodeA_'s tree
|
||||
auto result = nodeA_->cloneMultiple(
|
||||
{&nodeZ_->getFamily()},
|
||||
[&](const ShadowNode& oldShadowNode, const ShadowNodeFragment& fragment) {
|
||||
return oldShadowNode.clone({
|
||||
.props = newProps,
|
||||
.children = fragment.children,
|
||||
.state = fragment.state,
|
||||
});
|
||||
});
|
||||
|
||||
// cloneMultiple should return nullptr when the family has no path to the root
|
||||
EXPECT_EQ(result, nullptr);
|
||||
}
|
||||
|
||||
TEST_F(ShadowNodeTest, cloneMultipleWithMixOfValidAndInvalidFamilies) {
|
||||
auto newProps = std::make_shared<const TestProps>();
|
||||
// nodeAB_ is in the tree, nodeZ_ is not
|
||||
auto result = nodeA_->cloneMultiple(
|
||||
{&nodeAB_->getFamily(), &nodeZ_->getFamily()},
|
||||
[&](const ShadowNode& oldShadowNode, const ShadowNodeFragment& fragment) {
|
||||
return oldShadowNode.clone({
|
||||
.props = newProps,
|
||||
.children = fragment.children,
|
||||
.state = fragment.state,
|
||||
});
|
||||
});
|
||||
|
||||
// Should still succeed and clone the valid family (nodeAB_)
|
||||
EXPECT_NE(result, nullptr);
|
||||
EXPECT_EQ(result->getTag(), nodeA_->getTag());
|
||||
EXPECT_EQ(result->getProps(), newProps);
|
||||
|
||||
auto newNodeAB = result->getChildren()[1];
|
||||
EXPECT_EQ(newNodeAB->getTag(), nodeAB_->getTag());
|
||||
EXPECT_EQ(newNodeAB->getProps(), newProps);
|
||||
}
|
||||
|
||||
TEST_F(ShadowNodeTest, cloneMultipleWithEmptyFamilySet) {
|
||||
auto newProps = std::make_shared<const TestProps>();
|
||||
auto result = nodeA_->cloneMultiple(
|
||||
{},
|
||||
[&](const ShadowNode& oldShadowNode, const ShadowNodeFragment& fragment) {
|
||||
return oldShadowNode.clone({
|
||||
.props = newProps,
|
||||
.children = fragment.children,
|
||||
.state = fragment.state,
|
||||
});
|
||||
});
|
||||
|
||||
EXPECT_EQ(result, nullptr);
|
||||
}
|
||||
Reference in new issue
Block a user