r369195 - [analyzer] Turn an assert into an if condition

Kristof Umann via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 17 09:49:54 PDT 2019


Author: szelethus
Date: Sat Aug 17 09:49:54 2019
New Revision: 369195

URL: http://llvm.org/viewvc/llvm-project?rev=369195&view=rev
Log:
[analyzer] Turn an assert into an if condition

Shocker, turns out that terminator conditions that are binary operators
aren't always logical operators.

Modified:
    cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
    cfe/trunk/test/Analysis/track-control-dependency-conditions.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp?rev=369195&r1=369194&r2=369195&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp Sat Aug 17 09:49:54 2019
@@ -1755,10 +1755,9 @@ static bool isAssertlikeBlock(const CFGB
   // B1, 'A && B' for B2, and 'A && B || C' for B3. Let's check whether we
   // reached the end of the condition!
   if (const Stmt *ElseCond = Else->getTerminatorCondition())
-    if (isa<BinaryOperator>(ElseCond)) {
-      assert(cast<BinaryOperator>(ElseCond)->isLogicalOp());
-      return isAssertlikeBlock(Else, Context);
-    }
+    if (const auto *BinOp = dyn_cast<BinaryOperator>(ElseCond))
+      if (BinOp->isLogicalOp())
+        return isAssertlikeBlock(Else, Context);
 
   return false;
 }

Modified: cfe/trunk/test/Analysis/track-control-dependency-conditions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/track-control-dependency-conditions.cpp?rev=369195&r1=369194&r2=369195&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/track-control-dependency-conditions.cpp (original)
+++ cfe/trunk/test/Analysis/track-control-dependency-conditions.cpp Sat Aug 17 09:49:54 2019
@@ -459,6 +459,18 @@ void f(int flag) {
 
 } // end of namespace unimportant_write_before_collapse_point
 
+namespace dont_crash_on_nonlogical_binary_operator {
+
+void f6(int x) {
+  int a[20];
+  if (x == 25) {} // expected-note{{Assuming 'x' is equal to 25}}
+                  // expected-note at -1{{Taking true branch}}
+  if (a[x] == 123) {} // expected-warning{{The left operand of '==' is a garbage value due to array index out of bounds}}
+                      // expected-note at -1{{The left operand of '==' is a garbage value due to array index out of bounds}}
+}
+
+} // end of namespace dont_crash_on_nonlogical_binary_operator
+
 namespace dont_track_assertlike_conditions {
 
 extern void __assert_fail(__const char *__assertion, __const char *__file,
@@ -532,7 +544,6 @@ void f(int flag) {
 }
 
 #undef assert
-
 } // end of namespace dont_track_assertlike_and_conditions
 
 namespace dont_track_assertlike_or_conditions {




More information about the cfe-commits mailing list