From 77da4f12866311d41dc34298493ddb7144c4a92b Mon Sep 17 00:00:00 2001 From: Quentin Sabah Date: Mon, 2 Sep 2024 21:02:58 +0200 Subject: [PATCH] fix: integer overflow in Complexity analysis fixes #2508 --- src/ram/analysis/Complexity.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ram/analysis/Complexity.cpp b/src/ram/analysis/Complexity.cpp index ff0eabb65c5..77498edceda 100644 --- a/src/ram/analysis/Complexity.cpp +++ b/src/ram/analysis/Complexity.cpp @@ -29,6 +29,8 @@ namespace souffle::ram::analysis { +static const int MAX_COMPLEXITY = std::numeric_limits::max(); + int ComplexityAnalysis::getComplexity(const Node* node) const { // visitor class ValueComplexityVisitor : public Visitor { @@ -39,7 +41,12 @@ int ComplexityAnalysis::getComplexity(const Node* node) const { // conjunction int visit_(type_identity, const Conjunction& conj) override { - return dispatch(conj.getLHS()) + dispatch(conj.getRHS()); + int cl = dispatch(conj.getLHS()); + int cr = dispatch(conj.getRHS()); + if (cl == MAX_COMPLEXITY || cr == MAX_COMPLEXITY) { + return MAX_COMPLEXITY; + } + return cl + cr; } // negation @@ -58,11 +65,16 @@ int ComplexityAnalysis::getComplexity(const Node* node) const { } int visit_(type_identity, const Constraint& c) override { - return dispatch(c.getLHS()) + dispatch(c.getRHS()); + int cl = dispatch(c.getLHS()); + int cr = dispatch(c.getRHS()); + if (cl == MAX_COMPLEXITY || cr == MAX_COMPLEXITY) { + return MAX_COMPLEXITY; + } + return cl + cr; } int visit_(type_identity, const UserDefinedOperator&) override { - return std::numeric_limits::max(); + return MAX_COMPLEXITY; } // emptiness check @@ -74,7 +86,13 @@ int ComplexityAnalysis::getComplexity(const Node* node) const { int visit_(type_identity, const AbstractOperator& op) override { int exprComplexity = 0; for (auto* expr : op.getArguments()) { - exprComplexity += dispatch(*expr); + const int complexity = dispatch(*expr); + if (complexity == MAX_COMPLEXITY) { + exprComplexity = MAX_COMPLEXITY; + break; + } else { + exprComplexity += dispatch(*expr); + } } return exprComplexity; }