Skip to content

Commit 6456563

Browse files
Abbondanzofacebook-github-bot
authored andcommitted
Guard BaseViewProps downcasts in AnimationBackend (#60524)
Summary: `AnimationBackend::cloneProps` and `AnimationBackendCommitHook::shadowTreeWillCommit` previously used `std::static_pointer_cast<const BaseViewProps>` 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<const BaseViewProps>` 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 <abbo@meta.com> Reviewed By: javache Differential Revision: D136090146
1 parent 3888ad5 commit 6456563

3 files changed

Lines changed: 79 additions & 8 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ static inline Props::Shared cloneProps(
4646
propsParserContext, shadowNode.getProps(), {});
4747
}
4848

49-
auto viewProps = std::const_pointer_cast<BaseViewProps>(
50-
std::static_pointer_cast<const BaseViewProps>(newProps));
51-
for (auto& animatedProp : animatedProps.props) {
52-
cloneProp(*viewProps, *animatedProp);
49+
if (!animatedProps.props.empty()) {
50+
if (auto viewProps = std::const_pointer_cast<BaseViewProps>(
51+
std::dynamic_pointer_cast<const BaseViewProps>(newProps))) {
52+
for (auto& animatedProp : animatedProps.props) {
53+
cloneProp(*viewProps, *animatedProp);
54+
}
55+
}
5356
}
5457
return newProps;
5558
}

‎packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,16 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit(
6161
newProps = shadowNode.getComponentDescriptor().cloneProps(
6262
propsParserContext, shadowNode.getProps(), {});
6363
}
64-
viewProps = std::const_pointer_cast<BaseViewProps>(
65-
std::static_pointer_cast<const BaseViewProps>(newProps));
64+
if (!snapshot->propNames.empty()) {
65+
viewProps = std::const_pointer_cast<BaseViewProps>(
66+
std::dynamic_pointer_cast<const BaseViewProps>(newProps));
67+
}
6668
}
6769

68-
for (const auto& propName : snapshot->propNames) {
69-
updateProp(propName, *viewProps, *snapshot);
70+
if (viewProps) {
71+
for (const auto& propName : snapshot->propNames) {
72+
updateProp(propName, *viewProps, *snapshot);
73+
}
7074
}
7175

7276
if (snapshot->text) {

‎packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <react/renderer/animationbackend/AnimationBackend.h>
1111
#include <react/renderer/components/root/RootComponentDescriptor.h>
1212
#include <react/renderer/components/root/RootShadowNode.h>
13+
#include <react/renderer/components/text/TextShadowNode.h>
1314
#include <react/renderer/components/view/ViewShadowNode.h>
1415
#include <react/renderer/element/ComponentBuilder.h>
1516
#include <react/renderer/element/Element.h>
@@ -299,4 +300,67 @@ TEST_F(AnimationBackendTest, destroyingBackendUnregistersCommitHook) {
299300
localUiManager->setDelegate(nullptr);
300301
}
301302

303+
TEST_F(AnimationBackendTest, skipsTypedViewPropsForNonViewShadowNodes) {
304+
constexpr SurfaceId kSurfaceId = 3;
305+
std::shared_ptr<TextShadowNode> textNode;
306+
std::shared_ptr<RootShadowNode> initialRoot;
307+
builder.build(
308+
Element<RootShadowNode>()
309+
.surfaceId(kSurfaceId)
310+
.tag(1)
311+
.reference(initialRoot)
312+
.children({Element<TextShadowNode>()
313+
.surfaceId(kSurfaceId)
314+
.tag(20)
315+
.reference(textNode)}));
316+
317+
auto localUiManager = std::make_shared<UIManager>(
318+
[](std::function<void(jsi::Runtime&)>&& /*callback*/) {},
319+
contextContainer);
320+
RecordingUIManagerDelegate localDelegate;
321+
localUiManager->setDelegate(&localDelegate);
322+
323+
auto shadowTree = std::make_unique<ShadowTree>(
324+
kSurfaceId,
325+
LayoutConstraints{},
326+
LayoutContext{},
327+
*localUiManager,
328+
*contextContainer);
329+
auto* shadowTreePtr = shadowTree.get();
330+
331+
shadowTree->commit(
332+
[&initialRoot](const RootShadowNode& /*oldRootShadowNode*/) {
333+
return initialRoot;
334+
},
335+
{.mountSynchronously = true});
336+
337+
localUiManager->startEmptySurface(std::move(shadowTree));
338+
auto localBackend = std::make_shared<AnimationBackend>(
339+
std::make_shared<TestAnimationChoreographer>(), localUiManager);
340+
341+
localBackend->pushAnimationMutations([&textNode](AnimationTimestamp /*ts*/) {
342+
AnimatedPropsBuilder propsBuilder;
343+
propsBuilder.setOpacity(0.5);
344+
AnimationMutations mutations;
345+
mutations.batch.push_back(
346+
AnimationMutation{
347+
.tag = textNode->getTag(),
348+
.family = textNode->getFamilyShared(),
349+
.props = propsBuilder.get(),
350+
.hasLayoutUpdates = true});
351+
return mutations;
352+
});
353+
354+
auto status = shadowTreePtr->commit(
355+
[](const RootShadowNode& oldRootShadowNode) {
356+
return std::make_shared<RootShadowNode>(
357+
oldRootShadowNode, ShadowNodeFragment{});
358+
},
359+
{.source = ShadowTreeCommitSource::React});
360+
EXPECT_EQ(status, ShadowTree::CommitStatus::Succeeded);
361+
362+
localUiManager->stopSurface(kSurfaceId);
363+
localUiManager->setDelegate(nullptr);
364+
}
365+
302366
} // namespace facebook::react

0 commit comments

Comments
 (0)