stack-addr-ps.c revision d8aefab741a788ba308468df0c66c9dafb4c8530
1// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -verify %s
2// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -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.}}
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