Fix logic_substring behaviour with unexpected length/startpos values

This commit is contained in:
MoofEMP 2021-07-27 02:29:32 -04:00
parent 22557f3751
commit 41cde5ccf7

View File

@ -66,8 +66,19 @@ void CLogicSubstring::InputInValue( inputdata_t &inputData )
{ {
if( !m_bEnabled ) return; if( !m_bEnabled ) return;
char* strOutValue = (char*)malloc( m_nLength ); int startPosCheck = m_nStartPos < 0 ? Q_strlen(inputData.value.String()) + m_nStartPos : m_nStartPos;
Q_strncpy( strOutValue, inputData.value.String() + m_nStartPos, m_nLength + 1 ); // note length+1 to account for null terminator if( startPosCheck < 0 )
{
startPosCheck = 0;
}
int lengthCheck = (m_nLength < 0 || m_nLength > Q_strlen(inputData.value.String()) - startPosCheck ? Q_strlen(inputData.value.String()) - startPosCheck : m_nLength) + 1;
if( lengthCheck < 1 || startPosCheck > Q_strlen(inputData.value.String()) )
{
m_OutValue.Set( MAKE_STRING(""), inputData.pActivator, this );
return;
}
char* strOutValue = (char*)malloc( lengthCheck );
Q_strncpy( strOutValue, inputData.value.String() + startPosCheck, lengthCheck );
m_OutValue.Set( MAKE_STRING(strOutValue), inputData.pActivator, this ); m_OutValue.Set( MAKE_STRING(strOutValue), inputData.pActivator, this );
} }