xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/stack-addr-ps.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-store=region -fblocks -verify %s
2*f4a2713aSLionel Sambuc 
f1()3*f4a2713aSLionel Sambuc int* f1() {
4*f4a2713aSLionel Sambuc   int x = 0;
5*f4a2713aSLionel Sambuc   return &x; // expected-warning{{Address of stack memory associated with local variable 'x' returned}} expected-warning{{address of stack memory associated with local variable 'x' returned}}
6*f4a2713aSLionel Sambuc }
7*f4a2713aSLionel Sambuc 
f2(int y)8*f4a2713aSLionel Sambuc int* f2(int y) {
9*f4a2713aSLionel Sambuc   return &y;  // expected-warning{{Address of stack memory associated with local variable 'y' returned}} expected-warning{{address of stack memory associated with local variable 'y' returned}}
10*f4a2713aSLionel Sambuc }
11*f4a2713aSLionel Sambuc 
f3(int x,int * y)12*f4a2713aSLionel Sambuc int* f3(int x, int *y) {
13*f4a2713aSLionel Sambuc   int w = 0;
14*f4a2713aSLionel Sambuc 
15*f4a2713aSLionel Sambuc   if (x)
16*f4a2713aSLionel Sambuc     y = &w;
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc   return y; // expected-warning{{Address of stack memory associated with local variable 'w' returned to caller}}
19*f4a2713aSLionel Sambuc }
20*f4a2713aSLionel Sambuc 
compound_literal(int x,int y)21*f4a2713aSLionel Sambuc void* compound_literal(int x, int y) {
22*f4a2713aSLionel Sambuc   if (x)
23*f4a2713aSLionel Sambuc     return &(unsigned short){((unsigned short)0x22EF)}; // expected-warning{{Address of stack memory}}
24*f4a2713aSLionel Sambuc 
25*f4a2713aSLionel Sambuc   int* array[] = {};
26*f4a2713aSLionel Sambuc   struct s { int z; double y; int w; };
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc   if (y)
29*f4a2713aSLionel Sambuc     return &((struct s){ 2, 0.4, 5 * 8 }); // expected-warning{{Address of stack memory}}
30*f4a2713aSLionel Sambuc 
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc   void* p = &((struct s){ 42, 0.4, x ? 42 : 0 });
33*f4a2713aSLionel Sambuc   return p; // expected-warning{{Address of stack memory}}
34*f4a2713aSLionel Sambuc }
35*f4a2713aSLionel Sambuc 
alloca_test()36*f4a2713aSLionel Sambuc void* alloca_test() {
37*f4a2713aSLionel Sambuc   void* p = __builtin_alloca(10);
38*f4a2713aSLionel Sambuc   return p; // expected-warning{{Address of stack memory}}
39*f4a2713aSLionel Sambuc }
40*f4a2713aSLionel Sambuc 
array_test(int x[2])41*f4a2713aSLionel Sambuc int array_test(int x[2]) {
42*f4a2713aSLionel Sambuc   return x[0]; // no-warning
43*f4a2713aSLionel Sambuc }
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc struct baz {
46*f4a2713aSLionel Sambuc   int x;
47*f4a2713aSLionel Sambuc   int y[2];
48*f4a2713aSLionel Sambuc };
49*f4a2713aSLionel Sambuc 
struct_test(struct baz byVal,int flag)50*f4a2713aSLionel Sambuc int struct_test(struct baz byVal, int flag) {
51*f4a2713aSLionel Sambuc   if (flag)
52*f4a2713aSLionel Sambuc     return byVal.x; // no-warning
53*f4a2713aSLionel Sambuc   else {
54*f4a2713aSLionel Sambuc     return byVal.y[0]; // no-warning
55*f4a2713aSLionel Sambuc   }
56*f4a2713aSLionel Sambuc }
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc typedef int (^ComparatorBlock)(int a, int b);
test_return_block(void)59*f4a2713aSLionel Sambuc ComparatorBlock test_return_block(void) {
60*f4a2713aSLionel Sambuc   // This block is a global since it has no captures.
61*f4a2713aSLionel Sambuc   ComparatorBlock b = ^int(int a, int b){ return a > b; };
62*f4a2713aSLionel Sambuc   return b; // no-warning
63*f4a2713aSLionel Sambuc }
64*f4a2713aSLionel Sambuc 
test_return_block_with_capture(int x)65*f4a2713aSLionel Sambuc ComparatorBlock test_return_block_with_capture(int x) {
66*f4a2713aSLionel Sambuc   // This block is stack allocated because it has captures.
67*f4a2713aSLionel Sambuc   ComparatorBlock b = ^int(int a, int b){ return a > b + x; };
68*f4a2713aSLionel Sambuc   return b; // expected-warning{{Address of stack-allocated block}}
69*f4a2713aSLionel Sambuc }
70*f4a2713aSLionel Sambuc 
71*f4a2713aSLionel Sambuc ComparatorBlock test_return_block_neg_aux(void);
test_return_block_neg(void)72*f4a2713aSLionel Sambuc ComparatorBlock test_return_block_neg(void) {
73*f4a2713aSLionel Sambuc   ComparatorBlock b = test_return_block_neg_aux();
74*f4a2713aSLionel Sambuc   return b; // no-warning
75*f4a2713aSLionel Sambuc }
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc // <rdar://problem/7523821>
rdar_7523821_f2()78*f4a2713aSLionel Sambuc int *rdar_7523821_f2() {
79*f4a2713aSLionel Sambuc   int a[3];
80*f4a2713aSLionel Sambuc   return a; // expected-warning 2 {{ddress of stack memory associated with local variable 'a' returned}}
81*f4a2713aSLionel Sambuc };
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc // Handle blocks that have no captures or are otherwise declared 'static'.
84*f4a2713aSLionel Sambuc // <rdar://problem/10348049>
85*f4a2713aSLionel Sambuc typedef int (^RDar10348049)(int value);
test_rdar10348049(void)86*f4a2713aSLionel Sambuc RDar10348049 test_rdar10348049(void) {
87*f4a2713aSLionel Sambuc   static RDar10348049 b = ^int(int x) {
88*f4a2713aSLionel Sambuc     return x + 2;
89*f4a2713aSLionel Sambuc   };
90*f4a2713aSLionel Sambuc   return b; // no-warning
91*f4a2713aSLionel Sambuc }
92*f4a2713aSLionel Sambuc 
93