diff options
| author | Phil Nash | 2019-05-21 00:04:19 +0100 |
|---|---|---|
| committer | Phil Nash | 2019-05-21 00:04:44 +0100 |
| commit | 96355da34edf51cc813400115984050bd7c50630 (patch) | |
| tree | cb154679a962f6c437e8d169a97516be574e5905 /include/internal/catch_stringref.cpp | |
| parent | 71fce429afcebba7c32b6c46aced1013c6df9c0b (diff) | |
| download | catch3-96355da34edf51cc813400115984050bd7c50630.tar.gz | |
StringRef no longer repoints m_start to m_data after c_str() on a substring.
This fixes an issue where a self-assignment of a StringRef copy would point into internally (and now dangling) data.
(now self-assignment check is no longer needed)
Diffstat (limited to 'include/internal/catch_stringref.cpp')
| -rw-r--r-- | include/internal/catch_stringref.cpp | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/include/internal/catch_stringref.cpp b/include/internal/catch_stringref.cpp index 9e9095e4..90a320c9 100644 --- a/include/internal/catch_stringref.cpp +++ b/include/internal/catch_stringref.cpp @@ -39,9 +39,11 @@ namespace Catch { } auto StringRef::c_str() const -> char const* { - if( isSubstring() ) - const_cast<StringRef*>( this )->takeOwnership(); - return m_start; + if( !isSubstring() ) + return m_start; + + const_cast<StringRef *>( this )->takeOwnership(); + return m_data; } auto StringRef::currentData() const noexcept -> char const* { return m_start; @@ -59,7 +61,6 @@ namespace Catch { m_data = new char[m_size+1]; memcpy( m_data, m_start, m_size ); m_data[m_size] = '\0'; - m_start = m_data; } } auto StringRef::substr( size_type start, size_type size ) const noexcept -> StringRef { |