From 6c83352a422ff50aa3c6d58a223cf7ee50396fb8 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Mon, 23 Feb 2026 21:41:43 -0800 Subject: [PATCH] Fix hard errors in filter parsing (#55675) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55675 `filterTypeFromString()` throws `std::invalid_argument` on unknown filter names, which crashes in `parseFilterRawValue` where it's called without try/catch. Change the return type to `std::optional` and return `std::nullopt` instead of throwing. Update callers in `FilterPropsConversions.h` to check the optional and gracefully return empty results. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D94052729 fbshipit-source-id: 5d5976eb9ef3ce0a37d9dcd067c2bb85df89c67f --- .../components/view/FilterPropsConversions.h | 116 +++++++++--------- .../components/view/tests/ConversionsTest.cpp | 11 ++ .../react/renderer/graphics/Filter.h | 5 +- 3 files changed, 74 insertions(+), 58 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/FilterPropsConversions.h b/packages/react-native/ReactCommon/react/renderer/components/view/FilterPropsConversions.h index 0877a749a2d..264ea7d0c45 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/FilterPropsConversions.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/FilterPropsConversions.h @@ -45,64 +45,64 @@ parseProcessedFilter(const PropsParserContext &context, const RawValue &value, s auto rawFilterFunction = static_cast>(rawFilterPrimitive); FilterFunction filterFunction{}; - try { - filterFunction.type = filterTypeFromString(rawFilterFunction.begin()->first); - if (filterFunction.type == FilterType::DropShadow) { - auto rawDropShadow = static_cast>(rawFilterFunction.begin()->second); - DropShadowParams dropShadowParams{}; - - auto offsetX = rawDropShadow.find("offsetX"); - react_native_expect(offsetX != rawDropShadow.end()); - if (offsetX == rawDropShadow.end()) { - result = {}; - return; - } - - react_native_expect(offsetX->second.hasType()); - if (!offsetX->second.hasType()) { - result = {}; - return; - } - dropShadowParams.offsetX = (Float)offsetX->second; - - auto offsetY = rawDropShadow.find("offsetY"); - react_native_expect(offsetY != rawDropShadow.end()); - if (offsetY == rawDropShadow.end()) { - result = {}; - return; - } - react_native_expect(offsetY->second.hasType()); - if (!offsetY->second.hasType()) { - result = {}; - return; - } - dropShadowParams.offsetY = (Float)offsetY->second; - - auto standardDeviation = rawDropShadow.find("standardDeviation"); - if (standardDeviation != rawDropShadow.end()) { - react_native_expect(standardDeviation->second.hasType()); - if (!standardDeviation->second.hasType()) { - result = {}; - return; - } - dropShadowParams.standardDeviation = (Float)standardDeviation->second; - } - - auto color = rawDropShadow.find("color"); - if (color != rawDropShadow.end()) { - fromRawValue(context.contextContainer, context.surfaceId, color->second, dropShadowParams.color); - } - - filterFunction.parameters = dropShadowParams; - } else { - filterFunction.parameters = (float)rawFilterFunction.begin()->second; - } - filter.push_back(std::move(filterFunction)); - } catch (const std::exception &e) { - LOG(ERROR) << "Could not parse FilterFunction: " << e.what(); + auto filterType = filterTypeFromString(rawFilterFunction.begin()->first); + if (!filterType.has_value()) { + LOG(ERROR) << "Could not parse FilterFunction: " << rawFilterFunction.begin()->first; result = {}; return; } + filterFunction.type = *filterType; + if (filterFunction.type == FilterType::DropShadow) { + auto rawDropShadow = static_cast>(rawFilterFunction.begin()->second); + DropShadowParams dropShadowParams{}; + + auto offsetX = rawDropShadow.find("offsetX"); + react_native_expect(offsetX != rawDropShadow.end()); + if (offsetX == rawDropShadow.end()) { + result = {}; + return; + } + + react_native_expect(offsetX->second.hasType()); + if (!offsetX->second.hasType()) { + result = {}; + return; + } + dropShadowParams.offsetX = (Float)offsetX->second; + + auto offsetY = rawDropShadow.find("offsetY"); + react_native_expect(offsetY != rawDropShadow.end()); + if (offsetY == rawDropShadow.end()) { + result = {}; + return; + } + react_native_expect(offsetY->second.hasType()); + if (!offsetY->second.hasType()) { + result = {}; + return; + } + dropShadowParams.offsetY = (Float)offsetY->second; + + auto standardDeviation = rawDropShadow.find("standardDeviation"); + if (standardDeviation != rawDropShadow.end()) { + react_native_expect(standardDeviation->second.hasType()); + if (!standardDeviation->second.hasType()) { + result = {}; + return; + } + dropShadowParams.standardDeviation = (Float)standardDeviation->second; + } + + auto color = rawDropShadow.find("color"); + if (color != rawDropShadow.end()) { + fromRawValue(context.contextContainer, context.surfaceId, color->second, dropShadowParams.color); + } + + filterFunction.parameters = dropShadowParams; + } else { + filterFunction.parameters = (float)rawFilterFunction.begin()->second; + } + filter.push_back(filterFunction); } result = filter; @@ -313,11 +313,15 @@ inline std::optional parseFilterRawValue(const PropsParserContex } return {}; } else { + auto filterType = filterTypeFromString(filterKey); + if (!filterType.has_value()) { + return {}; + } if (auto amount = coerceAmount(rawFilter.begin()->second)) { if (*amount < 0.0f) { return {}; } - return FilterFunction{.type = filterTypeFromString(filterKey), .parameters = *amount}; + return FilterFunction{.type = *filterType, .parameters = *amount}; } return {}; } diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/tests/ConversionsTest.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/tests/ConversionsTest.cpp index b54b60430f7..8c286f20694 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/tests/ConversionsTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/tests/ConversionsTest.cpp @@ -239,4 +239,15 @@ TEST(ConversionsTest, unprocessed_filter_objects_multiple_objects) { EXPECT_TRUE(filters.empty()); } +TEST(ConversionsTest, unprocessed_filter_objects_unknown_type) { + RawValue value{ + folly::dynamic::array(folly::dynamic::object("unknown-filter", 5))}; + + std::vector filters; + parseUnprocessedFilter( + PropsParserContext{-1, ContextContainer{}}, value, filters); + + EXPECT_TRUE(filters.empty()); +} + } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/Filter.h b/packages/react-native/ReactCommon/react/renderer/graphics/Filter.h index bbc03873ef2..f4c1c261308 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/Filter.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/Filter.h @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -29,7 +30,7 @@ enum class FilterType { DropShadow }; -inline FilterType filterTypeFromString(std::string_view filterName) +inline std::optional filterTypeFromString(std::string_view filterName) { if (filterName == "blur") { return FilterType::Blur; @@ -52,7 +53,7 @@ inline FilterType filterTypeFromString(std::string_view filterName) } else if (filterName == "dropShadow") { return FilterType::DropShadow; } else { - throw std::invalid_argument(std::string(filterName)); + return std::nullopt; } }