Skip to content

Commit c91326e

Browse files
committed
Get rid of unsafe sprintf and strcpy. Update COM_FileBase from libpublic
1 parent 7383617 commit c91326e

17 files changed

+97
-119
lines changed

BaseMenu.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,10 @@ bool UI_StartBackGroundMap( void )
578578
int bgmapid = EngFuncs::RandomLong( 0, uiStatic.bgmapcount - 1 );
579579

580580
char cmd[128];
581-
sprintf( cmd, "maps/%s.bsp", uiStatic.bgmaps[bgmapid] );
581+
snprintf( cmd, sizeof( cmd ), "maps/%s.bsp", uiStatic.bgmaps[bgmapid] );
582582
if( !EngFuncs::FileExists( cmd, TRUE )) return FALSE;
583583

584-
sprintf( cmd, "map_background %s\n", uiStatic.bgmaps[bgmapid] );
584+
snprintf( cmd, sizeof( cmd ), "map_background %s\n", uiStatic.bgmaps[bgmapid] );
585585
EngFuncs::ClientCmd( FALSE, cmd );
586586

587587
return TRUE;

CFGScript.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ bool CSCR_ParseSingleCvar( parserstate_t *ps, scrvardef_t *result )
151151

152152
entry = new scrvarlistentry_t;
153153
entry->next = NULL;
154-
entry->szName = new char[strlen( szName ) + 1];
155-
strcpy( entry->szName, szName );
154+
entry->szName = StringCopy( szName );
156155
entry->flValue = atof( szValue );
157156

158157
if( !result->list.pEntries )

MenuStrings.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ static void UI_InitAliasStrings( void )
134134

135135
char token[1024];
136136
char token2[1024];
137-
sprintf( token, "StringsList_%d", aliasStrings[i].idx );
137+
snprintf( token, sizeof( token ), "StringsList_%d", aliasStrings[i].idx );
138138

139139
const char *fmt = L( token );
140140
if( fmt == token )
141141
{
142142
fmt = aliasStrings[i].defAliasString;
143143
}
144144

145-
sprintf( token2, fmt, gMenu.m_gameinfo.title );
145+
snprintf( token2, sizeof( token2 ), fmt, gMenu.m_gameinfo.title );
146146
MenuStrings[aliasStrings[i].idx] = StringCopy( token2 );
147147

148148
Dictionary_Insert( token, MenuStrings[aliasStrings[i].idx] );

Utils.cpp

+25-27
Original file line numberDiff line numberDiff line change
@@ -218,42 +218,40 @@ int COM_CompareSaves( const void **a, const void **b )
218218
/*
219219
============
220220
COM_FileBase
221+
222+
Extracts the base name of a file (no path, no extension, assumes '/' as path separator)
223+
a1ba: adapted and simplified version from QuakeSpasm
221224
============
222225
*/
223-
// Extracts the base name of a file (no path, no extension, assumes '/' as path separator)
224-
void COM_FileBase ( const char *in, char *out )
226+
#define COM_CheckString( string ) ( ( !string || !*string ) ? 0 : 1 )
227+
void COM_FileBase( const char *in, char *out, size_t size )
225228
{
226-
int len, start, end;
229+
const char *dot, *slash, *s;
230+
size_t len;
227231

228-
len = strlen( in );
229-
230-
// scan backward for '.'
231-
end = len - 1;
232-
while ( end && in[end] != '.' && in[end] != '/' && in[end] != '\\' )
233-
end--;
234-
235-
if ( in[end] != '.' ) // no '.', copy to end
236-
end = len-1;
237-
else
238-
end--; // Found ',', copy to left of '.'
232+
if( unlikely( !COM_CheckString( in ) || size <= 1 ))
233+
{
234+
out[0] = 0;
235+
return;
236+
}
239237

238+
slash = in;
239+
dot = NULL;
240+
for( s = in; *s; s++ )
241+
{
242+
if( *s == '/' || *s == '\\' )
243+
slash = s + 1;
240244

241-
// Scan backward for '/'
242-
start = len-1;
243-
while ( start >= 0 && in[start] != '/' && in[start] != '\\' )
244-
start--;
245+
if( *s == '.' )
246+
dot = s;
247+
}
245248

246-
if ( in[start] != '/' && in[start] != '\\' )
247-
start = 0;
248-
else
249-
start++;
249+
if( dot == NULL || dot < slash )
250+
dot = s;
250251

251-
// Length of new sting
252-
len = end - start + 1;
252+
len = Q_min( size - 1, dot - slash );
253253

254-
// Copy partial string
255-
strncpy( out, &in[start], len );
256-
// Terminate it
254+
memcpy( out, slash, len );
257255
out[len] = 0;
258256
}
259257

