From 146602ed4a17d3f0314cd5294435a56e6f0ce1e4 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 8 Oct 2016 17:56:52 -0400 Subject: [PATCH] Throw a custom exception type and attempt to catch it in the test program. This allows all the tests to run. It is possible to inspect the exception by breaking on __cxa_throw. --- test.cc | 22 +++++++++++++++++++++- zcpointer.cc | 4 +++- zcpointer.h | 6 ++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/test.cc b/test.cc index 4ea07e0..23c310a 100644 --- a/test.cc +++ b/test.cc @@ -66,6 +66,26 @@ void TestPtr() { ref->DoThing(); } +#define TEST_FUNC(fn) { #fn , Test##fn } + int main() { - TestPtr(); + struct { + const char* name; + void (*test)(); + } kTests[] = { + TEST_FUNC(Reset), + TEST_FUNC(Move), + TEST_FUNC(Ptr), + }; + + for (const auto& test : kTests) { + std::cout << "=== BEGIN " << test.name << " ===" << std::endl; + try { + test.test(); + std::cout << "=== FAIL " << test.name + << ": Did not receive UseAfterFreeException ===" << std::endl; + } catch (zc::UseAfterFreeError) { + std::cout << "=== PASS " << test.name << " ===" << std::endl; + } + } } diff --git a/zcpointer.cc b/zcpointer.cc index 1dc22ba..4f4afdf 100644 --- a/zcpointer.cc +++ b/zcpointer.cc @@ -14,11 +14,13 @@ #include +#include "zcpointer.h" + namespace zc { namespace internal { void RaiseUseAfterFree(const char* error) { - throw std::logic_error(error); + throw zc::UseAfterFreeError(error); } } // namespace internal diff --git a/zcpointer.h b/zcpointer.h index 3155051..10251c1 100644 --- a/zcpointer.h +++ b/zcpointer.h @@ -15,9 +15,15 @@ #include #include #include +#include namespace zc { +class UseAfterFreeError : public std::logic_error { + public: + using std::logic_error::logic_error; +}; + #if defined(ZCPOINTER_TRACK_REFS) && ZCPOINTER_TRACK_REFS template class ref; -- 2.22.5