Debian Codegraph @

clang/test/Analysis/NewDelete-intersections.mm

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