Skip to content

Commit

Permalink
fix: integer overflow in Complexity analysis
Browse files Browse the repository at this point in the history
fixes #2508
  • Loading branch information
quentin committed Sep 2, 2024
1 parent e6cc668 commit 77da4f1
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/ram/analysis/Complexity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

namespace souffle::ram::analysis {

static const int MAX_COMPLEXITY = std::numeric_limits<int>::max();

int ComplexityAnalysis::getComplexity(const Node* node) const {
// visitor
class ValueComplexityVisitor : public Visitor<int> {
Expand All @@ -39,7 +41,12 @@ int ComplexityAnalysis::getComplexity(const Node* node) const {

// conjunction
int visit_(type_identity<Conjunction>, 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
Expand All @@ -58,11 +65,16 @@ int ComplexityAnalysis::getComplexity(const Node* node) const {
}

int visit_(type_identity<Constraint>, 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<UserDefinedOperator>, const UserDefinedOperator&) override {
return std::numeric_limits<int>::max();
return MAX_COMPLEXITY;
}

// emptiness check
Expand All @@ -74,7 +86,13 @@ int ComplexityAnalysis::getComplexity(const Node* node) const {
int visit_(type_identity<AbstractOperator>, 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;
}
Expand Down

0 comments on commit 77da4f1

Please sign in to comment.