From 88e720bfc1d1b6918c0411fbedfd2f739af2668c Mon Sep 17 00:00:00 2001 From: nillerusr Date: Wed, 25 Aug 2021 12:10:12 +0300 Subject: [PATCH] utlvector: fix undefined behavior --- public/tier1/utlvector.h | 41 ++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/public/tier1/utlvector.h b/public/tier1/utlvector.h index 6fffc6a2..7fa9687e 100644 --- a/public/tier1/utlvector.h +++ b/public/tier1/utlvector.h @@ -182,6 +182,7 @@ public: // Purges the list and calls delete on each element in it. void PurgeAndDeleteElements(); + void PurgeAndDeleteElementsArray(); // Compacts the vector to the number of elements actually in use void Compact(); @@ -475,6 +476,18 @@ public: } } + void PurgeAndDeleteElementsArray() + { + if ( m_pData != StaticData() ) + { + for( int i=0; i < m_pData->m_Size; i++ ) + { + delete[] Element(i); + } + RemoveAll(); + } + } + void FastRemove( int elem ) { Assert( IsValidIndex(elem) ); @@ -1413,6 +1426,17 @@ inline void CUtlVector::PurgeAndDeleteElements() Purge(); } +template< typename T, class A > +inline void CUtlVector::PurgeAndDeleteElementsArray() +{ + for( int i=0; i < m_Size; i++ ) + { + delete[] Element(i); + } + RemoveAll(); +} + + template< typename T, class A > inline void CUtlVector::Compact() { @@ -1441,23 +1465,16 @@ void CUtlVector::Validate( CValidator &validator, char *pchName ) } #endif // DBGFLAG_VALIDATE -// A vector class for storing pointers, so that the elements pointed to by the pointers are deleted -// on exit. -template class CUtlVectorAutoPurge : public CUtlVector< T, CUtlMemory< T, int> > +// easy string list class with dynamically allocated strings. For use with V_SplitString, etc. +// Frees the dynamic strings in destructor. +class CUtlStringList : public CUtlVector< char*, CUtlMemory< char*, int > > { public: - ~CUtlVectorAutoPurge( void ) + ~CUtlStringList( void ) { - this->PurgeAndDeleteElements(); + PurgeAndDeleteElementsArray(); } -}; - -// easy string list class with dynamically allocated strings. For use with V_SplitString, etc. -// Frees the dynamic strings in destructor. -class CUtlStringList : public CUtlVectorAutoPurge< char *> -{ -public: void CopyAndAddToTail( char const *pString ) // clone the string and add to the end { char *pNewStr = new char[1 + strlen( pString )];