| 1 |
// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s -Wno-undefined-bool-conversion |
| 2 |
|
| 3 |
typedef __INTPTR_TYPE__ intptr_t; |
| 4 |
|
| 5 |
const 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 |
|
| 10 |
const 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 |
|
| 16 |
const 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 |
|
| 23 |
void g4() { |
| 24 |
static const int &x = 3; // no warning |
| 25 |
} |
| 26 |
|
| 27 |
int get_value(); |
| 28 |
|
| 29 |
const 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}} |
| 30 |
|
| 31 |
const int &get_reference2() { |
| 32 |
const int &x = get_value(); // expected-note {{binding reference variable 'x' here}} |
| 33 |
return x; // expected-warning{{Address of stack memory associated with temporary object of type 'int' lifetime extended by local variable 'x' returned to caller}} expected-warning {{returning reference to local temporary}} |
| 34 |
} |
| 35 |
|
| 36 |
const int &get_reference3() { |
| 37 |
const int &x1 = get_value(); // expected-note {{binding reference variable 'x1' here}} |
| 38 |
const int &x2 = x1; // expected-note {{binding reference variable 'x2' here}} |
| 39 |
return x2; // expected-warning{{Address of stack memory associated with temporary object of type 'int' lifetime extended by local variable 'x1' returned to caller}} expected-warning {{returning reference to local temporary}} |
| 40 |
} |
| 41 |
|
| 42 |
int global_var; |
| 43 |
int *f1() { |
| 44 |
int &y = global_var; |
| 45 |
return &y; |
| 46 |
} |
| 47 |
|
| 48 |
int *f2() { |
| 49 |
int x1; |
| 50 |
int &x2 = x1; // expected-note {{binding reference variable 'x2' here}} |
| 51 |
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}} |
| 52 |
} |
| 53 |
|
| 54 |
int *f3() { |
| 55 |
int x1; |
| 56 |
int *const &x2 = &x1; // expected-note {{binding reference variable 'x2' here}} |
| 57 |
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}} |
| 58 |
} |
| 59 |
|
| 60 |
const int *f4() { |
| 61 |
const int &x1 = get_value(); // expected-note {{binding reference variable 'x1' here}} |
| 62 |
const int &x2 = x1; // expected-note {{binding reference variable 'x2' here}} |
| 63 |
return &x2; // expected-warning{{Address of stack memory associated with temporary object of type 'int' lifetime extended by local variable 'x1' returned to caller}} expected-warning {{returning address of local temporary}} |
| 64 |
} |
| 65 |
|
| 66 |
struct S { |
| 67 |
int x; |
| 68 |
}; |
| 69 |
|
| 70 |
int *mf() { |
| 71 |
S s1; |
| 72 |
S &s2 = s1; // expected-note {{binding reference variable 's2' here}} |
| 73 |
int &x = s2.x; // expected-note {{binding reference variable 'x' here}} |
| 74 |
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}} |
| 75 |
} |
| 76 |
|
| 77 |
void *lf() { |
| 78 |
label: |
| 79 |
void *const &x = &&label; // expected-note {{binding reference variable 'x' here}} |
| 80 |
return x; // expected-warning {{returning address of label, which is local}} |
| 81 |
} |
| 82 |
|
| 83 |
template <typename T> |
| 84 |
struct TS { |
| 85 |
int *get(); |
| 86 |
int *m() { |
| 87 |
int *&x = get(); |
| 88 |
return x; |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
int* f5() { |
| 93 |
int& i = i; // expected-warning {{Assigned value is garbage or undefined}} expected-warning{{reference 'i' is not yet bound to a value when used within its own initialization}} |
| 94 |
return &i; |
| 95 |
} |
| 96 |
|
| 97 |
void *radar13226577() { |
| 98 |
void *p = &p; |
| 99 |
return p; // expected-warning {{stack memory associated with local variable 'p' returned to caller}} |
| 100 |
} |
| 101 |
|
| 102 |
namespace rdar13296133 { |
| 103 |
class ConvertsToBool { |
| 104 |
public: |
| 105 |
operator bool() const { return this; } |
| 106 |
}; |
| 107 |
|
| 108 |
class ConvertsToIntptr { |
| 109 |
public: |
| 110 |
operator intptr_t() const { return reinterpret_cast<intptr_t>(this); } |
| 111 |
}; |
| 112 |
|
| 113 |
class ConvertsToPointer { |
| 114 |
public: |
| 115 |
operator const void *() const { return this; } |
| 116 |
}; |
| 117 |
|
| 118 |
intptr_t returnAsNonLoc() { |
| 119 |
ConvertsToIntptr obj; |
| 120 |
return obj; // expected-warning{{Address of stack memory associated with local variable 'obj' returned to caller}} |
| 121 |
} |
| 122 |
|
| 123 |
bool returnAsBool() { |
| 124 |
ConvertsToBool obj; |
| 125 |
return obj; // no-warning |
| 126 |
} |
| 127 |
|
| 128 |
intptr_t returnAsNonLocViaPointer() { |
| 129 |
ConvertsToPointer obj; |
| 130 |
return reinterpret_cast<intptr_t>(static_cast<const void *>(obj)); // expected-warning{{Address of stack memory associated with local variable 'obj' returned to caller}} |
| 131 |
} |
| 132 |
|
| 133 |
bool returnAsBoolViaPointer() { |
| 134 |
ConvertsToPointer obj; |
| 135 |
return obj; // no-warning |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
void write_stack_address_to(char **q) { |
| 140 |
char local; |
| 141 |
*q = &local; |
| 142 |
// expected-warning@-1 {{Address of stack memory associated with local \ |
| 143 |
variable 'local' is still referred to by the stack variable 'p' upon \ |
| 144 |
returning to the caller}} |
| 145 |
} |
| 146 |
|
| 147 |
void test_stack() { |
| 148 |
char *p; |
| 149 |
write_stack_address_to(&p); |
| 150 |
} |
| 151 |
|
| 152 |
struct C { |
| 153 |
~C() {} // non-trivial class |
| 154 |
}; |
| 155 |
|
| 156 |
C make1() { |
| 157 |
C c; |
| 158 |
return c; // no-warning |
| 159 |
} |
| 160 |
|
| 161 |
void test_copy_elision() { |
| 162 |
C c1 = make1(); |
| 163 |
} |