Skip to content

Commit 8926274

Browse files
committed
Fix recursion protection.
1 parent 4b038e3 commit 8926274

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

libyul/optimiser/KnowledgeBase.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,21 @@ optional<u256> KnowledgeBase::valueIfKnownConstant(YulString _a)
8888

8989
Expression KnowledgeBase::simplify(Expression _expression)
9090
{
91-
bool startedRecursion = (m_recursionCounter == 0);
92-
ScopeGuard{[&] { if (startedRecursion) m_recursionCounter = 0; }};
91+
m_counter = 0;
92+
return simplifyRecursively(move(_expression));
93+
}
9394

94-
if (startedRecursion)
95-
m_recursionCounter = 100;
96-
else if (m_recursionCounter == 1)
95+
Expression KnowledgeBase::simplifyRecursively(Expression _expression)
96+
{
97+
if (m_counter++ > 100)
9798
return _expression;
98-
else
99-
--m_recursionCounter;
10099

101100
if (holds_alternative<FunctionCall>(_expression))
102101
for (Expression& arg: std::get<FunctionCall>(_expression).arguments)
103-
arg = simplify(arg);
102+
arg = simplifyRecursively(arg);
104103

105104
if (auto match = SimplificationRules::findFirstMatch(_expression, m_dialect, m_variableValues))
106-
return simplify(match->action().toExpression(debugDataOf(_expression)));
105+
return simplifyRecursively(match->action().toExpression(debugDataOf(_expression)));
107106

108107
return _expression;
109108
}

libyul/optimiser/KnowledgeBase.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ class KnowledgeBase
5656

5757
private:
5858
Expression simplify(Expression _expression);
59+
Expression simplifyRecursively(Expression _expression);
5960

6061
Dialect const& m_dialect;
6162
std::map<YulString, AssignedValue> const& m_variableValues;
62-
size_t m_recursionCounter = 0;
63+
size_t m_counter = 0;
6364
};
6465

6566
}

0 commit comments

Comments
 (0)