[cfe-dev] [help wanted] Implemented a simple Clang Static Checker, but finished executing after only finding one bug

Artem Dergachev via cfe-dev cfe-dev at lists.llvm.org
Tue Sep 10 11:52:05 PDT 2019


Hi,

Your code looks correct and i can't reproduce the problem that you're 
describing. In particular, for me your checker warns on both lines of 
the following sample code:


   void foo(int x, int y) {
     x + y;
     y + x;
   }


Here's how the output looks like for me:


   test.c:2:5: warning: two integer add may overflow
     x + y;
     ~~^~~
   test.c:3:5: warning: two integer add may overflow
     y + x;
     ~~^~~
   2 warnings generated.


What is your test case that you're having problems with?


On 9/4/19 9:02 PM, JunDong Xie via cfe-dev wrote:
> Hello everyone!
> I implemented a simple Clang static analysis checker to detect if there are two integer variables added in the program, because adding two integers may overflow. However, during the execution of the checker, it reports immediately to the user once it encounters the first bug, and then exits. I want it to continue to run and report all bugs. Refer to this link http://clang-analyzer.llvm.org/checker_dev_manual.html#links , I used the addTransition function to generate an ExplodedNode and pass it to the BugReport constructor, but it didn't work. I also tried the generateNonFatalErrorNode function and it didn't work too. Can someone help me to see where is the problem? Thanks very much! Below is the code related to my error report
> ```
> ExplodedNode *N = C.generateNonFatalErrorNode();
>    if (!N)
>      return;
>    auto R = std::make_unique<BugReport>(*BT, BT->getName(), N);
>    R->addRange(binOp->getSourceRange());
>    C.emitReport(std::move(R));
> ```
>
> Below is the whole code of my project;
> ```
> using namespace clang;
> using namespace ento;
>
>
> bool IsPureIntType(Expr *expr);
> bool IsPureIntType(Expr *expr)
> {
>    if (expr->getType()->isIntegerType() && !expr->getType()->isEnumeralType() && !expr->getType()->isBooleanType())
>    {
>      return true;
>    }
>    return false;
> }
>
> namespace
> {
>
> class IntegerAddOverFlowChecker : public Checker<check::PreStmt<BinaryOperator>>
> {
>    mutable std::unique_ptr<BugType> BT;
>
>    public:
>      IntegerAddOverFlowChecker();
>      void checkPreStmt(const BinaryOperator *binOp, CheckerContext &C) const;
>    };
> } // namespace
>
> IntegerAddOverFlowChecker::IntegerAddOverFlowChecker() {
>      BT.reset(new BuiltinBug(this, "two integer add may overflow!"));
> }
>
> void IntegerAddOverFlowChecker::checkPreStmt(const BinaryOperator *binOp, CheckerContext &C) const
> {
>    if (binOp->getOpcode() != BinaryOperatorKind::BO_AddAssign && binOp->getOpcode() != BinaryOperatorKind::BO_Add)
>    {
>      return;
>    }
>
>    if (binOp->getOpcode() == BinaryOperatorKind::BO_AddAssign)
>    {
>
>      if (!IsPureIntType(binOp->getRHS()))
>      {
>        return;
>      }
>
>      if (!IsPureIntType(binOp->getLHS()))
>      {
>        return;
>      }
>
>      SVal RV = C.getSVal(binOp->getRHS());
>      if (RV.isConstant())
>      {
>        return;
>      }
>    }
>    else if (binOp->getOpcode() == BinaryOperatorKind::BO_Add)
>    {
>      if (!IsPureIntType(binOp->getRHS()))
>      {
>        return;
>      }
>
>      if (!IsPureIntType(binOp->getLHS()))
>      {
>        return;
>      }
>
>      SVal LV = C.getSVal(binOp->getLHS());
>      SVal RV = C.getSVal(binOp->getRHS());
>      if (LV.isConstant() || RV.isConstant())
>      {
>        return;
>      }
>    }
>    ExplodedNode *N = C.generateNonFatalErrorNode();
>    if (!N)
>      return;
>    auto R = std::make_unique<BugReport>(*BT, BT->getName(), N);
>    R->addRange(binOp->getSourceRange());
>    C.emitReport(std::move(R));
> }
>
> extern "C" void clang_registerCheckers(CheckerRegistry &registry)
> {
>    registry.addChecker<IntegerAddOverFlowChecker>("alpha.core.IntegerAddOverFlowChecker", "Checks for two integer add may cause overflow", "NULL");
> }
>
> extern "C" const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING;
> ```
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list