[analyzer] Check for code testing a variable for 0 after using it as a denominator.
authorJordan Rose <jordan_rose@apple.com>
Thu, 10 Jul 2014 16:10:52 +0000 (16:10 +0000)
committerJordan Rose <jordan_rose@apple.com>
Thu, 10 Jul 2014 16:10:52 +0000 (16:10 +0000)
commitdc352bb82b31251188b40201c30708976fe2c127
treec049a5948a33be60ce644ffd0400a15fcb3a5157
parent511fea7acdc360bc77978370cfbe0218738ea24c
[analyzer] Check for code testing a variable for 0 after using it as a denominator.

This new checker, alpha.core.TestAfterDivZero, catches issues like this:

  int sum = ...
  int avg = sum / count; // potential division by zero...
  if (count == 0) { ... } // ...caught here

Because the analyzer does not necessarily explore /all/ paths through a program,
this check is restricted to only work on zero checks that immediately follow a
division operation (/ % /= %=). This could later be expanded to handle checks
dominated by a division operation but not necessarily in the same CFG block.

Patch by Anders Rönnholm! (with very minor modifications by me)

llvm-svn: 212731
clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
clang/lib/StaticAnalyzer/Checkers/Checkers.td
clang/lib/StaticAnalyzer/Checkers/TestAfterDivZeroChecker.cpp [new file with mode: 0644]
clang/test/Analysis/test-after-div-zero.c [new file with mode: 0644]