From b40680a32a1b50dfb3d228023f5f5ca44388a1f8 Mon Sep 17 00:00:00 2001 From: Mathieu Champlon Date: Sun, 9 Aug 2015 10:30:11 +0200 Subject: [PATCH] Fixed crash when comparing null pointers to const char* arguments in matcher --- doc/changelog.qbk | 1 + include/turtle/matcher.hpp | 4 +++- test/test_matcher.cpp | 6 +++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/doc/changelog.qbk b/doc/changelog.qbk index f5dcd63..c66b172 100644 --- a/doc/changelog.qbk +++ b/doc/changelog.qbk @@ -11,6 +11,7 @@ Not yet released * Fixed missing thread synchronization in mock::sequence +* Fixed crash when comparing null pointers to const char* arguments in matcher [endsect] diff --git a/include/turtle/matcher.hpp b/include/turtle/matcher.hpp index 162301b..1973099 100644 --- a/include/turtle/matcher.hpp +++ b/include/turtle/matcher.hpp @@ -48,7 +48,9 @@ namespace mock {} bool operator()( const char* actual ) { - return std::strcmp( actual, expected_ ) == 0; + return actual == expected_ + || actual && expected_ + && std::strcmp( actual, expected_ ) == 0; } friend std::ostream& operator<<( std::ostream& s, const matcher& m ) diff --git a/test/test_matcher.cpp b/test/test_matcher.cpp index c28648e..5ccf459 100644 --- a/test/test_matcher.cpp +++ b/test/test_matcher.cpp @@ -54,7 +54,11 @@ BOOST_FIXTURE_TEST_CASE( const_char_pointer_and_const_char_pointer_can_be_compar const char* expected = "same text"; BOOST_CHECK( match( expected, actual ) ); const char* unexpected = "different text"; - BOOST_CHECK( ! match( actual, unexpected ) ); + BOOST_CHECK( ! match( unexpected, actual ) ); + const char* null = 0; + BOOST_CHECK( ! match( expected, null ) ); + BOOST_CHECK( ! match( null, actual ) ); + BOOST_CHECK( match( null, null ) ); } BOOST_FIXTURE_TEST_CASE( const_char_pointer_and_string_literal_can_be_compared, fixture )