Home | History | Annotate | Download | only in Analysis
      1 // RUN: %clang_cc1 -analyze -analyzer-checker=core,cplusplus.NewDelete -std=c++11 -fblocks -verify %s
      2 // RUN: %clang_cc1 -analyze -analyzer-checker=core,cplusplus.NewDelete,cplusplus.NewDeleteLeaks -std=c++11 -DLEAKS -fblocks -verify %s
      3 #include "Inputs/system-header-simulator-cxx.h"
      4 #include "Inputs/system-header-simulator-objc.h"
      5 
      6 typedef __typeof__(sizeof(int)) size_t;
      7 extern "C" void *malloc(size_t);
      8 extern "C" void *alloca(size_t);
      9 extern "C" void free(void *);
     10 
     11 //----------------------------------------------------------------------------
     12 // Check for intersections with unix.Malloc and unix.MallocWithAnnotations 
     13 // checkers bounded with cplusplus.NewDelete.
     14 //----------------------------------------------------------------------------
     15 
     16 //----- malloc()/free() are subjects of unix.Malloc and unix.MallocWithAnnotations
     17 void testMallocFreeNoWarn() {
     18   int i;
     19   free(&i); // no warn
     20 
     21   int *p1 = (int *)malloc(sizeof(int));
     22   free(++p1); // no warn
     23 
     24   int *p2 = (int *)malloc(sizeof(int));
     25   free(p2);
     26   free(p2); // no warn
     27 
     28   int *p3 = (int *)malloc(sizeof(int)); // no warn
     29 
     30   int *p4 = (int *)malloc(sizeof(int));
     31   free(p4);
     32   int j = *p4; // no warn
     33 
     34   int *p5 = (int *)alloca(sizeof(int));
     35   free(p5); // no warn
     36 }
     37 
     38 void testDeleteMalloced() {
     39   int *p1 = (int *)malloc(sizeof(int));
     40   delete p1; // no warn
     41 
     42   int *p2 = (int *)__builtin_alloca(sizeof(int));
     43   delete p2; // no warn
     44 } 
     45 
     46 void testUseZeroAllocatedMalloced() {
     47   int *p1 = (int *)malloc(0);
     48   *p1 = 1; // no warn
     49 }
     50 
     51 //----- Test free standard new
     52 void testFreeOpNew() {
     53   void *p = operator new(0);
     54   free(p);
     55 }
     56 #ifdef LEAKS
     57 // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
     58 #endif
     59 
     60 void testFreeNewExpr() {
     61   int *p = new int;
     62   free(p);
     63 }
     64 #ifdef LEAKS
     65 // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
     66 #endif
     67 
     68 void testObjcFreeNewed() {
     69   int *p = new int;
     70   NSData *nsdata = [NSData dataWithBytesNoCopy:p length:sizeof(int) freeWhenDone:1];
     71 #ifdef LEAKS
     72   // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
     73 #endif
     74 }
     75 
     76 void testFreeAfterDelete() {
     77   int *p = new int;  
     78   delete p;
     79   free(p); // expected-warning{{Use of memory after it is freed}}
     80 }
     81 
     82 void testStandardPlacementNewAfterDelete() {
     83   int *p = new int;  
     84   delete p;
     85   p = new(p) int; // expected-warning{{Use of memory after it is freed}}
     86 }
     87