[PATCH] D99343: [Analyzer] Infer 0 value when the divisible is 0 (bug fix)

Gabor Marton via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 25 08:06:26 PDT 2021


martong created this revision.
martong added reviewers: steakhal, NoQ, vsavchenko, Szelethus.
Herald added subscribers: ASDenysPetrov, Charusso, gamesh411, dkrupp, donat.nagy, mikhail.ramalho, a.sidorin, rnkovacs, szepet, baloghadamsoftware, xazax.hun, whisperity.
martong requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Currently, we infer 0 if the divisible of the modulo op is 0:

  int a = x < 0; // a can be 0
  int b = a % y; // b is either 1 % sym or 0

However, we don't when the op is / :

  int a = x < 0; // a can be 0
  int b = a / y; // b is either 1 / sym or 0 / sym

This commit fixes the discrepancy.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99343

Files:
  clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
  clang/test/Analysis/zero-operands.c


Index: clang/test/Analysis/zero-operands.c
===================================================================
--- /dev/null
+++ clang/test/Analysis/zero-operands.c
@@ -0,0 +1,53 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core \
+// RUN:   -analyzer-checker=debug.ExprInspection \
+// RUN:   -verify %s
+
+void clang_analyzer_dump(int);
+
+void test_0_multiplier1(int x, int y) {
+  int a = x < 0; // Eagerly bifurcate.
+  clang_analyzer_dump(a);
+  // expected-warning at -1{{0 S32b}}
+  // expected-warning at -2{{1 S32b}}
+
+  int b = a * y;
+  clang_analyzer_dump(b);
+  // expected-warning at -1{{0 S32b}}
+  // expected-warning-re at -2{{reg_${{[[:digit:]]+}}<int y>}}
+}
+
+void test_0_multiplier2(int x, int y) {
+  int a = x < 0; // Eagerly bifurcate.
+  clang_analyzer_dump(a);
+  // expected-warning at -1{{0 S32b}}
+  // expected-warning at -2{{1 S32b}}
+
+  int b = y * a;
+  clang_analyzer_dump(b);
+  // expected-warning at -1{{0 S32b}}
+  // expected-warning-re at -2{{reg_${{[[:digit:]]+}}<int y>}}
+}
+
+void test_0_modulo(int x, int y) {
+  int a = x < 0; // Eagerly bifurcate.
+  clang_analyzer_dump(a);
+  // expected-warning at -1{{0 S32b}}
+  // expected-warning at -2{{1 S32b}}
+
+  int b = a % y;
+  clang_analyzer_dump(b);
+  // expected-warning at -1{{0 S32b}}
+  // expected-warning-re at -2{{1 % (reg_${{[[:digit:]]+}}<int y>)}}
+}
+
+void test_0_divisible(int x, int y) {
+  int a = x < 0; // Eagerly bifurcate.
+  clang_analyzer_dump(a);
+  // expected-warning at -1{{0 S32b}}
+  // expected-warning at -2{{1 S32b}}
+
+  int b = a / y;
+  clang_analyzer_dump(b);
+  // expected-warning at -1{{0 S32b}}
+  // expected-warning-re at -2{{1 / (reg_${{[[:digit:]]+}}<int y>)}}
+}
Index: clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
+++ clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
@@ -652,6 +652,7 @@
         if (LHSValue == 0)
           return evalCastFromNonLoc(lhs, resultTy);
         return makeSymExprValNN(op, InputLHS, InputRHS, resultTy);
+      case BO_Div:
       case BO_Rem:
         // 0 % x == 0
         if (LHSValue == 0)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99343.333308.patch
Type: text/x-patch
Size: 2163 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210325/b07635c0/attachment.bin>


More information about the cfe-commits mailing list