Skip to content

Commit c3a8dab

Browse files
Boaz Cahlonfacebook-github-bot
authored andcommitted
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
1 parent 39bd23c commit c3a8dab

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

litho-flexlayout/src/main/cpp/flexlayout/FlexLine.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <algorithm>
66
#include <functional>
77
#include <numeric>
8+
#include <stdexcept>
89

910
namespace facebook {
1011
namespace flexlayout {
@@ -38,6 +39,8 @@ struct Item {
3839
};
3940
} // namespace
4041

42+
static constexpr auto kMaxIterations = 10000;
43+
4144
static auto calculateRemainingFreeSpace(
4245
const std::vector<Item>& items,
4346
const FlexDirection mainAxis,
@@ -151,7 +154,12 @@ auto FlexLine::resolveFlexibleLengths(
151154
// 4. Loop:
152155
// a. Check for flexible items. If all the flex items on the line are
153156
// frozen, free space has been distributed; exit this loop.
157+
auto numIterations = 0;
154158
while (any_of(items, [](const Item& item) { return !item.isFrozen; })) {
159+
++numIterations;
160+
if (numIterations > kMaxIterations) {
161+
throw std::logic_error("Too many iterations: exceeded 10000 iterations");
162+
}
155163
// Calculate the remaining free space as for initial free space, above.
156164
auto remainingFreeSpace = calculateRemainingFreeSpace(
157165
items,

0 commit comments

Comments
 (0)