free.c revision 65d39251ff57b8e33cf6d3a7fcc6aa1c6f8cdc68
1// RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-store=region -analyzer-experimental-checks -fblocks -verify %s 2void free(void *); 3 4void t1 () { 5 int a[] = { 1 }; 6 free(a); // expected-warning {{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}} 7} 8 9void t2 () { 10 int a = 1; 11 free(&a); // expected-warning {{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}} 12} 13 14void t3 () { 15 static int a[] = { 1 }; 16 free(a); // expected-warning {{Argument to free() is the address of the static variable 'a', which is not memory allocated by malloc()}} 17} 18 19void t4 (char *x) { 20 free(x); // no-warning 21} 22 23void t5 () { 24 extern char *ptr(); 25 free(ptr()); // no-warning 26} 27 28void t6 () { 29 free((void*)1000); // expected-warning {{Argument to free() is a constant address (1000), which is not memory allocated by malloc()}} 30} 31 32void t7 (char **x) { 33 free(*x); // no-warning 34} 35 36void t8 (char **x) { 37 // ugh 38 free((*x)+8); // no-warning 39} 40 41void t9 () { 42label: 43 free(&&label); // expected-warning {{Argument to free() is the address of the label 'label', which is not memory allocated by malloc()}} 44} 45 46void t10 () { 47 free((void*)&t10); // expected-warning {{Argument to free() is the address of the function 't10', which is not memory allocated by malloc()}} 48} 49 50void t11 () { 51 char *p = (char*)__builtin_alloca(2); 52 free(p); // expected-warning {{Argument to free() was allocated by alloca(), not malloc()}} 53} 54 55void t12 () { 56 free(^{return;}); // expected-warning {{Argument to free() is a block, which is not memory allocated by malloc()}} 57} 58 59void t13 (char a) { 60 free(&a); // expected-warning {{Argument to free() is the address of the parameter 'a', which is not memory allocated by malloc()}} 61} 62 63static int someGlobal[2]; 64void t14 () { 65 free(someGlobal); // expected-warning {{Argument to free() is the address of the global variable 'someGlobal', which is not memory allocated by malloc()}} 66} 67 68void t15 (char **x, int offset) { 69 // Unknown value 70 free(x[offset]); // no-warning 71} 72