diff --git a/infer/tests/codetoanalyze/cpp/ownership/issues.exp b/infer/tests/codetoanalyze/cpp/ownership/issues.exp index ad0f24851..d717f2944 100644 --- a/infer/tests/codetoanalyze/cpp/ownership/issues.exp +++ b/infer/tests/codetoanalyze/cpp/ownership/issues.exp @@ -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] diff --git a/infer/tests/codetoanalyze/cpp/ownership/reference_wrapper.cpp b/infer/tests/codetoanalyze/cpp/ownership/reference_wrapper.cpp new file mode 100644 index 000000000..ccb7836b5 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/ownership/reference_wrapper.cpp @@ -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 +} diff --git a/infer/tests/codetoanalyze/cpp/ownership/use_after_destructor.cpp b/infer/tests/codetoanalyze/cpp/ownership/use_after_destructor.cpp index cea38ccb5..22d8c7ed8 100644 --- a/infer/tests/codetoanalyze/cpp/ownership/use_after_destructor.cpp +++ b/infer/tests/codetoanalyze/cpp/ownership/use_after_destructor.cpp @@ -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; +}