stack-addr-ps.c revision a8166156a6414ddd6a68514dc4f48e95d2259977
1// RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-store=basic -fblocks -verify %s
2// RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-store=region -fblocks -verify %s
3
4int* f1() {
5  int x = 0;
6  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}}
7}
8
9int* f2(int y) {
10  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}}
11}
12
13int* f3(int x, int *y) {
14  int w = 0;
15
16  if (x)
17    y = &w;
18
19  return y; // expected-warning{{Address of stack memory associated with local variable 'w' returned to caller}}
20}
21
22void* compound_literal(int x, int y) {
23  if (x)
24    return &(unsigned short){((unsigned short)0x22EF)}; // expected-warning{{Address of stack memory}}
25
26  int* array[] = {};
27  struct s { int z; double y; int w; };
28
29  if (y)
30    return &((struct s){ 2, 0.4, 5 * 8 }); // expected-warning{{Address of stack memory}}
31
32
33  void* p = &((struct s){ 42, 0.4, x ? 42 : 0 });
34  return p; // expected-warning{{Address of stack memory}}
35}
36
37void* alloca_test() {
38  void* p = __builtin_alloca(10);
39  return p; // expected-warning{{Address of stack memory}}
40}
41
42int array_test(int x[2]) {
43  return x[0]; // no-warning
44}
45
46struct baz {
47  int x;
48  int y[2];
49};
50
51int struct_test(struct baz byVal, int flag) {
52  if (flag)
53    return byVal.x; // no-warning
54  else {
55    return byVal.y[0]; // no-warning
56  }
57}
58
59typedef int (^ComparatorBlock)(int a, int b);
60ComparatorBlock test_return_block(void) {
61  ComparatorBlock b = ^int(int a, int b){ return a > b; };
62  return b; // expected-warning{{Address of stack-allocated block declared on line 61 returned to caller}}
63}
64
65ComparatorBlock test_return_block_neg_aux(void);
66ComparatorBlock test_return_block_neg(void) {
67  ComparatorBlock b = test_return_block_neg_aux();
68  return b; // no-warning
69}
70
71// <rdar://problem/7523821>
72int *rdar_7523821_f2() {
73  int a[3];
74  return a; // expected-warning 2 {{ddress of stack memory associated with local variable 'a' returned}}
75};
76
77
78