From 72a78d674c5ab731dcc0ade2e9d560abe30fd068 Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Fri, 12 Apr 2024 09:21:15 -0700 Subject: [PATCH] Fix unsafe downcasting in ShadowTree::emitLayoutEvents (#44058) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44058 ## Changelog: [Internal] - This was revealed when running an ASAN build on MacOS - we were doing an unsafe downcast from `LayoutableShadowNode->ViewShadowNode` inside `ShadowTree::emitLayoutEvents`. Which, even though incidentally worked, is generally unsafe, as we may get e.g. `ImageShadowNode` there, which doesn't inherit from `ViewShadowNode`. That downcast to `ViewShadowNode` wasn't even required, to begin with, as all the needed information can be already extracted from the `LayoutableShadowNode` itself. Reviewed By: christophpurrer, javache Differential Revision: D56062334 fbshipit-source-id: 08d5b3f5e0c57dc51b051d23506c7933581fea29 --- .../ReactCommon/react/renderer/mounting/ShadowTree.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp b/packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp index 45054ef8eb1..eadd5cff364 100644 --- a/packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp @@ -575,15 +575,14 @@ void ShadowTree::emitLayoutEvents( for (const auto* layoutableNode : affectedLayoutableNodes) { // Only instances of `ViewShadowNode` (and subclasses) are supported. - const auto& viewShadowNode = - static_cast(*layoutableNode); - const auto& viewEventEmitter = - static_cast(*viewShadowNode.getEventEmitter()); + + const auto& viewEventEmitter = static_cast( + *layoutableNode->getEventEmitter()); // Checking if the `onLayout` event was requested for the particular Shadow // Node. const auto& viewProps = - static_cast(*viewShadowNode.getProps()); + static_cast(*layoutableNode->getProps()); if (!viewProps.onLayout) { continue; }