Skip to content

Commit

Permalink
Use safe COM_ParseFile
Browse files Browse the repository at this point in the history
  • Loading branch information
a1batross committed Oct 3, 2021
1 parent f696041 commit fc78663
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 42 deletions.
6 changes: 3 additions & 3 deletions BaseMenu.cpp
Expand Up @@ -902,7 +902,7 @@ void UI_ParseColor( char *&pfile, unsigned int *outColor )

for( int i = 0; i < 3; i++ )
{
pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ));
if( !pfile ) break;
color[i] = atoi( token );
}
Expand All @@ -923,7 +923,7 @@ void UI_ApplyCustomColors( void )
return;
}

while(( pfile = EngFuncs::COM_ParseFile( pfile, token )) != NULL )
while(( pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ))) != NULL )
{
if( !stricmp( token, "HELP_COLOR" ))
{
Expand Down Expand Up @@ -984,7 +984,7 @@ static void UI_LoadBackgroundMapList( void )
return;
}

while(( pfile = EngFuncs::COM_ParseFile( pfile, token )) != NULL )
while(( pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ))) != NULL )
{
// skip the numbers (old format list)
if( isdigit( token[0] )) continue;
Expand Down
22 changes: 11 additions & 11 deletions CFGScript.cpp
Expand Up @@ -41,7 +41,7 @@ Return true if next token is pExpext and skip it
*/
bool CSCR_ExpectString( parserstate_t *ps, const char *pExpect, bool skip, bool error )
{
char *tmp = EngFuncs::COM_ParseFile( ps->buf, ps->token );
char *tmp = EngFuncs::COM_ParseFile( ps->buf, ps->token, sizeof( ps->token ));

if( !stricmp( ps->token, pExpect ) )
{
Expand Down Expand Up @@ -94,13 +94,13 @@ bool CSCR_ParseSingleCvar( parserstate_t *ps, scrvardef_t *result )
result->list.pArray = NULL;

// read the name
ps->buf = EngFuncs::COM_ParseFile( ps->buf, result->name );
ps->buf = EngFuncs::COM_ParseFile( ps->buf, result->name, sizeof( result->name ));

if( !CSCR_ExpectString( ps, "{", false, true ) )
goto error;

// read description
ps->buf = EngFuncs::COM_ParseFile( ps->buf, result->desc );
ps->buf = EngFuncs::COM_ParseFile( ps->buf, result->desc, sizeof( result->desc ));

if( !CSCR_ExpectString( ps, "{", false, true ) )
goto error;
Expand All @@ -116,11 +116,11 @@ bool CSCR_ParseSingleCvar( parserstate_t *ps, scrvardef_t *result )
break;
case T_NUMBER:
// min
ps->buf = EngFuncs::COM_ParseFile( ps->buf, ps->token );
ps->buf = EngFuncs::COM_ParseFile( ps->buf, ps->token, sizeof( ps->token ));
result->number.fMin = atof( ps->token );

// max
ps->buf = EngFuncs::COM_ParseFile( ps->buf, ps->token );
ps->buf = EngFuncs::COM_ParseFile( ps->buf, ps->token, sizeof( ps->token ));
result->number.fMax = atof( ps->token );

if( !CSCR_ExpectString( ps, "}", false, true ) )
Expand All @@ -141,11 +141,11 @@ bool CSCR_ParseSingleCvar( parserstate_t *ps, scrvardef_t *result )
// Read token for each item here

// ExpectString already moves buffer pointer, so just read from ps->token
// ps->buf = EngFuncs::COM_ParseFile( ps->buf, szName );
// ps->buf = EngFuncs::COM_ParseFile( ps->buf, szName, sizeof( szName ));
if( !szName[0] )
goto error;

ps->buf = EngFuncs::COM_ParseFile( ps->buf, szValue );
ps->buf = EngFuncs::COM_ParseFile( ps->buf, szValue, sizeof( szValue ));
if( !szValue[0] )
goto error;

Expand All @@ -172,7 +172,7 @@ bool CSCR_ParseSingleCvar( parserstate_t *ps, scrvardef_t *result )
goto error;

// default value
ps->buf = EngFuncs::COM_ParseFile( ps->buf, result->value );
ps->buf = EngFuncs::COM_ParseFile( ps->buf, result->value, sizeof( result->value ));

if( !CSCR_ExpectString( ps, "}", false, true ) )
goto error;
Expand Down Expand Up @@ -231,7 +231,7 @@ bool CSCR_ParseHeader( parserstate_t *ps )

// Parse in the version #
// Get the first token.
ps->buf = EngFuncs::COM_ParseFile( ps->buf, ps->token );
ps->buf = EngFuncs::COM_ParseFile( ps->buf, ps->token, sizeof( ps->token ));

if( atof( ps->token ) != 1 )
{
Expand All @@ -242,7 +242,7 @@ bool CSCR_ParseHeader( parserstate_t *ps )
if( !CSCR_ExpectString( ps, "DESCRIPTION", false, true ) )
return false;

ps->buf = EngFuncs::COM_ParseFile( ps->buf, ps->token );
ps->buf = EngFuncs::COM_ParseFile( ps->buf, ps->token, sizeof( ps->token ));

if( stricmp( ps->token, "INFO_OPTIONS") && stricmp( ps->token, "SERVER_OPTIONS" ) )
{
Expand Down Expand Up @@ -322,7 +322,7 @@ scrvardef_t *CSCR_LoadDefaultCVars( const char *scriptfilename, int *count )
break;
}

if( EngFuncs::COM_ParseFile( state.buf, state.token ) )
if( EngFuncs::COM_ParseFile( state.buf, state.token, sizeof( state.token )))
Con_DPrintf( "Got extra tokens!\n" );
else
success = true;
Expand Down
20 changes: 10 additions & 10 deletions MenuStrings.cpp
Expand Up @@ -324,23 +324,23 @@ static void Localize_AddToDictionary( const char *name, const char *lang )

pfile = afile;

pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ));

if( stricmp( token, "lang" ))
{
Con_Printf( "Localize_AddToDict( %s ): invalid header, got %s", filename, token );
goto error;
}

pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ) );

