[analyzer][solver] Prevent use of a null state
authorValeriy Savchenko <vsavchenko@apple.com>
Tue, 11 May 2021 14:30:02 +0000 (17:30 +0300)
committerValeriy Savchenko <vsavchenko@apple.com>
Thu, 13 May 2021 17:16:29 +0000 (20:16 +0300)
rdar://77686137

Differential Revision: https://reviews.llvm.org/D102240

clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp
clang/test/Analysis/PR50268.c [new file with mode: 0644]

index 9745359..e54b9c1 100644 (file)
@@ -1487,15 +1487,18 @@ private:
       // This is an infeasible assumption.
       return nullptr;
 
-    ProgramStateRef NewState = setConstraint(State, Sym, NewConstraint);
-    if (auto Equality = EqualityInfo::extract(Sym, Int, Adjustment)) {
-      // If the original assumption is not Sym + Adjustment !=/</> Int,
-      // we should invert IsEquality flag.
-      Equality->IsEquality = Equality->IsEquality != EQ;
-      return track(NewState, *Equality);
+    if (ProgramStateRef NewState = setConstraint(State, Sym, NewConstraint)) {
+      if (auto Equality = EqualityInfo::extract(Sym, Int, Adjustment)) {
+        // If the original assumption is not Sym + Adjustment !=/</> Int,
+        // we should invert IsEquality flag.
+        Equality->IsEquality = Equality->IsEquality != EQ;
+        return track(NewState, *Equality);
+      }
+
+      return NewState;
     }
 
-    return NewState;
+    return nullptr;
   }
 
   ProgramStateRef track(ProgramStateRef State, EqualityInfo ToTrack) {
diff --git a/clang/test/Analysis/PR50268.c b/clang/test/Analysis/PR50268.c
new file mode 100644 (file)
index 0000000..6e3536b
--- /dev/null
@@ -0,0 +1,12 @@
+// RUN: %clang_analyze_cc1 -w -analyzer-checker=core -verify %s \
+// RUN:    -analyzer-config eagerly-assume=true
+
+// expected-no-diagnostics
+
+
+int test(unsigned long a, unsigned long c, int b) {
+  c -= a;
+  if (0 >= b) {}
+  c == b;
+  return c ? 0 : 2; // no-crash
+}