From 6c8ec521a8b4a51d2631513ae15fb8975ca01471 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Mon, 30 Apr 2018 01:10:45 +0200 Subject: [PATCH] Clarify that base/reference causes stack-use-after-scope --- tests/base/reference.cc | 67 ++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/tests/base/reference.cc b/tests/base/reference.cc index 32c1c3971d..ac83b41c51 100644 --- a/tests/base/reference.cc +++ b/tests/base/reference.cc @@ -60,44 +60,37 @@ int main() initlog(); - try - { - Test a("A"); - const Test &b("B"); + Test a("A"); + const Test &b("B"); - SmartPointer r(&a, "Test R"); - SmartPointer s(&a, "const Test S"); + SmartPointer r(&a, "Test R"); + SmartPointer s(&a, "const Test S"); // SmartPointer t=&b; // this one should not work - SmartPointer t(const_cast(&b), "Test T"); - SmartPointer u(&b, "const Test"); - - - deallog << "a "; - a.f(); // should print "mutable", since #a# is not const - deallog << "b "; - b.f(); // should print "const", since #b# is const - deallog << "r "; - r->f(); // should print "mutable", since it points to the non-const #a# - deallog << "s "; - s->f(); // should print "const", since it points to the const #b# - // but we made it const - deallog << "t "; - t->f(); // should print "mutable", since #b# is const, but - // we casted the constness away - deallog << "u "; - u->f(); // should print "const" since #b# is const - // Now try if subscriptor works - Test c("C"); - r = &c; - Test d("D"); - r = &d; - // Destruction of "Test R" will cause a spurious ExcNotUsed here, - // since D was deleted first - } - catch (ExceptionBase &e) - { - deallog << e.get_exc_name() << std::endl; - } - + SmartPointer t(const_cast(&b), "Test T"); + SmartPointer u(&b, "const Test"); + + + deallog << "a "; + a.f(); // should print "mutable", since #a# is not const + deallog << "b "; + b.f(); // should print "const", since #b# is const + deallog << "r "; + r->f(); // should print "mutable", since it points to the non-const #a# + deallog << "s "; + s->f(); // should print "const", since it points to the const #b# + // but we made it const + deallog << "t "; + t->f(); // should print "mutable", since #b# is const, but + // we casted the constness away + deallog << "u "; + u->f(); // should print "const" since #b# is const + // Now try if subscriptor works + Test c("C"); + r = &c; + Test d("D"); + r = &d; + // Destruction of "Test R" will cause a spurious ExcNotUsed here, + // since D was deleted first. This shows up in address sanitizers + // as stack-use-after-scope, but we can't do anything about it. } -- 2.39.5