From c3a8dabc6dddfe1ac50a250aab64f076ebc87e9c Mon Sep 17 00:00:00 2001 From: Boaz Cahlon Date: Thu, 17 Oct 2024 13:51:03 -0700 Subject: [PATCH] add limit to number of iternations Summary: We identifly cases that this while can be infinite This cahnge both stops early and add inticative message Differential Revision: D64535095 fbshipit-source-id: ffd1139991a1a1c92e80faf9180d79555765d223 --- litho-flexlayout/src/main/cpp/flexlayout/FlexLine.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) 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,