blob: b1ee4c85cf5da8999bc1530da2e9461752361f74 [file] [log] [blame]
Jordan Rose10ad0812013-04-05 17:55:07 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,cplusplus.NewDelete -std=c++11 -verify %s
2// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,cplusplus.NewDelete,alpha.cplusplus.NewDeleteLeaks -DLEAKS -std=c++11 -verify %s
Anton Yartseve3377fb2013-04-04 23:46:29 +00003
4typedef __typeof(sizeof(int)) size_t;
5void *malloc(size_t);
6void free(void *);
7
8//--------------------------------------------------
9// Check that unix.Malloc catches all types of bugs.
10//--------------------------------------------------
11void testMallocDoubleFree() {
12 int *p = (int *)malloc(sizeof(int));
13 free(p);
14 free(p); // expected-warning{{Attempt to free released memory}}
15}
16
17void testMallocLeak() {
18 int *p = (int *)malloc(sizeof(int));
Anna Zaksa1de8562013-04-06 00:41:36 +000019} // expected-warning{{Potential leak of memory pointed to by 'p'}}
Anton Yartseve3377fb2013-04-04 23:46:29 +000020
21void testMallocUseAfterFree() {
22 int *p = (int *)malloc(sizeof(int));
23 free(p);
24 int j = *p; // expected-warning{{Use of memory after it is freed}}
25}
26
27void testMallocBadFree() {
28 int i;
29 free(&i); // expected-warning{{Argument to free() is the address of the local variable 'i', which is not memory allocated by malloc()}}
30}
31
32void testMallocOffsetFree() {
33 int *p = (int *)malloc(sizeof(int));
34 free(++p); // expected-warning{{Argument to free() is offset by 4 bytes from the start of memory allocated by malloc()}}
35}
36
37//-----------------------------------------------------------------
38// Check that unix.MismatchedDeallocator catches all types of bugs.
39//-----------------------------------------------------------------
40void testMismatchedDeallocator() {
41 int *x = (int *)malloc(sizeof(int));
42 delete x; // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not 'delete'}}
43}
44
45//----------------------------------------------------------------
46// Check that alpha.cplusplus.NewDelete catches all types of bugs.
47//----------------------------------------------------------------
48void testNewDoubleFree() {
49 int *p = new int;
50 delete p;
51 delete p; // expected-warning{{Attempt to free released memory}}
52}
53
54void testNewLeak() {
55 int *p = new int;
Jordan Rose26330562013-04-05 17:55:00 +000056}
57#ifdef LEAKS
Anna Zaksa1de8562013-04-06 00:41:36 +000058// expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
Jordan Rose26330562013-04-05 17:55:00 +000059#endif
Anton Yartseve3377fb2013-04-04 23:46:29 +000060
61void testNewUseAfterFree() {
62 int *p = (int *)operator new(0);
63 delete p;
64 int j = *p; // expected-warning{{Use of memory after it is freed}}
65}
66
67void testNewBadFree() {
68 int i;
69 delete &i; // expected-warning{{Argument to 'delete' is the address of the local variable 'i', which is not memory allocated by 'new'}}
70}
71
72void testNewOffsetFree() {
73 int *p = new int;
74 operator delete(++p); // expected-warning{{Argument to operator delete is offset by 4 bytes from the start of memory allocated by 'new'}}
75}
Anna Zaks93a21a82013-04-09 00:30:28 +000076
77//----------------------------------------------------------------
78// Test that we check for free errors on escaped pointers.
79//----------------------------------------------------------------
80void changePtr(int **p);
81static int *globalPtr;
82void changePointee(int *p);
83
84void testMismatchedChangePtrThroughCall() {
85 int *p = (int*)malloc(sizeof(int)*4);
86 changePtr(&p);
87 delete p; // no-warning the value of the pointer might have changed
88}
89
90void testMismatchedChangePointeeThroughCall() {
91 int *p = (int*)malloc(sizeof(int)*4);
92 changePointee(p);
93 delete p; // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not 'delete'}}
94}
95
96void testShouldReportDoubleFreeNotMismatched() {
97 int *p = (int*)malloc(sizeof(int)*4);
98 globalPtr = p;
99 free(p);
100 delete globalPtr; // expected-warning {{Attempt to free released memory}}
101}
102
103void testMismatchedChangePointeeThroughAssignment() {
104 int *arr = new int[4];
105 globalPtr = arr;
106 delete arr; // expected-warning{{Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'delete'}}
107}