diff --git a/litho-flexlayout/src/main/cpp/flexlayout/FlexLine.cpp b/litho-flexlayout/src/main/cpp/flexlayout/FlexLine.cpp index 10ba58c7cab..b6b3c114f30 100644 --- a/litho-flexlayout/src/main/cpp/flexlayout/FlexLine.cpp +++ b/litho-flexlayout/src/main/cpp/flexlayout/FlexLine.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace facebook { namespace flexlayout { @@ -38,6 +39,8 @@ struct Item { }; } // namespace +static constexpr auto kMaxIterations = 10000; + static auto calculateRemainingFreeSpace( const std::vector& items, const FlexDirection mainAxis, @@ -151,7 +154,12 @@ auto FlexLine::resolveFlexibleLengths( // 4. Loop: // a. Check for flexible items. If all the flex items on the line are // frozen, free space has been distributed; exit this loop. + auto numIterations = 0; while (any_of(items, [](const Item& item) { return !item.isFrozen; })) { + ++numIterations; + if (numIterations > kMaxIterations) { + throw std::logic_error("Too many iterations: exceeded 10000 iterations"); + } // Calculate the remaining free space as for initial free space, above. auto remainingFreeSpace = calculateRemainingFreeSpace( items,