1*67e74705SXin Li // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-store=region -analyzer-max-loop 4 -verify %s 2*67e74705SXin Li #include "Inputs/system-header-simulator.h" 3*67e74705SXin Li 4*67e74705SXin Li typedef __typeof(sizeof(int)) size_t; 5*67e74705SXin Li void *malloc(size_t); 6*67e74705SXin Li another_function(int * y)7*67e74705SXin Listatic int another_function(int *y) { 8*67e74705SXin Li if (*y > 0) 9*67e74705SXin Li return *y; 10*67e74705SXin Li return 0; 11*67e74705SXin Li } 12*67e74705SXin Li function_which_doesnt_give_up(int ** x)13*67e74705SXin Listatic void function_which_doesnt_give_up(int **x) { 14*67e74705SXin Li *x = 0; 15*67e74705SXin Li } 16*67e74705SXin Li function_which_gives_up(int * x)17*67e74705SXin Listatic void function_which_gives_up(int *x) { 18*67e74705SXin Li for (int i = 0; i < 5; ++i) 19*67e74705SXin Li (*x)++; 20*67e74705SXin Li } 21*67e74705SXin Li function_which_gives_up_nested(int * x)22*67e74705SXin Listatic void function_which_gives_up_nested(int *x) { 23*67e74705SXin Li function_which_gives_up(x); 24*67e74705SXin Li for (int i = 0; i < 5; ++i) 25*67e74705SXin Li (*x)++; 26*67e74705SXin Li } 27*67e74705SXin Li function_which_doesnt_give_up_nested(int * x,int * y)28*67e74705SXin Listatic void function_which_doesnt_give_up_nested(int *x, int *y) { 29*67e74705SXin Li *y = another_function(x); 30*67e74705SXin Li function_which_gives_up(x); 31*67e74705SXin Li } 32*67e74705SXin Li coverage1(int * x)33*67e74705SXin Livoid coverage1(int *x) { 34*67e74705SXin Li function_which_gives_up(x); 35*67e74705SXin Li char *m = (char*)malloc(12); 36*67e74705SXin Li } // expected-warning {{Potential leak of memory pointed to by 'm'}} 37*67e74705SXin Li coverage2(int * x)38*67e74705SXin Livoid coverage2(int *x) { 39*67e74705SXin Li if (x) { 40*67e74705SXin Li function_which_gives_up(x); 41*67e74705SXin Li char *m = (char*)malloc(12); 42*67e74705SXin Li } 43*67e74705SXin Li } // expected-warning {{Potential leak of memory pointed to by 'm'}} 44*67e74705SXin Li coverage3(int * x)45*67e74705SXin Livoid coverage3(int *x) { 46*67e74705SXin Li x++; 47*67e74705SXin Li function_which_gives_up(x); 48*67e74705SXin Li char *m = (char*)malloc(12); 49*67e74705SXin Li } // expected-warning {{Potential leak of memory pointed to by 'm'}} 50*67e74705SXin Li coverage4(int * x)51*67e74705SXin Livoid coverage4(int *x) { 52*67e74705SXin Li *x += another_function(x); 53*67e74705SXin Li function_which_gives_up(x); 54*67e74705SXin Li char *m = (char*)malloc(12); 55*67e74705SXin Li } // expected-warning {{Potential leak of memory pointed to by 'm'}} 56*67e74705SXin Li coverage5(int * x)57*67e74705SXin Livoid coverage5(int *x) { 58*67e74705SXin Li for (int i = 0; i<7; ++i) 59*67e74705SXin Li function_which_gives_up(x); 60*67e74705SXin Li // The root function gives up here. 61*67e74705SXin Li char *m = (char*)malloc(12); // no-warning 62*67e74705SXin Li } 63*67e74705SXin Li coverage6(int * x)64*67e74705SXin Livoid coverage6(int *x) { 65*67e74705SXin Li for (int i = 0; i<3; ++i) { 66*67e74705SXin Li function_which_gives_up(x); 67*67e74705SXin Li } 68*67e74705SXin Li char *m = (char*)malloc(12); 69*67e74705SXin Li } // expected-warning {{Potential leak of memory pointed to by 'm'}} 70*67e74705SXin Li coverage7_inline(int * i)71*67e74705SXin Liint coverage7_inline(int *i) { 72*67e74705SXin Li function_which_doesnt_give_up(&i); 73*67e74705SXin Li return *i; // expected-warning {{Dereference}} 74*67e74705SXin Li } 75*67e74705SXin Li coverage8(int * x)76*67e74705SXin Livoid coverage8(int *x) { 77*67e74705SXin Li int y; 78*67e74705SXin Li function_which_doesnt_give_up_nested(x, &y); 79*67e74705SXin Li y = (*x)/y; // expected-warning {{Division by zero}} 80*67e74705SXin Li char *m = (char*)malloc(12); 81*67e74705SXin Li } // expected-warning {{Potential leak of memory pointed to by 'm'}} 82*67e74705SXin Li function_which_gives_up_settonull(int ** x)83*67e74705SXin Livoid function_which_gives_up_settonull(int **x) { 84*67e74705SXin Li *x = 0; 85*67e74705SXin Li int y = 0; 86*67e74705SXin Li for (int i = 0; i < 5; ++i) 87*67e74705SXin Li y++; 88*67e74705SXin Li } 89*67e74705SXin Li coverage9(int * x)90*67e74705SXin Livoid coverage9(int *x) { 91*67e74705SXin Li int y = 5; 92*67e74705SXin Li function_which_gives_up_settonull(&x); 93*67e74705SXin Li y = (*x); // no warning 94*67e74705SXin Li } 95*67e74705SXin Li empty_function()96*67e74705SXin Listatic void empty_function(){ 97*67e74705SXin Li } use_empty_function(int x)98*67e74705SXin Liint use_empty_function(int x) { 99*67e74705SXin Li x = 0; 100*67e74705SXin Li empty_function(); 101*67e74705SXin Li return 5/x; //expected-warning {{Division by zero}} 102*67e74705SXin Li } 103