Browse Source

public: remove Q_sprintf, and patch all code that used it to use Q_snprintf instead

pull/2/head
Alibek Omarov 2 years ago
parent
commit
8f207362a5
  1. 28
      engine/client/cl_cmds.c
  2. 12
      engine/client/cl_demo.c
  3. 8
      engine/client/keys.c
  4. 2
      engine/common/common.c
  5. 8
      engine/common/con_utils.c
  6. 2
      engine/common/host.c
  7. 2
      engine/common/identification.c
  8. 6
      engine/common/imagelib/img_main.c
  9. 6
      engine/common/net_ws.c
  10. 8
      engine/common/soundlib/snd_main.c
  11. 2
      engine/server/sv_init.c
  12. 24
      public/crtlib.c
  13. 2
      ref/gl/gl_rmisc.c

28
engine/client/cl_cmds.c

@ -145,7 +145,7 @@ void CL_PlayCDTrack_f( void )
CL_ScreenshotGetName CL_ScreenshotGetName
================== ==================
*/ */
qboolean CL_ScreenshotGetName( int lastnum, char *filename ) static qboolean CL_ScreenshotGetName( int lastnum, char *filename, size_t size )
{ {
if( lastnum < 0 || lastnum > 9999 ) if( lastnum < 0 || lastnum > 9999 )
{ {
@ -153,9 +153,7 @@ qboolean CL_ScreenshotGetName( int lastnum, char *filename )
return false; return false;
} }
Q_sprintf( filename, "scrshots/%s_shot%04d.png", clgame.mapname, lastnum ); return Q_snprintf( filename, size, "scrshots/%s_shot%04d.png", clgame.mapname, lastnum ) > 0;
return true;
} }
/* /*
@ -163,7 +161,7 @@ qboolean CL_ScreenshotGetName( int lastnum, char *filename )
CL_SnapshotGetName CL_SnapshotGetName
================== ==================
*/ */
qboolean CL_SnapshotGetName( int lastnum, char *filename ) static qboolean CL_SnapshotGetName( int lastnum, char *filename, size_t size )
{ {
if( lastnum < 0 || lastnum > 9999 ) if( lastnum < 0 || lastnum > 9999 )
{ {
@ -172,9 +170,7 @@ qboolean CL_SnapshotGetName( int lastnum, char *filename )
return false; return false;
} }
Q_sprintf( filename, "../%s_%04d.png", clgame.mapname, lastnum ); return Q_snprintf( filename, size, "../%s_%04d.png", clgame.mapname, lastnum ) > 0;
return true;
} }
/* /*
@ -207,7 +203,7 @@ void CL_ScreenShot_f( void )
// scan for a free filename // scan for a free filename
for( i = 0; i < 9999; i++ ) for( i = 0; i < 9999; i++ )
{ {
if( !CL_ScreenshotGetName( i, checkname )) if( !CL_ScreenshotGetName( i, checkname, sizeof( checkname )))
return; // no namespace return; // no namespace
if( !FS_FileExists( checkname, false )) if( !FS_FileExists( checkname, false ))
@ -247,7 +243,7 @@ void CL_SnapShot_f( void )
// scan for a free filename // scan for a free filename
for( i = 0; i < 9999; i++ ) for( i = 0; i < 9999; i++ )
{ {
if( !CL_SnapshotGetName( i, checkname )) if( !CL_SnapshotGetName( i, checkname, sizeof( checkname )))
return; // no namespace return; // no namespace
if( !FS_FileExists( checkname, false )) if( !FS_FileExists( checkname, false ))
@ -278,7 +274,7 @@ void CL_EnvShot_f( void )
return; return;
} }
Q_sprintf( cls.shotname, "gfx/env/%s", Cmd_Argv( 1 )); Q_snprintf( cls.shotname, sizeof( cls.shotname ), "gfx/env/%s", Cmd_Argv( 1 ));
cls.scrshot_action = scrshot_envshot; // build new frame for envshot cls.scrshot_action = scrshot_envshot; // build new frame for envshot
cls.envshot_vieworg = NULL; // no custom view cls.envshot_vieworg = NULL; // no custom view
cls.envshot_viewsize = 0; cls.envshot_viewsize = 0;
@ -299,7 +295,7 @@ void CL_SkyShot_f( void )
return; return;
} }
Q_sprintf( cls.shotname, "gfx/env/%s", Cmd_Argv( 1 )); Q_snprintf( cls.shotname, sizeof( cls.shotname ),"gfx/env/%s", Cmd_Argv( 1 ));
cls.scrshot_action = scrshot_skyshot; // build new frame for skyshot cls.scrshot_action = scrshot_skyshot; // build new frame for skyshot
cls.envshot_vieworg = NULL; // no custom view cls.envshot_vieworg = NULL; // no custom view
cls.envshot_viewsize = 0; cls.envshot_viewsize = 0;
@ -323,7 +319,8 @@ void CL_LevelShot_f( void )
// check for exist // check for exist
if( cls.demoplayback && ( cls.demonum != -1 )) if( cls.demoplayback && ( cls.demonum != -1 ))
{ {
Q_sprintf( cls.shotname, "levelshots/%s_%s.bmp", cls.demoname, refState.wideScreen ? "16x9" : "4x3" ); Q_snprintf( cls.shotname, sizeof( cls.shotname ),
"levelshots/%s_%s.bmp", cls.demoname, refState.wideScreen ? "16x9" : "4x3" );
Q_snprintf( filename, sizeof( filename ), "%s.dem", cls.demoname ); Q_snprintf( filename, sizeof( filename ), "%s.dem", cls.demoname );
// make sure what levelshot is newer than demo // make sure what levelshot is newer than demo
@ -332,7 +329,8 @@ void CL_LevelShot_f( void )
} }
else else
{ {
Q_sprintf( cls.shotname, "levelshots/%s_%s.bmp", clgame.mapname, refState.wideScreen ? "16x9" : "4x3" ); Q_snprintf( cls.shotname, sizeof( cls.shotname ),
"levelshots/%s_%s.bmp", clgame.mapname, refState.wideScreen ? "16x9" : "4x3" );
// make sure what levelshot is newer than bsp // make sure what levelshot is newer than bsp
ft1 = FS_FileTime( cl.worldmodel->name, false ); ft1 = FS_FileTime( cl.worldmodel->name, false );
@ -360,7 +358,7 @@ void CL_SaveShot_f( void )
return; return;
} }
Q_sprintf( cls.shotname, DEFAULT_SAVE_DIRECTORY "%s.bmp", Cmd_Argv( 1 )); Q_snprintf( cls.shotname, sizeof( cls.shotname ), DEFAULT_SAVE_DIRECTORY "%s.bmp", Cmd_Argv( 1 ));
cls.scrshot_action = scrshot_savegame; // build new frame for saveshot cls.scrshot_action = scrshot_savegame; // build new frame for saveshot
} }

12
engine/client/cl_demo.c

@ -1317,16 +1317,16 @@ void CL_CheckStartupDemos( void )
CL_DemoGetName CL_DemoGetName
================== ==================
*/ */
static void CL_DemoGetName( int lastnum, char *filename ) static void CL_DemoGetName( int lastnum, char *filename, size_t size )
{ {
if( lastnum < 0 || lastnum > 9999 ) if( lastnum < 0 || lastnum > 9999 )
{ {
// bound // bound
Q_strcpy( filename, "demo9999" ); Q_strncpy( filename, "demo9999.dem", size );
return; return;
} }
Q_sprintf( filename, "demo%04d", lastnum ); Q_snprintf( filename, size, "demo%04d.dem", lastnum );
} }
/* /*
@ -1380,8 +1380,8 @@ void CL_Record_f( void )
// scan for a free filename // scan for a free filename
for( n = 0; n < 10000; n++ ) for( n = 0; n < 10000; n++ )
{ {
CL_DemoGetName( n, demoname ); CL_DemoGetName( n, demoname, sizeof( demoname ));
if( !FS_FileExists( va( "%s.dem", demoname ), true )) if( !FS_FileExists( demoname, true ))
break; break;
} }
@ -1394,7 +1394,7 @@ void CL_Record_f( void )
else Q_strncpy( demoname, name, sizeof( demoname )); else Q_strncpy( demoname, name, sizeof( demoname ));
// open the demo file // open the demo file
Q_sprintf( demopath, "%s.dem", demoname ); Q_snprintf( demopath, sizeof( demopath ), "%s.dem", demoname );
// make sure that old demo is removed // make sure that old demo is removed
if( FS_FileExists( demopath, false )) if( FS_FileExists( demopath, false ))

8
engine/client/keys.c

@ -434,8 +434,8 @@ void Key_Bind_f( void )
for( i = 2; i < c; i++ ) for( i = 2; i < c; i++ )
{ {
Q_strcat( cmd, Cmd_Argv( i )); Q_strncat( cmd, Cmd_Argv( i ), sizeof( cmd ));
if( i != ( c - 1 )) Q_strcat( cmd, " " ); if( i != ( c - 1 )) Q_strncat( cmd, " ", sizeof( cmd ));
} }
Key_SetBinding( b, cmd ); Key_SetBinding( b, cmd );
@ -541,8 +541,8 @@ void Key_AddKeyCommands( int key, const char *kb, qboolean down )
if( button[0] == '+' ) if( button[0] == '+' )
{ {
// button commands add keynum as a parm // button commands add keynum as a parm
if( down ) Q_sprintf( cmd, "%s %i\n", button, key ); if( down ) Q_snprintf( cmd, sizeof( cmd ), "%s %i\n", button, key );
else Q_sprintf( cmd, "-%s %i\n", button + 1, key ); else Q_snprintf( cmd, sizeof( cmd ), "-%s %i\n", button + 1, key );
Cbuf_AddText( cmd ); Cbuf_AddText( cmd );
} }
else if( down ) else if( down )

2
engine/common/common.c

@ -564,7 +564,7 @@ int GAME_EXPORT COM_ExpandFilename( const char *fileName, char *nameOutBuffer, i
// models\barney.mdl - D:\Xash3D\bshift\models\barney.mdl // models\barney.mdl - D:\Xash3D\bshift\models\barney.mdl
if(( path = FS_GetDiskPath( fileName, false )) != NULL ) if(( path = FS_GetDiskPath( fileName, false )) != NULL )
{ {
Q_sprintf( result, "%s/%s", host.rootdir, path ); Q_snprintf( result, sizeof( result ), "%s/%s", host.rootdir, path );
// check for enough room // check for enough room
if( Q_strlen( result ) > nameOutBufferSize ) if( Q_strlen( result ) > nameOutBufferSize )

8
engine/common/con_utils.c

@ -861,6 +861,7 @@ qboolean Cmd_CheckMapsList_R( qboolean fRefresh, qboolean onlyingamedir )
byte buf[MAX_SYSPATH]; byte buf[MAX_SYSPATH];
string mpfilter; string mpfilter;
char *buffer; char *buffer;
size_t buffersize;
string result; string result;
int i, size; int i, size;
search_t *t; search_t *t;
@ -883,7 +884,8 @@ qboolean Cmd_CheckMapsList_R( qboolean fRefresh, qboolean onlyingamedir )
return false; return false;
} }
buffer = Mem_Calloc( host.mempool, t->numfilenames * 2 * sizeof( result )); buffersize = t->numfilenames * 2 * sizeof( result );
buffer = Mem_Calloc( host.mempool, buffersize );
use_filter = COM_CheckStringEmpty( GI->mp_filter ) ? true : false; use_filter = COM_CheckStringEmpty( GI->mp_filter ) ? true : false;
for( i = 0; i < t->numfilenames; i++ ) for( i = 0; i < t->numfilenames; i++ )
@ -969,8 +971,8 @@ qboolean Cmd_CheckMapsList_R( qboolean fRefresh, qboolean onlyingamedir )
if( num_spawnpoints ) if( num_spawnpoints )
{ {
// format: mapname "maptitle"\n // format: mapname "maptitle"\n
Q_sprintf( result, "%s \"%s\"\n", mapname, message ); Q_snprintf( result, sizeof( result ), "%s \"%s\"\n", mapname, message );
Q_strcat( buffer, result ); // add new string Q_strncat( buffer, result, buffersize ); // add new string
} }
} }
} }

2
engine/common/host.c

@ -769,7 +769,7 @@ void GAME_EXPORT Host_Error( const char *error, ... )
recursive = true; recursive = true;
Q_strncpy( hosterror2, hosterror1, MAX_SYSPATH ); Q_strncpy( hosterror2, hosterror1, MAX_SYSPATH );
host.errorframe = host.framecount; // to avoid multply calls per frame host.errorframe = host.framecount; // to avoid multply calls per frame
Q_sprintf( host.finalmsg, "Server crashed: %s", hosterror1 ); Q_snprintf( host.finalmsg, sizeof( host.finalmsg ), "Server crashed: %s", hosterror1 );
// clearing cmd buffer to prevent execute any commands // clearing cmd buffer to prevent execute any commands
COM_InitHostState(); COM_InitHostState();

2
engine/common/identification.c

@ -677,7 +677,7 @@ void ID_Init( void )
MD5Final( (byte*)md5, &hash ); MD5Final( (byte*)md5, &hash );
for( i = 0; i < 16; i++ ) for( i = 0; i < 16; i++ )
Q_sprintf( &id_md5[i*2], "%02hhx", md5[i] ); Q_snprintf( &id_md5[i*2], sizeof( id_md5 ) - i * 2, "%02hhx", md5[i] );
#if XASH_ANDROID && !XASH_DEDICATED #if XASH_ANDROID && !XASH_DEDICATED
Android_SaveID( va("%016llX", id^SYSTEM_XOR_MASK ) ); Android_SaveID( va("%016llX", id^SYSTEM_XOR_MASK ) );

6
engine/common/imagelib/img_main.c

@ -422,7 +422,8 @@ qboolean FS_SaveImage( const char *filename, rgbdata_t *pix )
{ {
for( i = 0; i < 6; i++ ) for( i = 0; i < 6; i++ )
{ {
Q_sprintf( path, format->formatstring, savename, box[i].suf, format->ext ); Q_snprintf( path, sizeof( path ),
format->formatstring, savename, box[i].suf, format->ext );
if( !format->savefunc( path, pix )) break; // there were errors if( !format->savefunc( path, pix )) break; // there were errors
pix->buffer += pix->size; // move pointer pix->buffer += pix->size; // move pointer
} }
@ -444,7 +445,8 @@ qboolean FS_SaveImage( const char *filename, rgbdata_t *pix )
{ {
if( !Q_stricmp( ext, format->ext )) if( !Q_stricmp( ext, format->ext ))
{ {
Q_sprintf( path, format->formatstring, savename, "", format->ext ); Q_snprintf( path, sizeof( path ),
format->formatstring, savename, "", format->ext );
if( format->savefunc( path, pix )) if( format->savefunc( path, pix ))
{ {
// clear any force flags // clear any force flags

6
engine/common/net_ws.c

@ -732,7 +732,8 @@ const char *NET_AdrToString( const netadr_t a )
return s; return s;
} }
Q_sprintf( s, "%i.%i.%i.%i:%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3], ntohs( a.port )); Q_snprintf( s, sizeof( s ),
"%i.%i.%i.%i:%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3], ntohs( a.port ));
return s; return s;
} }
@ -758,7 +759,8 @@ const char *NET_BaseAdrToString( const netadr_t a )
return s; return s;
} }
Q_sprintf( s, "%i.%i.%i.%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3] ); Q_snprintf( s, sizeof( s ),
"%i.%i.%i.%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3] );
return s; return s;
} }

8
engine/common/soundlib/snd_main.c

@ -90,7 +90,9 @@ wavdata_t *FS_LoadSound( const char *filename, const byte *buffer, size_t size )
{ {
if( anyformat || !Q_stricmp( ext, format->ext )) if( anyformat || !Q_stricmp( ext, format->ext ))
{ {
Q_sprintf( path, format->formatstring, loadname, "", format->ext ); Q_snprintf( path, sizeof( path ),
format->formatstring, loadname, "", format->ext );
f = FS_LoadFile( path, &filesize, false ); f = FS_LoadFile( path, &filesize, false );
if( f && filesize > 0 ) if( f && filesize > 0 )
{ {
@ -175,7 +177,9 @@ stream_t *FS_OpenStream( const char *filename )
{ {
if( anyformat || !Q_stricmp( ext, format->ext )) if( anyformat || !Q_stricmp( ext, format->ext ))
{ {
Q_sprintf( path, format->formatstring, loadname, "", format->ext ); Q_snprintf( path, sizeof( path ),
format->formatstring, loadname, "", format->ext );
if(( stream = format->openfunc( path )) != NULL ) if(( stream = format->openfunc( path )) != NULL )
{ {
stream->format = format; stream->format = format;

2
engine/server/sv_init.c

@ -997,7 +997,7 @@ qboolean SV_SpawnServer( const char *mapname, const char *startspot, qboolean ba
for( i = WORLD_INDEX; i < sv.worldmodel->numsubmodels; i++ ) for( i = WORLD_INDEX; i < sv.worldmodel->numsubmodels; i++ )
{ {
Q_sprintf( sv.model_precache[i+1], "*%i", i ); Q_snprintf( sv.model_precache[i+1], sizeof( sv.model_precache[i+1] ), "*%i", i );
sv.models[i+1] = Mod_ForName( sv.model_precache[i+1], false, false ); sv.models[i+1] = Mod_ForName( sv.model_precache[i+1], false, false );
SetBits( sv.model_precache_flags[i+1], RES_FATALIFMISSING ); SetBits( sv.model_precache_flags[i+1], RES_FATALIFMISSING );
} }

24
public/crtlib.c

@ -490,18 +490,6 @@ int Q_snprintf( char *buffer, size_t buffersize, const char *format, ... )
return result; return result;
} }
int Q_sprintf( char *buffer, const char *format, ... )
{
va_list args;
int result;
va_start( args, format );
result = Q_vsnprintf( buffer, 99999, format, args );
va_end( args );
return result;
}
void COM_StripColors( const char *in, char *out ) void COM_StripColors( const char *in, char *out )
{ {
while ( *in ) while ( *in )
@ -530,14 +518,14 @@ char *Q_pretifymem( float value, int digitsafterdecimal )
if( value > onemb ) if( value > onemb )
{ {
value /= onemb; value /= onemb;
Q_strcpy( suffix, " Mb" ); Q_strncpy( suffix, " Mb", sizeof( suffix ));
} }
else if( value > onekb ) else if( value > onekb )
{ {
value /= onekb; value /= onekb;
Q_strcpy( suffix, " Kb" ); Q_strncpy( suffix, " Kb", sizeof( suffix ));
} }
else Q_strcpy( suffix, " bytes" ); else Q_strncpy( suffix, " bytes", sizeof( suffix ));
// clamp to >= 0 // clamp to >= 0
digitsafterdecimal = Q_max( digitsafterdecimal, 0 ); digitsafterdecimal = Q_max( digitsafterdecimal, 0 );
@ -545,15 +533,15 @@ char *Q_pretifymem( float value, int digitsafterdecimal )
// if it's basically integral, don't do any decimals // if it's basically integral, don't do any decimals
if( fabs( value - (int)value ) < 0.00001f ) if( fabs( value - (int)value ) < 0.00001f )
{ {
Q_sprintf( val, "%i%s", (int)value, suffix ); Q_snprintf( val, sizeof( val ), "%i%s", (int)value, suffix );
} }
else else
{ {
char fmt[32]; char fmt[32];
// otherwise, create a format string for the decimals // otherwise, create a format string for the decimals
Q_sprintf( fmt, "%%.%if%s", digitsafterdecimal, suffix ); Q_snprintf( fmt, sizeof( fmt ), "%%.%if%s", digitsafterdecimal, suffix );
Q_sprintf( val, fmt, (double)value ); Q_snprintf( val, sizeof( val ), fmt, (double)value );
} }
// copy from in to out // copy from in to out

2
ref/gl/gl_rmisc.c

@ -119,7 +119,7 @@ void R_NewMap( void )
Q_strncpy( mapname, WORLDMODEL->name, sizeof( mapname )); Q_strncpy( mapname, WORLDMODEL->name, sizeof( mapname ));
COM_StripExtension( mapname ); COM_StripExtension( mapname );
Q_sprintf( filepath, "%s_detail.txt", mapname ); Q_snprintf( filepath, sizeof( filepath ), "%s_detail.txt", mapname );
R_ParseDetailTextures( filepath ); R_ParseDetailTextures( filepath );
} }

Loading…
Cancel
Save