From 6456563438f08f99eddcc01065b0b154be210148 Mon Sep 17 00:00:00 2001 From: Peter Abbondanzo Date: Sat, 5 Dec 2026 13:00:36 -0800 Subject: [PATCH] Guard BaseViewProps downcasts in AnimationBackend (#60524) Summary: `AnimationBackend::cloneProps` and `AnimationBackendCommitHook::shadowTreeWillCommit` previously used `std::static_pointer_cast` when cloning props for animated nodes. If a non-`BaseViewProps` node (such as `TextShadowNode`) receives an animated update, the unconditional static downcast is undefined behavior and writing typed view props overflows the `Props` object. Guard typed view-prop application in both paths with `std::dynamic_pointer_cast` and skip typed view props when the downcast is null. Changelog: [General][Fixed] - Guard `BaseViewProps` downcasts in `AnimationBackend` against non-view shadow nodes Signed-off-by: Peter Abbondanzo Reviewed By: javache Differential Revision: D136090146 --- .../animationbackend/AnimationBackend.cpp | 11 ++-- .../AnimationBackendCommitHook.cpp | 12 ++-- .../tests/AnimationBackendTest.cpp | 64 +++++++++++++++++++ 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp index 4e461d22663..99d21997c4c 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp @@ -46,10 +46,13 @@ static inline Props::Shared cloneProps( propsParserContext, shadowNode.getProps(), {}); } - auto viewProps = std::const_pointer_cast( - std::static_pointer_cast(newProps)); - for (auto& animatedProp : animatedProps.props) { - cloneProp(*viewProps, *animatedProp); + if (!animatedProps.props.empty()) { + if (auto viewProps = std::const_pointer_cast( + std::dynamic_pointer_cast(newProps))) { + for (auto& animatedProp : animatedProps.props) { + cloneProp(*viewProps, *animatedProp); + } + } } return newProps; } diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp index f60c9a997d8..909da3f3681 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp @@ -61,12 +61,16 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit( newProps = shadowNode.getComponentDescriptor().cloneProps( propsParserContext, shadowNode.getProps(), {}); } - viewProps = std::const_pointer_cast( - std::static_pointer_cast(newProps)); + if (!snapshot->propNames.empty()) { + viewProps = std::const_pointer_cast( + std::dynamic_pointer_cast(newProps)); + } } - for (const auto& propName : snapshot->propNames) { - updateProp(propName, *viewProps, *snapshot); + if (viewProps) { + for (const auto& propName : snapshot->propNames) { + updateProp(propName, *viewProps, *snapshot); + } } if (snapshot->text) { diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp index 38677ebb873..8516a865637 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -299,4 +300,67 @@ TEST_F(AnimationBackendTest, destroyingBackendUnregistersCommitHook) { localUiManager->setDelegate(nullptr); } +TEST_F(AnimationBackendTest, skipsTypedViewPropsForNonViewShadowNodes) { + constexpr SurfaceId kSurfaceId = 3; + std::shared_ptr textNode; + std::shared_ptr initialRoot; + builder.build( + Element() + .surfaceId(kSurfaceId) + .tag(1) + .reference(initialRoot) + .children({Element() + .surfaceId(kSurfaceId) + .tag(20) + .reference(textNode)})); + + auto localUiManager = std::make_shared( + [](std::function&& /*callback*/) {}, + contextContainer); + RecordingUIManagerDelegate localDelegate; + localUiManager->setDelegate(&localDelegate); + + auto shadowTree = std::make_unique( + kSurfaceId, + LayoutConstraints{}, + LayoutContext{}, + *localUiManager, + *contextContainer); + auto* shadowTreePtr = shadowTree.get(); + + shadowTree->commit( + [&initialRoot](const RootShadowNode& /*oldRootShadowNode*/) { + return initialRoot; + }, + {.mountSynchronously = true}); + + localUiManager->startEmptySurface(std::move(shadowTree)); + auto localBackend = std::make_shared( + std::make_shared(), localUiManager); + + localBackend->pushAnimationMutations([&textNode](AnimationTimestamp /*ts*/) { + AnimatedPropsBuilder propsBuilder; + propsBuilder.setOpacity(0.5); + AnimationMutations mutations; + mutations.batch.push_back( + AnimationMutation{ + .tag = textNode->getTag(), + .family = textNode->getFamilyShared(), + .props = propsBuilder.get(), + .hasLayoutUpdates = true}); + return mutations; + }); + + auto status = shadowTreePtr->commit( + [](const RootShadowNode& oldRootShadowNode) { + return std::make_shared( + oldRootShadowNode, ShadowNodeFragment{}); + }, + {.source = ShadowTreeCommitSource::React}); + EXPECT_EQ(status, ShadowTree::CommitStatus::Succeeded); + + localUiManager->stopSurface(kSurfaceId); + localUiManager->setDelegate(nullptr); +} + } // namespace facebook::react