[PATCH] [analyzer] Fix a crash by division by zero

Daniel Marjamäki daniel.marjamaki at evidente.se
Thu Jun 25 07:06:28 PDT 2015


REPOSITORY
  rL LLVM

http://reviews.llvm.org/D10145

Files:
  cfe/trunk/lib/StaticAnalyzer/Core/BasicValueFactory.cpp
  cfe/trunk/test/Analysis/division-by-zero.c

Index: cfe/trunk/lib/StaticAnalyzer/Core/BasicValueFactory.cpp
===================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/BasicValueFactory.cpp
+++ cfe/trunk/lib/StaticAnalyzer/Core/BasicValueFactory.cpp
@@ -154,9 +154,13 @@
       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:
Index: cfe/trunk/test/Analysis/division-by-zero.c
===================================================================
--- cfe/trunk/test/Analysis/division-by-zero.c
+++ cfe/trunk/test/Analysis/division-by-zero.c
@@ -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;
+}

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10145.28462.patch
Type: text/x-patch
Size: 985 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150625/62d268fc/attachment.bin>


More information about the cfe-commits mailing list