stack-addr-ps.cpp revision 5e6c06bc7deaaefe130b730032a9acb9cd38bf0c
1// RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-store=region -verify %s
2
3typedef __INTPTR_TYPE__ intptr_t;
4
5const int& g() {
6  int s;
7  return s; // expected-warning{{Address of stack memory associated with local variable 's' returned}} expected-warning{{reference to stack memory associated with local variable 's' returned}}
8}
9
10const int& g2() {
11  int s1;
12  int &s2 = s1; // expected-note {{binding reference variable 's2' here}}
13  return s2; // expected-warning{{Address of stack memory associated with local variable 's1' returned}} expected-warning {{reference to stack memory associated with local variable 's1' returned}}
14}
15
16const int& g3() {
17  int s1;
18  int &s2 = s1; // expected-note {{binding reference variable 's2' here}}
19  int &s3 = s2; // expected-note {{binding reference variable 's3' here}}
20  return s3; // expected-warning{{Address of stack memory associated with local variable 's1' returned}} expected-warning {{reference to stack memory associated with local variable 's1' returned}}
21}
22
23int get_value();
24
25const int &get_reference1() { return get_value(); } // expected-warning{{Address of stack memory associated with temporary object of type 'int' returned}} expected-warning {{returning reference to local temporary}}
26
27const int &get_reference2() {
28  const int &x = get_value(); // expected-note {{binding reference variable 'x' here}}
29  return x; // expected-warning{{Address of stack memory associated with temporary object of type 'int' returned}} expected-warning {{returning reference to local temporary}}
30}
31
32const int &get_reference3() {
33  const int &x1 = get_value(); // expected-note {{binding reference variable 'x1' here}}
34  const int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
35  return x2; // expected-warning{{Address of stack memory associated with temporary object of type 'int' returned}} expected-warning {{returning reference to local temporary}}
36}
37
38int global_var;
39int *f1() {
40  int &y = global_var;
41  return &y;
42}
43
44int *f2() {
45  int x1;
46  int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
47  return &x2; // expected-warning{{Address of stack memory associated with local variable 'x1' returned}} expected-warning {{address of stack memory associated with local variable 'x1' returned}}
48}
49
50int *f3() {
51  int x1;
52  int *const &x2 = &x1; // expected-note {{binding reference variable 'x2' here}}
53  return x2; // expected-warning {{address of stack memory associated with local variable 'x1' returned}} expected-warning {{Address of stack memory associated with local variable 'x1' returned to caller}}
54}
55
56const int *f4() {
57  const int &x1 = get_value(); // expected-note {{binding reference variable 'x1' here}}
58  const int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
59  return &x2; // expected-warning{{Address of stack memory associated with temporary object of type 'int' returned}} expected-warning {{returning address of local temporary}}
60}
61
62struct S {
63  int x;
64};
65
66int *mf() {
67  S s1;
68  S &s2 = s1; // expected-note {{binding reference variable 's2' here}}
69  int &x = s2.x; // expected-note {{binding reference variable 'x' here}}
70  return &x; // expected-warning{{Address of stack memory associated with local variable 's1' returned}} expected-warning {{address of stack memory associated with local variable 's1' returned}}
71}
72
73void *lf() {
74    label:
75    void *const &x = &&label; // expected-note {{binding reference variable 'x' here}}
76    return x; // expected-warning {{returning address of label, which is local}}
77}
78
79template <typename T>
80struct TS {
81  int *get();
82  int *m() {
83    int *&x = get();
84    return x;
85  }
86};
87
88// rdar://11345441
89int* f5() {
90  int& i = i; // expected-warning {{Assigned value is garbage or undefined}} expected-note {{binding reference variable 'i' here}} expected-warning{{reference 'i' is not yet bound to a value when used within its own initialization}}
91  return &i; // expected-warning {{address of stack memory associated with local variable 'i' returned}}
92}
93
94void *radar13226577() {
95    void *p = &p;
96    return p; // expected-warning {{stack memory associated with local variable 'p' returned to caller}}
97}
98
99namespace rdar13296133 {
100  class ConvertsToBool {
101  public:
102    operator bool() const { return this; }
103  };
104
105  class ConvertsToIntptr {
106  public:
107    operator intptr_t() const { return reinterpret_cast<intptr_t>(this); }
108  };
109
110  class ConvertsToPointer {
111  public:
112    operator const void *() const { return this; }
113  };
114
115  intptr_t returnAsNonLoc() {
116    ConvertsToIntptr obj;
117    return obj; // expected-warning{{Address of stack memory associated with local variable 'obj' returned to caller}}
118  }
119
120  bool returnAsBool() {
121    ConvertsToBool obj;
122    return obj; // no-warning
123  }
124
125  intptr_t returnAsNonLocViaPointer() {
126    ConvertsToPointer obj;
127    return reinterpret_cast<intptr_t>(static_cast<const void *>(obj)); // expected-warning{{Address of stack memory associated with local variable 'obj' returned to caller}}
128  }
129
130  bool returnAsBoolViaPointer() {
131    ConvertsToPointer obj;
132    return obj; // no-warning
133  }
134}
135
136