Fix recursion protection.

This commit is contained in:
chriseth 2021-08-19 12:14:50 +02:00
parent 4b038e3f02
commit 8926274000
2 changed files with 10 additions and 10 deletions

View File

@ -88,22 +88,21 @@ optional<u256> KnowledgeBase::valueIfKnownConstant(YulString _a)
Expression KnowledgeBase::simplify(Expression _expression) Expression KnowledgeBase::simplify(Expression _expression)
{ {
bool startedRecursion = (m_recursionCounter == 0); m_counter = 0;
ScopeGuard{[&] { if (startedRecursion) m_recursionCounter = 0; }}; return simplifyRecursively(move(_expression));
}
if (startedRecursion) Expression KnowledgeBase::simplifyRecursively(Expression _expression)
m_recursionCounter = 100; {
else if (m_recursionCounter == 1) if (m_counter++ > 100)
return _expression; return _expression;
else
--m_recursionCounter;
if (holds_alternative<FunctionCall>(_expression)) if (holds_alternative<FunctionCall>(_expression))
for (Expression& arg: std::get<FunctionCall>(_expression).arguments) for (Expression& arg: std::get<FunctionCall>(_expression).arguments)
arg = simplify(arg); arg = simplifyRecursively(arg);
if (auto match = SimplificationRules::findFirstMatch(_expression, m_dialect, m_variableValues)) if (auto match = SimplificationRules::findFirstMatch(_expression, m_dialect, m_variableValues))
return simplify(match->action().toExpression(debugDataOf(_expression))); return simplifyRecursively(match->action().toExpression(debugDataOf(_expression)));
return _expression; return _expression;
} }

View File

@ -56,10 +56,11 @@ public:
private: private:
Expression simplify(Expression _expression); Expression simplify(Expression _expression);
Expression simplifyRecursively(Expression _expression);
Dialect const& m_dialect; Dialect const& m_dialect;
std::map<YulString, AssignedValue> const& m_variableValues; std::map<YulString, AssignedValue> const& m_variableValues;
size_t m_recursionCounter = 0; size_t m_counter = 0;
}; };
} }