1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
// RUN: %clang_analyze_cc1 -x c++ -std=c++14 -analyzer-checker=core -analyzer-output=text -verify %s
int initializer1(int &p, int x) {
if (x) { // expected-note{{'x' is 0}}
// expected-note@-1{{Taking false branch}}
p = 1;
return 0;
} else {
return 1; // expected-note {{Returning without writing to 'p'}}
}
}
int param_not_initialized_by_func() {
int outP; // expected-note {{'outP' declared without an initial value}}
int out = initializer1(outP, 0); // expected-note{{Calling 'initializer1'}}
// expected-note@-1{{Returning from 'initializer1'}}
return outP; // expected-note{{Undefined or garbage value returned to caller}}
// expected-warning@-1{{Undefined or garbage value returned to caller}}
}
struct S {
int initialize(int *p, int param) {
if (param) { // expected-note{{'param' is 0}}
// expected-note@-1{{Taking false branch}}
*p = 1;
return 1;
}
return 0; // expected-note{{Returning without writing to '*p'}}
}
};
int use(S *s) {
int p; //expected-note{{'p' declared without an initial value}}
s->initialize(&p, 0); //expected-note{{Calling 'S::initialize'}}
//expected-note@-1{{Returning from 'S::initialize'}}
return p; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
int initializer2(const int &p) {
return 0;
}
int no_msg_const_ref() {
int p; //expected-note{{'p' declared without an initial value}}
initializer2(p);
return p; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
void nested() {}
void init_in_nested_func(int **x) {
*x = 0; // expected-note{{Null pointer value stored to 'y'}}
nested();
} // no-note
int call_init_nested() {
int x = 0;
int *y = &x;
init_in_nested_func(&y); // expected-note{{Calling 'init_in_nested_func'}}
// expected-note@-1{{Returning from 'init_in_nested_func'}}
return *y; //expected-warning{{Dereference of null pointer (loaded from variable 'y')}}
//expected-note@-1{{Dereference of null pointer (loaded from variable 'y')}}
}
struct A {
int x;
int y;
};
void partial_init_by_reference(A &a) {
a.x = 0;
} // expected-note {{Returning without writing to 'a.y'}}
int use_partial_init_by_reference() {
A a;
partial_init_by_reference(a); // expected-note{{Calling 'partial_init_by_reference'}}
// expected-note@-1{{Returning from 'partial_init_by_reference'}}
return a.y; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
struct B : A {
};
void partially_init_inherited_struct(B *b) {
b->x = 0;
} // expected-note{{Returning without writing to 'b->y'}}
int use_partially_init_inherited_struct() {
B b;
partially_init_inherited_struct(&b); // expected-note{{Calling 'partially_init_inherited_struct'}}
// expected-note@-1{{Returning from 'partially_init_inherited_struct'}}
return b.y; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
struct C {
int x;
int y;
C(int pX, int pY) : x(pX) {} // expected-note{{Returning without writing to 'this->y'}}
C(int pX, int pY, bool Flag) {
x = pX;
if (Flag) // expected-note{{Assuming 'Flag' is true}}
// expected-note@-1{{Taking true branch}}
return; // expected-note{{Returning without writing to 'this->y'}}
y = pY;
}
};
int use_constructor() {
C c(0, 0); // expected-note{{Calling constructor for 'C'}}
// expected-note@-1{{Returning from constructor for 'C'}}
return c.y; // expected-note{{Undefined or garbage value returned to caller}}
// expected-warning@-1{{Undefined or garbage value returned to caller}}
}
int coin();
int use_other_constructor() {
C c(0, 0, coin()); // expected-note{{Calling constructor for 'C'}}
// expected-note@-1{{Returning from constructor for 'C'}}
return c.y; // expected-note{{Undefined or garbage value returned to caller}}
// expected-warning@-1{{Undefined or garbage value returned to caller}}
}
struct D {
void initialize(int *);
};
void D::initialize(int *p) {
} // expected-note{{Returning without writing to '*p'}}
int use_d_initializer(D* d) {
int p; // expected-note {{'p' declared without an initial value}}
d->initialize(&p); // expected-note{{Calling 'D::initialize'}}
// expected-note@-1{{Returning from 'D::initialize'}}
return p; // expected-note{{Undefined or garbage value returned to caller}}
// expected-warning@-1{{Undefined or garbage value returned to caller}}
}
struct S2 {
int x;
};
int pointerreference(S2* &s) {
if (coin()) // expected-note{{Assuming the condition is true}}
// expected-note@-1{{Taking true branch}}
return 1; // expected-note{{Returning without writing to 's->x'}}
s->x = 0;
return 0;
}
int usepointerreference() {
S2 s;
S2* p = &s;
pointerreference(p); //expected-note{{Calling 'pointerreference'}}
//expected-note@-1{{Returning from 'pointerreference'}}
return s.x; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
void *has_no_argument_and_returns_null(void) {
return 0;
}
void rdar40335545() {
int local; // expected-note{{}}
void (*takes_int_ptr_argument)(int *) = (void (*)(int*))has_no_argument_and_returns_null;
takes_int_ptr_argument(&local); // no-crash
int useLocal = local; //expected-warning{{}}
//expected-note@-1{{}}
(void)useLocal;
}
////////
struct HasRef {
int &a;
HasRef(int &a) : a(a) {}
};
void maybeInitialize(const HasRef &&pA) {
if (coin()) // expected-note{{Assuming the condition is false}}
// expected-note@-1{{Taking false branch}}
pA.a = 120;
} // expected-note{{Returning without writing to 'pA.a'}}
int useMaybeInitializerWritingIntoField() {
int z; // expected-note{{'z' declared without an initial value}}
maybeInitialize(HasRef(z)); // expected-note{{Calling constructor for 'HasRef'}}
// expected-note@-1{{Returning from constructor for 'HasRef'}}
// expected-note@-2{{Calling 'maybeInitialize'}}
// expected-note@-3{{Returning from 'maybeInitialize'}}
return z; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
////////
struct HasRefToItself {
HasRefToItself &Ref; // no infinite loop
int &z;
HasRefToItself(int &z) : Ref(*this), z(z) {}
};
void maybeInitialize(const HasRefToItself &&pA) {
if (coin()) // expected-note{{Assuming the condition is false}}
// expected-note@-1{{Taking false branch}}
pA.z = 120;
} // expected-note{{Returning without writing to 'pA.Ref.z'}}
int useMaybeInitializerWritingIntoFieldWithRefToItself() {
int z; // expected-note{{'z' declared without an initial value}}
maybeInitialize(HasRefToItself(z)); // expected-note{{Calling constructor for 'HasRefToItself'}}
// expected-note@-1{{Returning from constructor for 'HasRefToItself'}}
// expected-note@-2{{Calling 'maybeInitialize'}}
// expected-note@-3{{Returning from 'maybeInitialize'}}
return z; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
////
void maybeInitialize(const HasRef *pA) {
if (coin()) // expected-note{{Assuming the condition is false}}
// expected-note@-1{{Taking false branch}}
pA->a = 120;
} // expected-note{{Returning without writing to 'pA->a'}}
int useMaybeInitializerStructByPointer() {
int z; // expected-note{{'z' declared without an initial value}}
HasRef wrapper(z); // expected-note{{Calling constructor for 'HasRef'}}
// expected-note@-1{{Returning from constructor for 'HasRef'}}
maybeInitialize(&wrapper); // expected-note{{Calling 'maybeInitialize'}}
// expected-note@-1{{Returning from 'maybeInitialize'}}
return z; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
////////
struct HasParentWithRef : public HasRef {
HasParentWithRef(int &a) : HasRef(a) {} // expected-note{{Calling constructor for 'HasRef'}}
// expected-note@-1{{Returning from constructor for 'HasRef'}}
};
void maybeInitializeWithParent(const HasParentWithRef &pA) {
if (coin()) // expected-note{{Assuming the condition is false}}
// expected-note@-1{{Taking false branch}}
pA.a = 120;
} // expected-note{{Returning without writing to 'pA.a'}}
int useMaybeInitializerWritingIntoParentField() {
int z; // expected-note{{'z' declared without an initial value}}
maybeInitializeWithParent(HasParentWithRef(z)); // expected-note{{Calling constructor for 'HasParentWithRef'}}
// expected-note@-1{{Returning from constructor for 'HasParentWithRef'}}
// expected-note@-2{{Calling 'maybeInitializeWithParent'}}
// expected-note@-3{{Returning from 'maybeInitializeWithParent'}}
return z; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
////////
struct HasIndirectRef {
HasRef &Ref;
HasIndirectRef(HasRef &Ref) : Ref(Ref) {}
};
void maybeInitializeIndirectly(const HasIndirectRef &pA) {
if (coin()) // expected-note{{Assuming the condition is false}}
// expected-note@-1{{Taking false branch}}
pA.Ref.a = 120;
} // expected-note{{Returning without writing to 'pA.Ref.a'}}
int useMaybeInitializeIndirectly() {
int z; // expected-note{{'z' declared without an initial value}}
HasRef r(z); // expected-note{{Calling constructor for 'HasRef'}}
// expected-note@-1{{Returning from constructor for 'HasRef'}}
maybeInitializeIndirectly(HasIndirectRef(r)); // expected-note{{Calling 'maybeInitializeIndirectly'}}
// expected-note@-1{{Returning from 'maybeInitializeIndirectly'}}
return z; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
////////
struct HasIndirectRefByValue {
HasRef Ref;
HasIndirectRefByValue(HasRef Ref) : Ref(Ref) {}
};
void maybeInitializeIndirectly(const HasIndirectRefByValue &pA) {
if (coin()) // expected-note{{Assuming the condition is false}}
// expected-note@-1{{Taking false branch}}
pA.Ref.a = 120;
} // expected-note{{Returning without writing to 'pA.Ref.a'}}
int useMaybeInitializeIndirectlyIndirectRefByValue() {
int z; // expected-note{{'z' declared without an initial value}}
HasRef r(z); // expected-note{{Calling constructor for 'HasRef'}}
// expected-note@-1{{Returning from constructor for 'HasRef'}}
maybeInitializeIndirectly(HasIndirectRefByValue(r)); // expected-note{{Calling 'maybeInitializeIndirectly'}}
// expected-note@-1{{Returning from 'maybeInitializeIndirectly'}}
return z; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
////////
struct HasIndirectPointerRef {
HasRef *Ref;
HasIndirectPointerRef(HasRef *Ref) : Ref(Ref) {}
};
void maybeInitializeIndirectly(const HasIndirectPointerRef &pA) {
if (coin()) // expected-note{{Assuming the condition is false}}
// expected-note@-1{{Taking false branch}}
pA.Ref->a = 120;
} // expected-note{{Returning without writing to 'pA.Ref->a'}}
int useMaybeInitializeIndirectlyWithPointer() {
int z; // expected-note{{'z' declared without an initial value}}
HasRef r(z); // expected-note{{Calling constructor for 'HasRef'}}
// expected-note@-1{{Returning from constructor for 'HasRef'}}
maybeInitializeIndirectly(HasIndirectPointerRef(&r)); // expected-note{{Calling 'maybeInitializeIndirectly'}}
// expected-note@-1{{Returning from 'maybeInitializeIndirectly'}}
return z; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
////////
struct HasFieldA {
int x;
};
struct HasFieldB {
int x;
};
void maybeInitializeHasField(HasFieldA *b) {
if (coin()) // expected-note{{Assuming the condition is false}}
// expected-note@-1{{Taking false branch}}
((HasFieldB*)b)->x = 120;
}
int forceElementRegionApperence() {
HasFieldA a;
maybeInitializeHasField(&a); // expected-note{{Calling 'maybeInitializeHasField'}}
// expected-note@-1{{Returning from 'maybeInitializeHasField'}}
return ((HasFieldB*)&a)->x; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
////////
struct HasForgottenField {
int x;
HasForgottenField() {} // expected-note{{Returning without writing to 'this->x'}}
};
// Test that tracking across exclamation mark works.
bool tracksThroughExclamationMark() {
HasForgottenField a; // expected-note{{Calling default constructor for 'HasForgottenField'}}
// expected-note@-1{{Returning from default constructor for 'HasForgottenField'}}
return !a.x; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
|