]> granicus.if.org Git - clang/commitdiff
Fix a crash by division by zero in analyzer
authorDaniel Marjamaki <daniel.marjamaki@evidente.se>
Thu, 25 Jun 2015 14:06:02 +0000 (14:06 +0000)
committerDaniel Marjamaki <daniel.marjamaki@evidente.se>
Thu, 25 Jun 2015 14:06:02 +0000 (14:06 +0000)
Patch by takeshi-yoshimura!

Differential Revision: http://reviews.llvm.org/D10145

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@240643 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Core/BasicValueFactory.cpp
test/Analysis/division-by-zero.c [new file with mode: 0644]

index 0e90566839ca7d029c31636fa8181eb2015680e7..3c3f41a885e95ccca12d0552b9a4727343e77a53 100644 (file)
@@ -154,9 +154,13 @@ BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
       return &getValue( V1 * V2 );
 
     case BO_Div:
+      if (V2 == 0) // Avoid division by zero
+        return nullptr;
       return &getValue( V1 / V2 );
 
     case BO_Rem:
+      if (V2 == 0) // Avoid division by zero
+        return nullptr;
       return &getValue( V1 % V2 );
 
     case BO_Add:
diff --git a/test/Analysis/division-by-zero.c b/test/Analysis/division-by-zero.c
new file mode 100644 (file)
index 0000000..d3c228e
--- /dev/null
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -analyze -analyzer-checker=unix.Malloc %s
+// Do not crash due to division by zero
+
+int f(unsigned int a) {
+  if (a <= 0) return 1 / a;
+  return a;
+}