209 lines · c
1// RUN: rm -f %t2// RUN: %clang_analyze_cc1 -fblocks -analyzer-checker=core,unix.Malloc -analyzer-output=plist -verify -o %t -analyzer-config eagerly-assume=false %s3// RUN: tail -n +11 %t | %normalize_plist | diff -ub %S/Inputs/expected-plists/malloc-plist.c.plist -4 5typedef __typeof(sizeof(int)) size_t;6void *malloc(size_t);7void free(void *);8void *realloc(void *ptr, size_t size);9 10void diagnosticTest(int in) {11 if (in > 5) {12 int *p = malloc(12);13 *p = 0;14 (*p)++;15 }16 in++; // expected-warning {{leak}}17}18 19void myArrayAllocation(void) {20 int **A;21 A = malloc(2*sizeof(int*));22 A[0] = 0;23}//expected-warning{{Potential leak}}24 25void reallocDiagnostics(void) {26 char * buf = malloc(100);27 char * tmp;28 tmp = (char*)realloc(buf, 0x1000000);29 if (!tmp) {30 return;// expected-warning {{leak}}31 }32 buf = tmp;33 free(buf);34}35 36void *wrapper(void) {37 void *x = malloc(100);38 // This is intentionally done to test diagnostic emission.39 if (x)40 return x;41 return 0;42}43 44void test_wrapper(void) {45 void *buf = wrapper();46 (void) buf;47}//expected-warning{{Potential leak}}48 49// Test what happens when the same call releases and allocated memory.50// Also tests the stack hint for parameters, when they are passed directly or via pointer.51void my_free(void *x) {52 free(x);53}54void my_malloc_and_free(void **x) {55 *x = malloc(100);56 if (*x)57 my_free(*x);58 return;59}60void *test_double_action_call(void) {61 void *buf;62 my_malloc_and_free(&buf);63 return buf; //expected-warning{{Use of memory after it is released}}64}65 66// Test stack hint for 'reallocation failed'.67char *my_realloc(char *buf) {68 char *tmp;69 tmp = (char*)realloc(buf, 0x1000000);70 if (!tmp) {71 return tmp;72 }73 return tmp;74}75void reallocIntra(void) {76 char *buf = (char *)malloc(100);77 buf = my_realloc(buf);78 free(buf);//expected-warning{{Potential leak}}79}80 81// Test stack hint when returning a result.82static char *malloc_wrapper_ret(void) {83 return (char*)malloc(12);84}85void use_ret(void) {86 char *v;87 v = malloc_wrapper_ret();88}//expected-warning{{Potential leak}}89 90// Passing a block as a parameter to an inlined call for which we generate91// a stack hint message caused crashes.92void myfree_takingblock(void (^ignored)(void), int *p) {93 free(p);94}95 96void call_myfree_takingblock(void) {97 void (^some_block)(void) = ^void(void) { };98 99 int *p = malloc(sizeof(int));100 myfree_takingblock(some_block, p);101 *p = 3;//expected-warning{{Use of memory after it is released}}102}103 104// Test that we refer to the last symbol used in the leak diagnostic.105void LeakedSymbol(int in) {106 int *m = 0;107 int *p;108 p = (int*)malloc(12);109 *p = 0;110 (*p)++;111 m = p;112 p = 0;113 (*m)++;114 in++;//expected-warning{{Potential leak}}115}116 117// Tests that exercise running remove dead bindings at Call exit.118static void function_with_leak1(void) {119 char *x = (char*)malloc(12);120} //expected-warning{{Potential leak}}121void use_function_with_leak1(void) {122 function_with_leak1();123 int y = 0;124}125 126static void function_with_leak2(void) {127 char *x = (char*)malloc(12);128 int m = 0; //expected-warning{{Potential leak}}129}130void use_function_with_leak2(void) {131 function_with_leak2();132}133 134static void function_with_leak3(int y) {135 char *x = (char*)malloc(12);136 if (y)137 y++;138}//expected-warning{{Potential leak}}139void use_function_with_leak3(int y) {140 function_with_leak3(y);141}142 143static void function_with_leak4(int y) {144 char *x = (char*)malloc(12);145 if (y)146 y++;147 else148 y--;//expected-warning{{Potential leak}}149}150void use_function_with_leak4(int y) {151 function_with_leak4(y);152}153 154int anotherFunction5(void) {155 return 5;156}157static int function_with_leak5(void) {158 char *x = (char*)malloc(12);159 return anotherFunction5();//expected-warning{{Potential leak}}160}161void use_function_with_leak5(void) {162 function_with_leak5();163}164 165void anotherFunction6(int m) {166 m++;167}168static void function_with_leak6(void) {169 char *x = (char*)malloc(12);170 anotherFunction6(3);//expected-warning{{Potential leak}}171}172void use_function_with_leak6(void) {173 function_with_leak6();174}175 176static void empty_function(void){177}178void use_empty_function(void) {179 empty_function();180}181static char *function_with_leak7(void) {182 return (char*)malloc(12);183}184void use_function_with_leak7(void) {185 function_with_leak7();186}//expected-warning{{Potential memory leak}}187 188// Test that we do not print the name of a variable not visible from where189// the issue is reported.190int *my_malloc(void) {191 int *p = malloc(12);192 return p;193}194void testOnlyRefferToVisibleVariables(void) {195 my_malloc();196} // expected-warning{{Potential memory leak}}197 198struct PointerWrapper{199 int*p;200};201int *my_malloc_into_struct(void) {202 struct PointerWrapper w;203 w.p = malloc(12);204 return w.p;205}206void testMyMalloc(void) {207 my_malloc_into_struct();208} // expected-warning{{Potential memory leak}}209