Utils.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,19 @@ inline float RemapVal( const float val, const float A, const float B, const floa
148148
return C + (D - C) * (val - A) / (B - A);
149149
}
150150

151+
extern const unsigned int g_iColorTable[8];
152+
151153
int colorstricmp( const char *a, const char *b );
152154
int colorstrcmp( const char *a, const char *b );
153-
extern int ColorStrlen( const char *str ); // returns string length without color symbols
154-
extern int ColorPrexfixCount( const char *str );
155-
extern const unsigned int g_iColorTable[8];
156-
extern void COM_FileBase( const char *in, char *out ); // ripped out from hlsdk 2.3
157-
extern int UI_FadeAlpha( int starttime, int endtime );
158-
extern const char *Info_ValueForKey( const char *s, const char *key );
159-
extern int KEY_GetKey( const char *binding ); // ripped out from engine
160-
extern char *StringCopy( const char *input ); // copy string into new memory
161-
extern int COM_CompareSaves( const void **a, const void **b );
162-
extern void Com_EscapeCommand( char *newCommand, const char *oldCommand, int len );
163-
extern void UI_EnableTextInput( bool enable );
155+
int ColorStrlen( const char *str ); // returns string length without color symbols
156+
void COM_FileBase( const char *in, char *out, size_t size );
157+
int UI_FadeAlpha( int starttime, int endtime );
158+
const char *Info_ValueForKey( const char *s, const char *key );
159+
int KEY_GetKey( const char *binding ); // ripped out from engine
160+
char *StringCopy( const char *input ); // copy string into new memory
161+
int COM_CompareSaves( const void **a, const void **b );
162+
void Com_EscapeCommand( char *newCommand, const char *oldCommand, int len );
163+
void UI_EnableTextInput( bool enable );
164164

165165
void UI_LoadCustomStrings( void );
166166
const char *L( const char *szStr ); // L means Localize!

controls/BaseItem.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,7 @@ bool CMenuBaseItem::KeyValueData(const char *key, const char *data)
287287

288288
if( m_bAllocName )
289289
{
290-
char *name = new char[strlen( data ) + 1];
291-
strcpy( name, data );
292-
293-
szName = name;
290+
szName = StringCopy( data );
294291
}
295292
}
296293
else if( !strcmp( key, "textAlignment" ) )

controls/Editable.cpp

+4-11
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,24 @@ void CMenuEditable::SetCvarValue( float value )
5454
if( bUpdateImmediately ) WriteCvar();
5555
}
5656

57-
void CMenuEditable::SetCvarString(const char *string)
57+
void CMenuEditable::SetCvarString( const char *string )
5858
{
5959
if( string != m_szString )
60-
{
61-
Q_strncpy( m_szString, string, CS_SIZE );
62-
m_szString[CS_SIZE-1] = 0;
63-
}
60+
Q_strncpy( m_szString, string, sizeof( m_szString ));
6461

6562
if( onCvarChange ) onCvarChange( this );
6663
if( bUpdateImmediately ) WriteCvar();
6764
}
6865

6966
void CMenuEditable::SetOriginalString( const char *psz )
7067
{
71-
Q_strncpy( m_szString, psz, CS_SIZE );
72-
Q_strncpy( m_szOriginalString, m_szString, CS_SIZE );
73-
m_szOriginalString[CS_SIZE-1] = 0;
74-
68+
Q_strncpy( m_szOriginalString, psz, sizeof( m_szOriginalString ));
7569
SetCvarString( m_szOriginalString );
7670
}
7771

7872
void CMenuEditable::SetOriginalValue( float val )
7973
{
80-
m_flValue = m_flOriginalValue = val;
81-
74+
m_flOriginalValue = val;
8275
SetCvarValue( m_flOriginalValue );
8376
}
8477

controls/Field.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CMenuField : public CMenuEditable
5050

