|
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 | + |
| 22 | +// The order of these declarations are important to verify that analisys still works even |
| 23 | +// if there are less specific declarations of the same functions |
| 24 | +void __attribute((ownership_returns(malloc3))) *my_malloc3(size_t); |
| 25 | +void *my_malloc3(size_t); |
| 26 | + |
| 27 | +void *my_malloc4(size_t); |
| 28 | +void __attribute((ownership_returns(malloc4))) *my_malloc4(size_t); |
| 29 | + |
17 | 30 | //---------------------------------------------------------------
|
18 | 31 | // Test if an allocation function matches deallocation function
|
19 | 32 | //---------------------------------------------------------------
|
@@ -60,6 +73,41 @@ void testMalloc8() {
|
60 | 73 | operator delete[](p); // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'operator delete[]'}}
|
61 | 74 | }
|
62 | 75 |
|
| 76 | +void testMalloc9() { |
| 77 | + int *p = (int *)my_malloc(sizeof(int)); |
| 78 | + my_free(p); // no warning |
| 79 | +} |
| 80 | + |
| 81 | +void testMalloc10() { |
| 82 | + int *p = (int *)my_malloc1(sizeof(int)); |
| 83 | + my_free1(p); // no warning |
| 84 | +} |
| 85 | + |
| 86 | +void testMalloc11() { |
| 87 | + int *p = (int *)my_malloc1(sizeof(int)); |
| 88 | + 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'}} |
| 89 | +} |
| 90 | + |
| 91 | +void testMalloc12() { |
| 92 | + int *p = (int *)my_malloc2(sizeof(int)); |
| 93 | + 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'}} |
| 94 | +} |
| 95 | + |
| 96 | +void testMalloc13() { |
| 97 | + int *p = (int *)my_malloc1(sizeof(int)); |
| 98 | + free(p); // expected-warning{{Memory allocated by 'my_malloc1()' should be deallocated by function that takes ownership of 'malloc1', not 'free()'}} |
| 99 | +} |
| 100 | + |
| 101 | +void testMalloc14() { |
| 102 | + int *p = (int *)my_malloc3(sizeof(int)); |
| 103 | + free(p); // expected-warning{{Memory allocated by 'my_malloc3()' should be deallocated by function that takes ownership of 'malloc3', not 'free()'}} |
| 104 | +} |
| 105 | + |
| 106 | +void testMalloc15() { |
| 107 | + int *p = (int *)my_malloc4(sizeof(int)); |
| 108 | + free(p); // expected-warning{{Memory allocated by 'my_malloc4()' should be deallocated by function that takes ownership of 'malloc4', not 'free()'}} |
| 109 | +} |
| 110 | + |
63 | 111 | void testAlloca() {
|
64 | 112 | int *p = (int *)__builtin_alloca(sizeof(int));
|
65 | 113 | delete p; // expected-warning{{Memory allocated by 'alloca()' should not be deallocated}}
|
|
0 commit comments