diff options
Diffstat (limited to 'mp/src/tier1/utlbuffer.cpp')
| -rw-r--r-- | mp/src/tier1/utlbuffer.cpp | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/mp/src/tier1/utlbuffer.cpp b/mp/src/tier1/utlbuffer.cpp index 55dd8f6b..fc89d773 100644 --- a/mp/src/tier1/utlbuffer.cpp +++ b/mp/src/tier1/utlbuffer.cpp @@ -607,17 +607,19 @@ int CUtlBuffer::PeekDelimitedStringLength( CUtlCharConversion *pConv, bool bActu //----------------------------------------------------------------------------- // Reads a null-terminated string //----------------------------------------------------------------------------- -void CUtlBuffer::GetString( char* pString, int nMaxChars ) +void CUtlBuffer::GetStringInternal( char *pString, size_t maxLenInChars ) { - if (!IsValid()) + if ( !IsValid() ) { *pString = 0; return; } - if ( nMaxChars == 0 ) + Assert( maxLenInChars != 0 ); + + if ( maxLenInChars == 0 ) { - nMaxChars = INT_MAX; + return; } // Remember, this *includes* the null character @@ -629,24 +631,21 @@ void CUtlBuffer::GetString( char* pString, int nMaxChars ) EatWhiteSpace(); } - if ( nLen == 0 ) + if ( nLen <= 0 ) { *pString = 0; m_Error |= GET_OVERFLOW; return; } - - // Strip off the terminating NULL - if ( nLen <= nMaxChars ) - { - Get( pString, nLen - 1 ); - pString[ nLen - 1 ] = 0; - } - else + + const size_t nCharsToRead = min( (size_t)nLen, maxLenInChars ) - 1; + + Get( pString, nCharsToRead ); + pString[nCharsToRead] = 0; + + if ( (size_t)nLen > (nCharsToRead + 1) ) { - Get( pString, nMaxChars - 1 ); - pString[ nMaxChars - 1 ] = 0; - SeekGet( SEEK_CURRENT, nLen - 1 - nMaxChars ); + SeekGet( SEEK_CURRENT, nLen - (nCharsToRead + 1) ); } // Read the terminating NULL in binary formats @@ -731,7 +730,7 @@ void CUtlBuffer::GetDelimitedString( CUtlCharConversion *pConv, char *pString, i { if ( !IsText() || !pConv ) { - GetString( pString, nMaxChars ); + GetStringInternal( pString, nMaxChars ); return; } @@ -1041,7 +1040,7 @@ int CUtlBuffer::VaScanf( const char* pFmt, va_list list ) case 's': { char* s = va_arg( list, char * ); - GetString( s ); + GetStringInternal( s, 256 ); } break; |