5151
void SetBuffer( const char *buffer )
5252
{
53-
Q_strncpy( szBuffer, buffer, UI_MAX_FIELD_LINE );
53+
Q_strncpy( szBuffer, buffer, sizeof( szBuffer ));
5454
iCursor = strlen( szBuffer );
5555
iScroll = g_FontMgr->CutText( font, szBuffer, m_scChSize, iRealWidth, true );
5656
SetCvarString( szBuffer );

controls/SpinControl.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ void CMenuSpinControl::SetCurrentValue( const char *stringValue )
309309
m_flCurValue = -1;
310310
SetCvarString( stringValue );
311311

312-
Q_strncpy( m_szDisplay, stringValue, CS_SIZE );
312+
Q_strncpy( m_szDisplay, stringValue, sizeof( m_szDisplay ));
313313
}
314314

315315
void CMenuSpinControl::SetDisplayPrecision( short precision )
@@ -323,7 +323,7 @@ void CMenuSpinControl::Display()
323323
{
324324
SetCvarValue( m_flCurValue );
325325

326-
snprintf( m_szDisplay, CS_SIZE, "%.*f", m_iFloatPrecision, m_flCurValue );
326+
snprintf( m_szDisplay, sizeof( m_szDisplay ), "%.*f", m_iFloatPrecision, m_flCurValue );
327327
}
328328
else
329329
{
@@ -336,11 +336,11 @@ void CMenuSpinControl::Display()
336336
case CVAR_VALUE: SetCvarValue( m_flCurValue ); break;
337337
}
338338

339-
Q_strncpy( m_szDisplay, stringValue, CS_SIZE );
339+
Q_strncpy( m_szDisplay, stringValue, sizeof( m_szDisplay ));
340340
}
341341
}
342342

343-
void CMenuSpinControl::ForceDisplayString(const char *display)
343+
void CMenuSpinControl::ForceDisplayString( const char *display )
344344
{
345-
Q_strncpy( m_szDisplay, display, CS_SIZE );
345+
Q_strncpy( m_szDisplay, display, sizeof( m_szDisplay ));
346346
}

controls/YesNoMessageBox.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ void UI_ShowMessageBox( const char *text )
228228
static char msg[1024];
229229
static CMenuYesNoMessageBox msgBox( true );
230230

231-
Q_strncpy( msg, text, sizeof( msg ) );
231+
Q_strncpy( msg, text, sizeof( msg ));
232232

233233
if( !UI_IsVisible() )
234234
{

menus/Credits.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ void CMenuCredits::_Init( void )
168168
memcpy( tmp, buffer, count );
169169
EngFuncs::COM_FreeFile( buffer );
170170
buffer = tmp;
171-
strncpy( buffer + count, "\r", 1 ); // add terminator
171+
Q_strncpy( buffer + count, "\r", 1 ); // add terminator
172172
count += 2; // added "\r\0"
173173
}
174174
p = buffer;

menus/CustomGame.cpp

+11-13
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct mod_t
6464
}\
6565
static int method ## Descend( const void *a, const void *b ) \
6666
{\
67-
return (( const mod_t *)b)->method( *(( const mod_t *)a) );;\
67+
return (( const mod_t *)b)->method( *(( const mod_t *)a) );\
6868
}\
6969

7070
GENERATE_COMPAR_FN( TypeCmp )
@@ -129,7 +129,7 @@ class CMenuCustomGame: public CMenuFramework
129129
void CMenuCustomGame::ChangeGame( void *pExtra )
130130
{
131131
char cmd[128];
132-
sprintf( cmd, "game %s\n", (const char*)pExtra );
132+
snprintf( cmd, sizeof( cmd ), "game %s\n", (const char*)pExtra );
133133
EngFuncs::ClientCmd( FALSE, cmd );
134134
}
135135

