Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Fix StringRef self-assignment after substring

Thanks to Alex Tkachenko for spotting it.
Commit
3bce8ba14b5dfb55c0127b8ad9eddf5a92b662c0
Author
Phil Nash ·
Parents
e680c4b9fb1d

Files · Download .tar.gz

Changes

 include/internal/catch_stringref.h                    | 10 ++++++----
 projects/SelfTest/IntrospectiveTests/String.tests.cpp |  5 +++++
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/include/internal/catch_stringref.h b/include/internal/catch_stringref.h
index 64f595b3..4e41bd47 100644
--- a/include/internal/catch_stringref.h
+++ b/include/internal/catch_stringref.h
@@ -71,10 +71,12 @@ namespace Catch {
         }
 
         auto operator = ( StringRef const &other ) noexcept -> StringRef& {
-            delete[] m_data;
-            m_data = nullptr;
-            m_start = other.m_start;
-            m_size = other.m_size;
+            if( this != &other ) {
+                delete[] m_data;
+                m_data = nullptr;
+                m_start = other.m_start;
+                m_size = other.m_size;
+            }
             return *this;
         }
 
diff --git a/projects/SelfTest/IntrospectiveTests/String.tests.cpp b/projects/SelfTest/IntrospectiveTests/String.tests.cpp
index cb2c350c..1a3bf6d2 100644
--- a/projects/SelfTest/IntrospectiveTests/String.tests.cpp
+++ b/projects/SelfTest/IntrospectiveTests/String.tests.cpp
@@ -92,6 +92,11 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
             REQUIRE( isOwned( ss ) );
 
             REQUIRE( ss.currentData() != s.currentData() ); // different pointer value
+
+            SECTION( "Self-assignment after substring" ) {
+                ss = ss;
+                REQUIRE(isOwned(ss) == true);
+            }
         }
 
         SECTION( "non-zero-based substring") {