Skip to content

Commit

Permalink
add limit to number of iternations
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Boaz Cahlon authored and facebook-github-bot committed Oct 17, 2024
1 parent 39bd23c commit c3a8dab
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions litho-flexlayout/src/main/cpp/flexlayout/FlexLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <algorithm>
#include <functional>
#include <numeric>
#include <stdexcept>

namespace facebook {
namespace flexlayout {
Expand Down Expand Up @@ -38,6 +39,8 @@ struct Item {
};
} // namespace

static constexpr auto kMaxIterations = 10000;

static auto calculateRemainingFreeSpace(
const std::vector<Item>& items,
const FlexDirection mainAxis,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c3a8dab

Please sign in to comment.