if( strcmp( token, "{" ))
{
Con_Printf( "Localize_AddToDict( %s ): want {, got %s", filename, token );
goto error;
}

pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ) );

if( stricmp( token, "Language" ))
{
Expand All @@ -349,31 +349,31 @@ static void Localize_AddToDictionary( const char *name, const char *lang )
}

// skip language actual name
pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ) );

pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ) );

if( stricmp( token, "Tokens" ))
{
Con_Printf( "Localize_AddToDict( %s ): want Tokens, got %s", filename, token );
goto error;
}

pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ) );

if( strcmp( token, "{" ))
{
Con_Printf( "Localize_AddToDict( %s ): want { after Tokens, got %s", filename, token );
goto error;
}

while( (pfile = EngFuncs::COM_ParseFile( pfile, token )))
while( (pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ))))
{
if( !strcmp( token, "}" ))
break;

char szLocString[4096];
pfile = EngFuncs::COM_ParseFile( pfile, szLocString );
pfile = EngFuncs::COM_ParseFile( pfile, szLocString, sizeof( szLocString ));

if( !strcmp( szLocString, "}" ))
break;
Expand Down Expand Up @@ -470,7 +470,7 @@ void UI_LoadCustomStrings( void )
if( !afile )
goto localize_init;

while(( pfile = EngFuncs::COM_ParseFile( pfile, token )) != NULL )
while(( pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ))) != NULL )
{
if( isdigit( token[0] ))
{
Expand All @@ -484,7 +484,7 @@ void UI_LoadCustomStrings( void )
else continue; // invalid declaration ?

// parse new string
pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ));
MenuStrings[string_num] = StringCopy( token ); // replace default string with custom
}

Expand Down
14 changes: 7 additions & 7 deletions controls/BackgroundBitmap.cpp
Expand Up @@ -224,22 +224,22 @@ bool CMenuBackgroundBitmap::LoadBackgroundImage( bool gamedirOnly )

pfile = afile;

pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ) );
if( !pfile || strcmp( token, "resolution" )) // resolution at first!
goto freefile;

pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ) );
if( !pfile ) goto freefile;

s_BackgroundImageSize.w = atoi( token );

pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ) );
if( !pfile ) goto freefile;

s_BackgroundImageSize.h = atoi( token );

// Now read all tiled background list
while(( pfile = EngFuncs::COM_ParseFile( pfile, token )))
while(( pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ) )))
{
bimage_t img;

Expand All @@ -251,14 +251,14 @@ bool CMenuBackgroundBitmap::LoadBackgroundImage( bool gamedirOnly )
if( !img.hImage ) goto freefile;

// ignore "scaled" attribute. What does it mean?
pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ) );
if( !pfile ) goto freefile;

pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ) );
if( !pfile ) goto freefile;
img.coord.x = atoi( token );

pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ) );
if( !pfile ) goto freefile;
img.coord.y = atoi( token );

Expand Down
10 changes: 5 additions & 5 deletions controls/ItemsHolder.cpp
Expand Up @@ -534,7 +534,7 @@ bool RES_ExpectString( char **data, const char *expect, bool skip = true )
if( !data || !*data )
return true;

