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