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