Skip to content

Commit

Permalink
Properly handle parsing failure in FormulaEvaluator
Browse files Browse the repository at this point in the history
Fixed a segmentation fault when the FormulaEvaluator was unable
to fully parse an expression which contained multiple binary
operators.
  • Loading branch information
Dr15Jones committed Feb 26, 2018
1 parent 20de6fd commit 8b117c5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CommonTools/Utils/src/FormulaEvaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ namespace {
fullExpression.evaluator.reset();
}

if(fullExpression.evaluator == nullptr) {
//we had a parsing problem
return fullExpression;
}

//Now to handle precedence
auto topNode = fullExpression.top;
auto binaryEval = dynamic_cast<reco::formula::BinaryOperatorEvaluatorBase*>(fullExpression.evaluator.get());
Expand Down
59 changes: 59 additions & 0 deletions CommonTools/Utils/test/testFormulaEvaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "CommonTools/Utils/interface/FormulaEvaluator.h"

#include "FWCore/Utilities/interface/Exception.h"

#include <algorithm>
#include <cmath>
#include "TMath.h"
Expand Down Expand Up @@ -895,4 +897,61 @@ testFormulaEvaluator::checkFormulaEvaluator() {
}
}

{
auto t = [] () {
reco::FormulaEvaluator f("doesNotExist(2)");
};

CPPUNIT_ASSERT_THROW( t(), cms::Exception );
}

{
auto t = [] () {
reco::FormulaEvaluator f("doesNotExist(2) + abs(-1)");
};

CPPUNIT_ASSERT_THROW( t(), cms::Exception );
}

{
auto t = [] () {
reco::FormulaEvaluator f("abs(-1) + doesNotExist(2)");
};

CPPUNIT_ASSERT_THROW( t(), cms::Exception );
}

{
auto t = [] () {
reco::FormulaEvaluator f("abs(-1) + ( 5 * doesNotExist(2))");
};

CPPUNIT_ASSERT_THROW( t(), cms::Exception );
}

{
auto t = [] () {
reco::FormulaEvaluator f("( 5 * doesNotExist(2)) + abs(-1)");
};

CPPUNIT_ASSERT_THROW( t(), cms::Exception );
}

{
auto t = [] () {
reco::FormulaEvaluator f("TMath::Exp(2)");
};

CPPUNIT_ASSERT_THROW( t(), cms::Exception );
}

{
//this was previously causing a seg fault
auto t = [] () {
reco::FormulaEvaluator f("1 + 2 * 3 + 5 * doesNotExist(2) ");
};

CPPUNIT_ASSERT_THROW( t(), cms::Exception );
}

}

0 comments on commit 8b117c5

Please sign in to comment.