|
14 | 14 | void free(void *);
|
15 | 15 | void __attribute((ownership_takes(malloc, 1))) my_free(void *);
|
16 | 16 |
|
| 17 | +void __attribute((ownership_returns(malloc1))) *my_malloc1(size_t); |
| 18 | +void __attribute((ownership_takes(malloc1, 1))) my_free1(void *); |
| 19 | + |
| 20 | +void __attribute((ownership_returns(malloc2))) *my_malloc2(size_t); |
| 21 | +void __attribute((ownership_returns(malloc2))) *my_malloc3(size_t); |
| 22 | +void __attribute((ownership_takes(malloc2, 1))) __attribute((ownership_takes(malloc3, 1))) my_free23(void *); |
| 23 | + |
17 | 24 | //---------------------------------------------------------------
|
18 | 25 | // Test if an allocation function matches deallocation function
|
19 | 26 | //---------------------------------------------------------------
|
@@ -60,6 +67,41 @@ void testMalloc8() {
|
60 | 67 | operator delete[](p); // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not operator delete[]}}
|
61 | 68 | }
|
62 | 69 |
|
| 70 | +void testMalloc9() { |
| 71 | + int *p = (int *)my_malloc(sizeof(int)); |
| 72 | + my_free(p); // no warning |
| 73 | +} |
| 74 | + |
| 75 | +void testMalloc10() { |
| 76 | + int *p = (int *)my_malloc1(sizeof(int)); |
| 77 | + my_free1(p); // no warning |
| 78 | +} |
| 79 | + |
| 80 | +void testMalloc11() { |
| 81 | + int *p = (int *)my_malloc2(sizeof(int)); |
| 82 | + my_free23(p); // no warning |
| 83 | +} |
| 84 | + |
| 85 | +void testMalloc12() { |
| 86 | + int *p = (int *)my_malloc1(sizeof(int)); |
| 87 | + my_free(p); // expected-warning{{Memory allocated by my_malloc1() should be deallocated by function that takes ownership of 'malloc1', not my_free(), which takes ownership of 'malloc'}} |
| 88 | +} |
| 89 | + |
| 90 | +void testMalloc13() { |
| 91 | + int *p = (int *)my_malloc2(sizeof(int)); |
| 92 | + my_free1(p); // expected-warning{{Memory allocated by my_malloc2() should be deallocated by function that takes ownership of 'malloc2', not my_free1(), which takes ownership of 'malloc1'}} |
| 93 | +} |
| 94 | + |
| 95 | +void testMalloc14() { |
| 96 | + int *p = (int *)my_malloc1(sizeof(int)); |
| 97 | + my_free23(p); // expected-warning{{Memory allocated by my_malloc1() should be deallocated by function that takes ownership of 'malloc1', not my_free23(), which takes ownership of 'malloc2', 'malloc3'}} |
| 98 | +} |
| 99 | + |
| 100 | +void testMalloc15() { |
| 101 | + int *p = (int *)my_malloc1(sizeof(int)); |
| 102 | + free(p); // expected-warning{{Memory allocated by my_malloc1() should be deallocated by function that takes ownership of 'malloc1', not free()}} |
| 103 | +} |
| 104 | + |
63 | 105 | void testAlloca() {
|
64 | 106 | int *p = (int *)__builtin_alloca(sizeof(int));
|
65 | 107 | delete p; // expected-warning{{Memory allocated by alloca() should not be deallocated}}
|
|
0 commit comments