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