@@ -169,28 +169,26 @@ void CMenuModListModel::Update( void )
169169
{
170170
Q_strncpy( mods[i].dir, games[i]->gamefolder, sizeof( mods[i].dir ));
171171
Q_strncpy( mods[i].webSite, games[i]->game_url, sizeof( mods[i].webSite ));
172+
Q_strncpy( mods[i].type, games[i]->type, sizeof( mods[i].type ));
173+
Q_strncpy( mods[i].ver, games[i]->version, sizeof( mods[i].ver ));
172174

173-
Q_strncpy( mods[i].type, games[i]->type, 32 );
174-
175-
if( ColorStrlen( games[i]->title ) > 31 ) // NAME_LENGTH
175+
if( ColorStrlen( games[i]->title ) > sizeof( mods[i].name ) - 1 ) // NAME_LENGTH
176176
{
177-
Q_strncpy( mods[i].name, games[i]->title, 32 - 4 );
177+
Q_strncpy( mods[i].name, games[i]->title, sizeof( mods[i].name ) - 4 );
178178
// I am lazy to put strncat here :(
179179
mods[i].name[28] = mods[i].name[29] = mods[i].name[30] = '.';
180180
mods[i].name[31] = 0;
181181
}
182-
else Q_strncpy( mods[i].name, games[i]->title, 32 );
183-
184-
Q_strncpy( mods[i].ver, games[i]->version, 32 );
182+
else Q_strncpy( mods[i].name, games[i]->title, sizeof( mods[i].name ));
185183

186184
if( games[i]->size[0] && atoi( games[i]->size ) != 0 )
187-
Q_strncpy( mods[i].size, games[i]->size, 32 );
188-
else Q_strncpy( mods[i].size, "0.0 Mb", 32 );
185+
Q_strncpy( mods[i].size, games[i]->size, sizeof( mods[i].size ));
186+
else Q_strncpy( mods[i].size, "0.0 Mb", sizeof( mods[i].size ));
189187
}
190188

191189
m_iNumItems = numGames;
192190

193-
if(numGames)
191+
if( numGames )
194192
{
195193
if( m_iSortingColumn != -1 )
196194
Sort( m_iSortingColumn, m_bAscend );
@@ -269,4 +267,4 @@ void CMenuCustomGame::_Init( void )
269267
}
270268
}
271269

272-
ADD_MENU( menu_customgame, CMenuCustomGame, UI_CustomGame_Menu );
270+
ADD_MENU( menu_customgame, CMenuCustomGame, UI_CustomGame_Menu )

menus/LoadGame.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,15 @@ void CMenuSavesListModel::Update( void )
193193
Q_strncpy( date[i], comment, sizeof( date[i] ));
194194
save_comment[i][0] = 0;
195195
elapsed_time[i][0] = 0;
196-
COM_FileBase( filenames[j], saveName[i] );
197-
COM_FileBase( filenames[j], delName[i] );
196+
COM_FileBase( filenames[j], saveName[i], sizeof( saveName[i] ));
197+
COM_FileBase( filenames[j], delName[i], sizeof( delName[i] ));
198198
}
199199
continue;
200200
}
201201

202202
// strip path, leave only filename (empty slots doesn't have savename)
203-
COM_FileBase( filenames[j], saveName[i] );
204-
COM_FileBase( filenames[j], delName[i] );
203+
COM_FileBase( filenames[j], saveName[i], sizeof( saveName[i] ));
204+
COM_FileBase( filenames[j], delName[i], sizeof( delName[i] ));
205205

206206
// they are defined by comment string format
207207
// time and date
@@ -341,7 +341,7 @@ void CMenuLoadGame::LoadGame()
341341
if( saveName[0] )
342342
{
343343
char cmd[128];
344-
sprintf( cmd, "load \"%s\"\n", saveName );
344+
snprintf( cmd, sizeof( cmd ), "load \"%s\"\n", saveName );
345345

346346
EngFuncs::StopBackgroundTrack( );
347347

@@ -358,10 +358,10 @@ void CMenuLoadGame::SaveGame()
358358
{
359359
char cmd[128];
360360

361-
sprintf( cmd, "save/%s.bmp", saveName );
361+
snprintf( cmd, sizeof( cmd ), "save/%s.bmp", saveName );
362362
EngFuncs::PIC_Free( cmd );
363363

364-
sprintf( cmd, "save \"%s\"\n", saveName );
364+
snprintf( cmd, sizeof( cmd ), "save \"%s\"\n", saveName );
365365
EngFuncs::ClientCmd( FALSE, cmd );
366366

367367
UI_CloseMenu();
@@ -390,11 +390,11 @@ void CMenuLoadGame::DeleteGame()
390390
if( delName[0] )
391391
{
392392
char cmd[128];
393-
sprintf( cmd, "killsave \"%s\"\n", delName );
393+
snprintf( cmd, sizeof( cmd ), "killsave \"%s\"\n", delName );
394394

395395
EngFuncs::ClientCmd( TRUE, cmd );
396396

397-
sprintf( cmd, "save/%s.bmp", delName );
397+
snprintf( cmd, sizeof( cmd ), "save/%s.bmp", delName );
398398
EngFuncs::PIC_Free( cmd );
399399

400400
savesListModel.Update();

0 commit comments

Comments
 (0)