tmp = EngFuncs::COM_ParseFile( *data, token );
tmp = EngFuncs::COM_ParseFile( *data, token, sizeof( token ) );

if( skip )
*data = tmp;
Expand Down Expand Up @@ -566,7 +566,7 @@ bool CMenuItemsHolder::LoadRES(const char *filename)
if( !pfile )
return false;

afile = EngFuncs::COM_ParseFile( afile, token );
afile = EngFuncs::COM_ParseFile( afile, token, sizeof( token ) );

Con_DPrintf( "Loading res file from %s, name %s\n", filename, token );

Expand All @@ -583,7 +583,7 @@ bool CMenuItemsHolder::LoadRES(const char *filename)
{
CMenuBaseItem *item;

afile = EngFuncs::COM_ParseFile( afile, token );
afile = EngFuncs::COM_ParseFile( afile, token, sizeof( token ) );

if( !afile )
return FreeFile( pfile, false );
Expand All @@ -603,11 +603,11 @@ bool CMenuItemsHolder::LoadRES(const char *filename)
char key[1024];
char value[1024];

afile = EngFuncs::COM_ParseFile( afile, key );
afile = EngFuncs::COM_ParseFile( afile, key, sizeof( key ));
if( !afile )
return FreeFile( pfile, false );

afile = EngFuncs::COM_ParseFile( afile, value );
afile = EngFuncs::COM_ParseFile( afile, value, sizeof( value ));
if( !afile )
return FreeFile( pfile, false );

Expand Down
6 changes: 6 additions & 0 deletions enginecallback_menu.h
Expand Up @@ -277,6 +277,12 @@ class EngFuncs
{
return textfuncs.pfnDoubleTime();
}

static inline char* COM_ParseFile( char *data, char *token, const int size )
{ return textfuncs.pfnParseFile( data, token, size, 0, nullptr ); }

static inline char* COM_ParseFile( char *data, char *token, const int size, int flags, int *len )
{ return textfuncs.pfnParseFile( data, token, size, flags, len ); }
};


Expand Down
8 changes: 4 additions & 4 deletions menus/Controls.cpp
Expand Up @@ -185,7 +185,7 @@ void CMenuKeysModel::Update( void )
memset( firstKey, 0, sizeof( firstKey ));
memset( secondKey, 0, sizeof( secondKey ));

while(( pfile = EngFuncs::COM_ParseFile( pfile, token )) != NULL )
while(( pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ))) != NULL )
{
if( !stricmp( token, "blank" ))
{
Expand All @@ -209,7 +209,7 @@ void CMenuKeysModel::Update( void )
CMenuControls::GetKeyBindings( token, keys );
Q_strncpy( keysBind[i], token, sizeof( keysBind[i] ));

pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ));
if( !pfile ) break; // technically an error

if( token[0] == '#' )
Expand Down Expand Up @@ -275,13 +275,13 @@ void CMenuControls::ResetKeysList( void )
return;
}

while(( pfile = EngFuncs::COM_ParseFile( pfile, token )) != NULL )
while(( pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ))) != NULL )
{
char key[32];

Q_strncpy( key, token, sizeof( key ));

pfile = EngFuncs::COM_ParseFile( pfile, token );
pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ));
if( !pfile ) break; // technically an error

char cmd[4096];
Expand Down
4 changes: 2 additions & 2 deletions menus/CreateGame.cpp
Expand Up @@ -190,12 +190,12 @@ void CMenuMapListModel::Update( void )
strcpy( mapName[0], L( "GameUI_RandomMap" ) );
mapsDescription[0][0] = 0;

while(( pfile = EngFuncs::COM_ParseFile( pfile, token )) != NULL )
while(( pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ))) != NULL )
{
if( numMaps >= UI_MAXGAMES ) break;

Q_strncpy( mapName[numMaps], token, 64 );
if(( pfile = EngFuncs::COM_ParseFile( pfile, token )) == NULL )
if(( pfile = EngFuncs::COM_ParseFile( pfile, token, sizeof( token ))) == NULL )
{
Q_strncpy( mapsDescription[numMaps], mapName[numMaps], 64 );
break; // unexpected end of file
Expand Down

2 comments on commit fc78663

@Velaron
Copy link
Member

@Velaron Velaron commented on fc78663 Oct 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ui_extendedfuncs_t doesn't have pfnParseFile?

@a1batross
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Velaron oh well, didn't noticed I pushed it to the master.
There is a pending PR in the engine repo: FWGS/xash3d-fwgs#628

You're welcome to review it. :)

Please sign in to comment.