From 5daa22dcc37628ffdf1fb65fea9ff9436d133142 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Tue, 20 May 2014 18:49:28 +0100 Subject: [PATCH] Fixed dereferenced null issue --- include/internal/catch_capture.hpp | 5 ++++- include/internal/catch_context.h | 4 ++-- include/internal/catch_context_impl.hpp | 18 ++++++------------ include/internal/catch_runner_impl.hpp | 4 ++-- include/internal/catch_section.hpp | 4 ++-- 5 files changed, 16 insertions(+), 19 deletions(-) diff --git a/include/internal/catch_capture.hpp b/include/internal/catch_capture.hpp index 1578f996..a86bb2c1 100644 --- a/include/internal/catch_capture.hpp +++ b/include/internal/catch_capture.hpp @@ -25,7 +25,10 @@ namespace Catch { inline IResultCapture& getResultCapture() { - return getCurrentContext().getResultCapture(); + if( IResultCapture* capture = getCurrentContext().getResultCapture() ) + return *capture; + else + throw std::logic_error( "No result capture instance" ); } template diff --git a/include/internal/catch_context.h b/include/internal/catch_context.h index db0704f1..9304b303 100644 --- a/include/internal/catch_context.h +++ b/include/internal/catch_context.h @@ -28,8 +28,8 @@ namespace Catch { { virtual ~IContext(); - virtual IResultCapture& getResultCapture() = 0; - virtual IRunner& getRunner() = 0; + virtual IResultCapture* getResultCapture() = 0; + virtual IRunner* getRunner() = 0; virtual size_t getGeneratorIndex( std::string const& fileInfo, size_t totalSize ) = 0; virtual bool advanceGeneratorsForCurrentTest() = 0; virtual Ptr getConfig() const = 0; diff --git a/include/internal/catch_context_impl.hpp b/include/internal/catch_context_impl.hpp index c29a07f3..75bbfb0c 100644 --- a/include/internal/catch_context_impl.hpp +++ b/include/internal/catch_context_impl.hpp @@ -22,17 +22,11 @@ namespace Catch { void operator=( Context const& ); public: // IContext - virtual IResultCapture& getResultCapture() { - if( m_resultCapture ) - return *m_resultCapture; - else - throw std::logic_error( "No result capture instance" ); + virtual IResultCapture* getResultCapture() { + return m_resultCapture; } - virtual IRunner& getRunner() { - if( m_runner ) - return *m_runner; - else - throw std::logic_error( "No runner instance" ); + virtual IRunner* getRunner() { + return m_runner; } virtual size_t getGeneratorIndex( std::string const& fileInfo, size_t totalSize ) { return getGeneratorsForCurrentTest() @@ -63,7 +57,7 @@ namespace Catch { private: IGeneratorsForTest* findGeneratorsForCurrentTest() { - std::string testName = getResultCapture().getCurrentTestName(); + std::string testName = getResultCapture()->getCurrentTestName(); std::map::const_iterator it = m_generatorsByTestName.find( testName ); @@ -75,7 +69,7 @@ namespace Catch { IGeneratorsForTest& getGeneratorsForCurrentTest() { IGeneratorsForTest* generators = findGeneratorsForCurrentTest(); if( !generators ) { - std::string testName = getResultCapture().getCurrentTestName(); + std::string testName = getResultCapture()->getCurrentTestName(); generators = createGeneratorsForTest(); m_generatorsByTestName.insert( std::make_pair( testName, generators ) ); } diff --git a/include/internal/catch_runner_impl.hpp b/include/internal/catch_runner_impl.hpp index 1eca9d51..b4f53d55 100644 --- a/include/internal/catch_runner_impl.hpp +++ b/include/internal/catch_runner_impl.hpp @@ -63,8 +63,8 @@ namespace Catch { m_activeTestCase( NULL ), m_config( config ), m_reporter( reporter ), - m_prevRunner( &m_context.getRunner() ), - m_prevResultCapture( &m_context.getResultCapture() ), + m_prevRunner( m_context.getRunner() ), + m_prevResultCapture( m_context.getResultCapture() ), m_prevConfig( m_context.getConfig() ) { m_context.setRunner( this ); diff --git a/include/internal/catch_section.hpp b/include/internal/catch_section.hpp index 96fc8320..a71f75b6 100644 --- a/include/internal/catch_section.hpp +++ b/include/internal/catch_section.hpp @@ -19,14 +19,14 @@ namespace Catch { std::string const& name, std::string const& description ) : m_info( name, description, lineInfo ), - m_sectionIncluded( getCurrentContext().getResultCapture().sectionStarted( m_info, m_assertions ) ) + m_sectionIncluded( getResultCapture().sectionStarted( m_info, m_assertions ) ) { m_timer.start(); } Section::~Section() { if( m_sectionIncluded ) - getCurrentContext().getResultCapture().sectionEnded( m_info, m_assertions, m_timer.getElapsedSeconds() ); + getResultCapture().sectionEnded( m_info, m_assertions, m_timer.getElapsedSeconds() ); } // This indicates whether the section should be executed or not