Skip to content

Commit

Permalink
[engine] Cleanup sv_tags handling and ensure no KeyValue leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dimhotepus committed Dec 13, 2024
1 parent a55f75f commit 560eafc
Showing 1 changed file with 49 additions and 51 deletions.
100 changes: 49 additions & 51 deletions engine/baseserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,56 +97,55 @@ static void ServerTagsCleanUp( void )
{
CUtlVector<char*> TagList;
ConVarRef sv_tags( "sv_tags" );
if ( sv_tags.IsValid() )
{
int i;
char tmptags[MAX_TAG_STRING_LENGTH];
tmptags[0] = '\0';
if ( !sv_tags.IsValid() ) return;

char tmptags[MAX_TAG_STRING_LENGTH];
tmptags[0] = '\0';

V_SplitString( sv_tags.GetString(), ",", TagList );
V_SplitString( sv_tags.GetString(), ",", TagList );

// make a pass on the tags to eliminate preceding whitespace and empty tags
for ( i = 0; i < TagList.Count(); i++ )
// make a pass on the tags to eliminate preceding whitespace and empty tags
for ( intp i = 0; i < TagList.Count(); i++ )
{
if ( i > 0 )
{
if ( i > 0 )
{
Q_strncat( tmptags, ",", MAX_TAG_STRING_LENGTH );
}
V_strcat_safe( tmptags, "," );
}

char *pChar = TagList[i];
while ( *pChar && *pChar == ' ' )
{
pChar++;
}
char *pChar = TagList[i];
while ( *pChar && *pChar == ' ' )
{
pChar++;
}

// make sure we don't have an empty string (all spaces or ,,)
if ( *pChar )
{
Q_strncat( tmptags, pChar, MAX_TAG_STRING_LENGTH );
}
// make sure we don't have an empty string (all spaces or ,,)
if ( *pChar )
{
V_strcat_safe( tmptags, pChar );
}
}

// reset our lists and sort the tags
TagList.PurgeAndDeleteElementsArray();
V_SplitString( tmptags, ",", TagList );
TagList.Sort( SortServerTags );
tmptags[0] = '\0';
// reset our lists and sort the tags
TagList.PurgeAndDeleteElementsArray();

// create our new, sorted list of tags
for ( i = 0; i < TagList.Count(); i++ )
{
if ( i > 0 )
{
Q_strncat( tmptags, ",", MAX_TAG_STRING_LENGTH );
}
V_SplitString( tmptags, ",", TagList );
TagList.Sort( SortServerTags );
tmptags[0] = '\0';

Q_strncat( tmptags, TagList[i], MAX_TAG_STRING_LENGTH );
// create our new, sorted list of tags
for ( intp i = 0; i < TagList.Count(); i++ )
{
if ( i > 0 )
{
V_strcat_safe( tmptags, "," );
}

// set our convar and purge our list
sv_tags.SetValue( tmptags );
TagList.PurgeAndDeleteElementsArray();
V_strcat_safe( tmptags, TagList[i] );
}

// set our convar and purge our list
sv_tags.SetValue( tmptags );
TagList.PurgeAndDeleteElementsArray();
}

static void SvTagsChangeCallback( IConVar *pConVar, const char *pOldValue, float flOldValue )
Expand Down Expand Up @@ -2397,7 +2396,7 @@ void CBaseServer::RecalculateTags( void )
// Games without this interface will have no tagged cvars besides "increased_maxplayers"
if ( serverGameTags )
{
KeyValues *pKV = new KeyValues( "GameTags" );
auto pKV = KeyValues::AutoDelete( "GameTags" );

serverGameTags->GetTaggedConVarList( pKV );

Expand All @@ -2421,8 +2420,6 @@ void CBaseServer::RecalculateTags( void )

p = p->GetNextKey();
}

pKV->deleteThis();
}

// Check maxplayers
Expand Down Expand Up @@ -2466,20 +2463,20 @@ void CBaseServer::AddTag( const char *pszTag )
{
CUtlVector<char*> TagList;
V_SplitString( sv_tags.GetString(), ",", TagList );
for ( int i = 0; i < TagList.Count(); i++ )
for ( const char *tag : TagList )
{
// Already in the tag list?
if ( !Q_stricmp(TagList[i],pszTag) )
if ( !Q_stricmp(tag,pszTag) )
return;
}
TagList.PurgeAndDeleteElementsArray();

// Append it
char tmptags[MAX_TAG_STRING_LENGTH];
tmptags[0] = '\0';
Q_strncpy( tmptags, pszTag, MAX_TAG_STRING_LENGTH );
Q_strncat( tmptags, ",", MAX_TAG_STRING_LENGTH );
Q_strncat( tmptags, sv_tags.GetString(), MAX_TAG_STRING_LENGTH );
V_strcpy_safe( tmptags, pszTag );
V_strcat_safe( tmptags, "," );
V_strcat_safe( tmptags, sv_tags.GetString() );
sv_tags.SetValue( tmptags );
}

Expand All @@ -2494,17 +2491,18 @@ void CBaseServer::RemoveTag( const char *pszTag )

char tmptags[MAX_TAG_STRING_LENGTH];
tmptags[0] = '\0';

bool bFoundIt = false;

CUtlVector<char*> TagList;
bool bFoundIt = false;
V_SplitString( sv_tags.GetString(), ",", TagList );
for ( int i = 0; i < TagList.Count(); i++ )
for ( const char *tag : TagList )
{
// Keep any tags other than the specified one
if ( Q_stricmp(TagList[i],pszTag) )
if ( Q_stricmp(tag,pszTag) )
{
Q_strncat( tmptags, TagList[i], MAX_TAG_STRING_LENGTH );
Q_strncat( tmptags, ",", MAX_TAG_STRING_LENGTH );
V_strcat_safe( tmptags, tag );
V_strcat_safe( tmptags, "," );
}
else
{
Expand Down

0 comments on commit 560eafc

Please sign in to comment.