You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
51 lines
1.1 KiB
8 years ago
|
/*
|
||
6 years ago
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
8 years ago
|
*
|
||
7 years ago
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
8 years ago
|
*/
|
||
|
void initialized_no_dangling_ok() {
|
||
|
int* p = new int(42);
|
||
|
delete p;
|
||
|
}
|
||
|
|
||
|
void uninitialized_dangling_bad() {
|
||
|
int* p;
|
||
|
delete p;
|
||
|
}
|
||
8 years ago
|
|
||
|
struct TestDangling {
|
||
|
int* p;
|
||
|
// Field p is deliberately left uninitialized
|
||
|
TestDangling() {}
|
||
|
// This constructor is deliberately left unimplemented
|
||
|
// to make sure Infer treats undefined function conservatively
|
||
|
TestDangling(int);
|
||
|
};
|
||
|
|
||
|
int known_ctor_dangling_bad() {
|
||
|
auto t0 = new TestDangling();
|
||
|
int ret = *(t0->p); // should report a dangling pointer dereference here as p
|
||
|
// is dangling
|
||
|
delete t0;
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
int unknown_ctor_assume_no_dangling_ok() {
|
||
|
auto t1 = new TestDangling(0);
|
||
|
int ret =
|
||
|
*(t1->p); // TestDangling(0) is not known so p may have been initialized
|
||
|
delete t1;
|
||
|
return ret;
|
||
|
}
|
||
7 years ago
|
|
||
|
short union_ok(int* param) {
|
||
|
union {
|
||
|
int* a;
|
||
|
short* b;
|
||
|
} u;
|
||
|
u.a = param;
|
||
|
short* p = u.b;
|
||
|
return *p;
|
||
|
}
|