[ownership] More examples

Reviewed By: jvillard

Differential Revision: D9684636

fbshipit-source-id: c92b1b9ec
master
Daiva Naudziuniene 6 years ago committed by Facebook Github Bot
parent e2c08e4085
commit e5b38a42d8

@ -17,5 +17,8 @@ codetoanalyze/cpp/ownership/use_after_delete.cpp, reassign_field_of_deleted_bad,
codetoanalyze/cpp/ownership/use_after_delete.cpp, return_deleted_bad, 3, USE_AFTER_LIFETIME, no_bucket, ERROR, [End of variable lifetime,Use of invalid variable]
codetoanalyze/cpp/ownership/use_after_delete.cpp, use_in_branch_bad, 4, USE_AFTER_LIFETIME, no_bucket, ERROR, [End of variable lifetime,Use of invalid variable]
codetoanalyze/cpp/ownership/use_after_delete.cpp, use_in_loop_bad, 4, USE_AFTER_LIFETIME, no_bucket, ERROR, [End of variable lifetime,Use of invalid variable]
codetoanalyze/cpp/ownership/use_after_destructor.cpp, FP_destructor_order_empty_destructor_ok, 4, USE_AFTER_LIFETIME, no_bucket, ERROR, [End of variable lifetime,Use of invalid variable]
codetoanalyze/cpp/ownership/use_after_destructor.cpp, destructor_order_bad, 4, USE_AFTER_LIFETIME, no_bucket, ERROR, [End of variable lifetime,Use of invalid variable]
codetoanalyze/cpp/ownership/use_after_destructor.cpp, double_destructor_bad, 5, USE_AFTER_LIFETIME, no_bucket, ERROR, [End of variable lifetime,Use of invalid variable]
codetoanalyze/cpp/ownership/use_after_destructor.cpp, use_after_destructor_bad, 3, USE_AFTER_LIFETIME, no_bucket, ERROR, [End of variable lifetime,Use of invalid variable]
codetoanalyze/cpp/ownership/use_after_destructor.cpp, use_after_scope4_bad, 6, USE_AFTER_LIFETIME, no_bucket, ERROR, [End of variable lifetime,Use of invalid variable]

@ -0,0 +1,33 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
struct B {
B(int v) : f(v){};
int f;
};
struct A {
A(int f) { b = new B(f); }
B* b;
B* getb() { return b; };
~A() { delete b; }
};
struct ReferenceWrapper {
ReferenceWrapper(A& a) : b(a.getb()){};
B* b;
};
ReferenceWrapper getwrapper() {
A a(1);
return a; // We store a.b in ReferenceWrapper, but we delete a.b in the
// destructor of A
}
int FN_reference_wrapper_bad() {
ReferenceWrapper rw = getwrapper();
return rw.b->f; // we want to report use after lifetime bug here
}

@ -162,3 +162,54 @@ void destructor_in_loop_ok() {
S s(1);
}
}
int FN_use_after_scope3_bad() {
int* p;
{
int value = 3;
p = &value;
} // we do not know in the plugin that value is out of scope
return *p;
}
struct C {
C(int v) : f(v){};
~C();
int f;
};
int use_after_scope4_bad() {
C* pc;
{
C c(3);
pc = &c;
}
return pc->f;
}
struct B {
~B();
};
struct A {
~A() { (void)*f; }
const B* f;
};
void destructor_order_bad() {
A a;
B b;
a.f = &b;
}
struct A2 {
~A2() {}
const B* f;
};
// need interprocedural analysis to fix this
void FP_destructor_order_empty_destructor_ok() {
A2 a;
B b;
a.f = &b;
}

Loading…
Cancel
Save