NewDelete-intersections.mm revision 5184dd45b046b5c68a095d2d18a157723aeb904f
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,cplusplus.NewDelete -analyzer-store region -std=c++11 -fblocks -verify %s
2#include "Inputs/system-header-simulator-cxx.h"
3#include "Inputs/system-header-simulator-objc.h"
4
5typedef __typeof__(sizeof(int)) size_t;
6extern "C" void *malloc(size_t);
7extern "C" void free(void *);
8
9//----------------------------------------------------------------------------
10// Check for intersections with unix.Malloc and unix.MallocWithAnnotations 
11// checkers bounded with cplusplus.NewDelete.
12//----------------------------------------------------------------------------
13
14// malloc()/free() are subjects of unix.Malloc and unix.MallocWithAnnotations
15void testMallocFreeNoWarn() {
16  int i;
17  free(&i); // no warn
18
19  int *p1 = (int *)malloc(sizeof(int));
20  free(++p1); // no warn
21
22  int *p2 = (int *)malloc(sizeof(int));
23  free(p2);
24  free(p2); // no warn
25
26  int *p3 = (int *)malloc(sizeof(int)); // no warn
27}
28
29void testDeleteMalloced() {
30  int *p = (int *)malloc(sizeof(int));
31  delete p; // no warn
32} 
33
34//----- Test free standard new
35void testFreeOpNew() {
36  void *p = operator new(0);
37  free(p);
38} // expected-warning{{Memory is never released; potential leak}}
39// FIXME: Pointer should escape
40
41void testFreeNewExpr() {
42  int *p = new int;
43  free(p);
44} // expected-warning{{Memory is never released; potential leak}}
45// FIXME: Pointer should escape
46
47void testObjcFreeNewed() {
48  int *p = new int;
49  NSData *nsdata = [NSData dataWithBytesNoCopy:p length:sizeof(int) freeWhenDone:1]; // expected-warning{{Memory is never released; potential leak}}
50}
51// FIXME: Pointer should escape
52
53void testFreeAfterDelete() {
54  int *p = new int;  
55  delete p;
56  free(p); // expected-warning{{Use of memory after it is freed}}
57}
58
59void testStandardPlacementNewAfterDelete() {
60  int *p = new int;  
61  delete p;
62  p = new(p) int; // expected-warning{{Use of memory after it is freed}}
63}
64