mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 20:27:11 +01:00 
			
		
		
		
	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)
This commit is contained in:
		| @@ -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 { | ||||
|   | ||||
| @@ -71,12 +71,12 @@ namespace Catch { | ||||
|         } | ||||
|  | ||||
|         auto operator = ( StringRef const &other ) noexcept -> StringRef& { | ||||
|             if( this != &other ) { | ||||
| //            if( this != &other ) { | ||||
|                 delete[] m_data; | ||||
|                 m_data = nullptr; | ||||
|                 m_start = other.m_start; | ||||
|                 m_size = other.m_size; | ||||
|             } | ||||
|   //          } | ||||
|             return *this; | ||||
|         } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Phil Nash
					Phil Nash