From ad96f9361097d7933c0e972405eec74318e666fe Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 9 Oct 2016 00:29:18 -0400 Subject: [PATCH] Add file-line information to test failure messages. For EXPECT_UAF, attempt to catch exceptions in non-ZCPOINTER_TRACK_REFS mode, in case the stdlib throws one. --- Makefile | 10 ++++++++-- test.cc | 22 ++++++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 935231b..2245bf2 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,24 @@ CXX := clang++ CXXFLAGS := -g -std=c++11 +ifeq ($(OPTIMIZED),1) + CXXFLAGS += -O2 +endif + FILES := test.cc zcpointer.cc +DEPS := $(FILES) zcpointer.h Makefile + all: test-zc test-tr test: test-zc test-tr ./test-zc ./test-tr -test-zc: $(FILES) zcpointer.h +test-zc: $(DEPS) $(CXX) $(CXXFLAGS) $(FILES) -o $@ -test-tr: $(FILES) zcpointer.h +test-tr: $(DEPS) $(CXX) -DZCPOINTER_TRACK_REFS=1 $(CXXFLAGS) $(FILES) -o $@ clean: diff --git a/test.cc b/test.cc index 8769f4c..e8cbdf2 100644 --- a/test.cc +++ b/test.cc @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include "zcpointer.h" @@ -24,14 +25,23 @@ class C { void DoThing() {} }; -#define EXPECT(expr) do { if (!(expr)) { throw std::logic_error(#expr); } } while(0) +class TestFailure : public std::logic_error { + public: + using std::logic_error::logic_error; +}; + +#define STRING(x) QUOTE(x) +#define QUOTE(x) #x +#define AT_FILE_LINE " @ " __FILE__ ":" STRING(__LINE__) + +#define EXPECT(expr) do { if (!(expr)) { throw TestFailure(#expr AT_FILE_LINE); } } while(0) #if defined(ZCPOINTER_TRACK_REFS) && ZCPOINTER_TRACK_REFS #define EXPECT_UAF(expr) do { \ try { \ (expr); \ - throw std::logic_error("Expected use-after-free: " #expr); \ + throw TestFailure("Expected use-after-free: " #expr AT_FILE_LINE); \ } catch (zc::UseAfterFreeError) {} \ } while(0) @@ -39,7 +49,11 @@ class C { #define EXPECT_UAF(expr) do { \ std::cout << ">>> ZCPOINTER_TRACK_REFS not enabled, cannot catch UAF" << std::endl; \ - (expr); \ + try { \ + (expr); \ + } catch (std::logic_error& e) { \ + std::cout << ">>> Caught error: " << typeid(e).name() << ": " << e.what() << std::endl; \ + } \ } while(0) #endif @@ -176,7 +190,7 @@ int main() { try { test.test(); std::cout << "+++ PASS " << test.name << " +++" << std::endl; - } catch (const std::logic_error& e) { + } catch (const TestFailure& e) { passed = false; std::cout << "!!! FAIL " << test.name << ": Assertion failure: " << e.what() << " ===" << std::endl; -- 2.43.5