From 0bbeb852950c5b0e5f54007f58fa03481aa69625 Mon Sep 17 00:00:00 2001 From: Daiva Naudziuniene Date: Fri, 14 Sep 2018 07:52:41 -0700 Subject: [PATCH] [ownership] Stack reference wrapper example Reviewed By: jvillard Differential Revision: D9771647 fbshipit-source-id: 40c66659d --- .../cpp/ownership/reference_wrapper.cpp | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/infer/tests/codetoanalyze/cpp/ownership/reference_wrapper.cpp b/infer/tests/codetoanalyze/cpp/ownership/reference_wrapper.cpp index ccb7836b5..2561a128e 100644 --- a/infer/tests/codetoanalyze/cpp/ownership/reference_wrapper.cpp +++ b/infer/tests/codetoanalyze/cpp/ownership/reference_wrapper.cpp @@ -16,18 +16,33 @@ struct A { ~A() { delete b; } }; -struct ReferenceWrapper { - ReferenceWrapper(A& a) : b(a.getb()){}; +struct ReferenceWrapperHeap { + ReferenceWrapperHeap(A& a) : b(a.getb()){}; B* b; }; -ReferenceWrapper getwrapper() { +ReferenceWrapperHeap getwrapperHeap() { 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(); +int FN_reference_wrapper_heap_bad() { + ReferenceWrapperHeap rw = getwrapperHeap(); + return rw.b->f; // we want to report use after lifetime bug here +} + +struct ReferenceWrapperStack { + ReferenceWrapperStack(B& bref) : b(&bref){}; + B* b; +}; + +ReferenceWrapperStack getwrapperStack() { + B b(1); + return b; +} + +int FN_reference_wrapper_stack_bad() { + ReferenceWrapperStack rw = getwrapperStack(); return rw.b->f; // we want to report use after lifetime bug here }