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<FilterType>` 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
This commit is contained in:
Nick Gerleman authored and meta-codesync[bot] committed 2026-02-23 21:41:43 -08:00
1 parent a04904cb84
commit 6c83352a42
3 files changed
+74 -58

No files matched your search

@@ -45,64 +45,64 @@ parseProcessedFilter(const PropsParserContext &context, const RawValue &value, s
auto rawFilterFunction = static_cast<std::unordered_map<std::string, RawValue>>(rawFilterPrimitive);
FilterFunction filterFunction{};
try {
filterFunction.type = filterTypeFromString(rawFilterFunction.begin()->first);
if (filterFunction.type == FilterType::DropShadow) {
auto rawDropShadow = static_cast<std::unordered_map<std::string, RawValue>>(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<Float>());
if (!offsetX->second.hasType<Float>()) {
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<Float>());
if (!offsetY->second.hasType<Float>()) {
result = {};
return;
}
dropShadowParams.offsetY = (Float)offsetY->second;
auto standardDeviation = rawDropShadow.find("standardDeviation");
if (standardDeviation != rawDropShadow.end()) {
react_native_expect(standardDeviation->second.hasType<Float>());
if (!standardDeviation->second.hasType<Float>()) {
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<std::unordered_map<std::string, RawValue>>(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<Float>());
if (!offsetX->second.hasType<Float>()) {
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<Float>());
if (!offsetY->second.hasType<Float>()) {
result = {};
return;
}
dropShadowParams.offsetY = (Float)offsetY->second;
auto standardDeviation = rawDropShadow.find("standardDeviation");
if (standardDeviation != rawDropShadow.end()) {
react_native_expect(standardDeviation->second.hasType<Float>());
if (!standardDeviation->second.hasType<Float>()) {
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<FilterFunction> 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 {};
}
@@ -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<FilterFunction> filters;
parseUnprocessedFilter(
PropsParserContext{-1, ContextContainer{}}, value, filters);
EXPECT_TRUE(filters.empty());
}
} // namespace facebook::react
@@ -10,6 +10,7 @@
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/Float.h>
#include <optional>
#include <string>
#include <string_view>
#include <variant>
@@ -29,7 +30,7 @@ enum class FilterType {
DropShadow
};
inline FilterType filterTypeFromString(std::string_view filterName)
inline std::optional<FilterType> 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;
}
}