[cfe-commits] r158219 - in /cfe/trunk: lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp test/Analysis/malloc-sizeof.c

Anna Zaks ganna at apple.com
Fri Jun 8 11:44:43 PDT 2012


Author: zaks
Date: Fri Jun  8 13:44:43 2012
New Revision: 158219

URL: http://llvm.org/viewvc/llvm-project?rev=158219&view=rev
Log:
[analyzer] MallocSizeofChecker false positive: when sizeof is argument
to addition.

We should not to warn in case the malloc size argument is an
addition containing 'sizeof' operator - it is common to use the pattern
to pack values of different sizes into a buffer. 

Ex:

uint8_t *buffer = (uint8_t*)malloc(dataSize + sizeof(length));

Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp
    cfe/trunk/test/Analysis/malloc-sizeof.c

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp?rev=158219&r1=158218&r2=158219&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp Fri Jun  8 13:44:43 2012
@@ -118,11 +118,6 @@
     Visit(E->getRHS());
   }
 
-  void VisitBinAdd(const BinaryOperator *E) {
-    Visit(E->getLHS());
-    Visit(E->getRHS());
-  }
-
   void VisitImplicitCastExpr(const ImplicitCastExpr *E) {
     return Visit(E->getSubExpr());
   }

Modified: cfe/trunk/test/Analysis/malloc-sizeof.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/malloc-sizeof.c?rev=158219&r1=158218&r2=158219&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/malloc-sizeof.c (original)
+++ cfe/trunk/test/Analysis/malloc-sizeof.c Fri Jun  8 13:44:43 2012
@@ -10,13 +10,14 @@
 struct A {};
 struct B {};
 
-void foo() {
+void foo(unsigned int unsignedInt, unsigned int readSize) {
   int *ip1 = malloc(sizeof(1));
   int *ip2 = malloc(4 * sizeof(int));
 
   long *lp1 = malloc(sizeof(short)); // expected-warning {{Result of 'malloc' is converted to a pointer of type 'long', which is incompatible with sizeof operand type 'short'}}
   long *lp2 = malloc(5 * sizeof(double)); // expected-warning {{Result of 'malloc' is converted to a pointer of type 'long', which is incompatible with sizeof operand type 'double'}}
-  long *lp3 = malloc(5 * sizeof(char) + 2); // expected-warning {{Result of 'malloc' is converted to a pointer of type 'long', which is incompatible with sizeof operand type 'char'}}
+  char *cp3 = malloc(5 * sizeof(char) + 2); // no warning
+  unsigned char *buf = malloc(readSize + sizeof(unsignedInt)); // no warning
 
   struct A *ap1 = calloc(1, sizeof(struct A));
   struct A *ap2 = calloc(2, sizeof(*ap1));





More information about the cfe-commits mailing list