Skip to content

Commit

Permalink
Remove all uses of sprintf.
Browse files Browse the repository at this point in the history
Some toolsets are now warning that sprintf is insecure/deprecated. This
replaces all sprintf calls with equivalent b2 equivalents or new prtinf
style utilities. This is for all platforms. Which avoids having to debug
alternate implementations.
  • Loading branch information
grafikrobot committed Aug 26, 2023
1 parent d6ad469 commit 33245e2
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 159 deletions.
26 changes: 11 additions & 15 deletions src/engine/builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@ LIST * builtin_calc( FRAME * frame, int flags )
long lhs_value;
long rhs_value;
long result_value;
char buffer[ 16 ];
char const * lhs;
char const * op;
char const * rhs;
Expand Down Expand Up @@ -519,8 +518,7 @@ LIST * builtin_calc( FRAME * frame, int flags )
else
return L0;

sprintf( buffer, "%ld", result_value );
result = list_push_back( result, object_new( buffer ) );
result = list_push_back( result, b2::value::as_string(result_value) );
return result;
}

Expand Down Expand Up @@ -1408,18 +1406,16 @@ LIST * builtin_backtrace( FRAME * frame, int flags )
{
char const * file;
int line;
char buf[ 32 ];
string module_name[ 1 ];
get_source_line( frame, &file, &line );
sprintf( buf, "%d", line );
string_new( module_name );
if ( frame->module->name )
{
string_append( module_name, object_str( frame->module->name ) );
string_append( module_name, "." );
}
result = list_push_back( result, object_new( file ) );
result = list_push_back( result, object_new( buf ) );
result = list_push_back( result, b2::value::as_string(line) );
result = list_push_back( result, object_new( module_name->value ) );
result = list_push_back( result, object_new( frame->rulename ) );
string_free( module_name );
Expand Down Expand Up @@ -1695,12 +1691,10 @@ LIST * builtin_nearest_user_location( FRAME * frame, int flags )
LIST * result = L0;
char const * file;
int line;
char buf[ 32 ];

get_source_line( nearest_user_frame, &file, &line );
sprintf( buf, "%d", line );
result = list_push_back( result, object_new( file ) );
result = list_push_back( result, object_new( buf ) );
result = list_push_back( result, b2::value::as_string(line) );
return result;
}
}
Expand Down Expand Up @@ -1730,8 +1724,13 @@ LIST * builtin_md5( FRAME * frame, int flags )
md5_append( &state, (md5_byte_t const *)s, strlen( s ) );
md5_finish( &state, digest );

static const char hex_digit[] = "0123456789abcdef";
for ( di = 0; di < 16; ++di )
sprintf( hex_output + di * 2, "%02x", digest[ di ] );
{
hex_output[di*2+0] = hex_digit[digest[di]>>4];
hex_output[di*2+1] = hex_digit[digest[di]&0xF];
}
hex_output[16*2] = '\0';

return list_new( object_new( hex_output ) );
}
Expand All @@ -1742,7 +1741,6 @@ LIST * builtin_file_open( FRAME * frame, int flags )
char const * name = object_str( list_front( lol_get( frame->args, 0 ) ) );
char const * mode = object_str( list_front( lol_get( frame->args, 1 ) ) );
int fd;
char buffer[ sizeof( "4294967295" ) ];

if ( strcmp(mode, "t") == 0 )
{
Expand Down Expand Up @@ -1772,8 +1770,7 @@ LIST * builtin_file_open( FRAME * frame, int flags )

if ( fd != -1 )
{
sprintf( buffer, "%d", fd );
return list_new( object_new( buffer ) );
return list_new( b2::value::as_string(fd) );
}
return L0;
}
Expand Down Expand Up @@ -2114,8 +2111,7 @@ LIST * builtin_shell( FRAME * frame, int flags )
/* Harmonize VMS success status with POSIX */
if ( exit_status == 1 ) exit_status = EXIT_SUCCESS;
#endif
sprintf( buffer, "%d", exit_status );
result = list_push_back( result, object_new( buffer ) );
result = list_push_back( result, b2::value::as_string(exit_status) );
}

return result;
Expand Down
22 changes: 8 additions & 14 deletions src/engine/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,6 @@ static void debug_parent_copy_breakpoints( void )
static void debug_start_child( int argc, const char * * argv )
{
#if NT
char buf[ 80 ];
HANDLE pipe1[ 2 ];
HANDLE pipe2[ 2 ];
string self[ 1 ];
Expand Down Expand Up @@ -1143,12 +1142,10 @@ static void debug_start_child( int argc, const char * * argv )
string_copy( command_line, "b2 " );
/* Pass the handles as the first and second arguments. */
string_append( command_line, debugger_opt );
sprintf( buf, "%p", pipe1[ 0 ] );
string_append( command_line, buf );
string_append( command_line, b2::value::format( "%p", pipe1[ 0 ] )->str() );
string_push_back( command_line, ' ' );
string_append( command_line, debugger_opt );
sprintf( buf, "%p", pipe2[ 1 ] );
string_append( command_line, buf );
string_append( command_line, b2::value::format( "%p", pipe2[ 1 ] )->str() );
/* Pass the rest of the command line. */
{
int i;
Expand Down Expand Up @@ -1495,7 +1492,6 @@ static void debug_parent_delete( int argc, const char * * argv )

static void debug_parent_clear( int argc, const char * * argv )
{
char buf[ 16 ];
const char * new_args[ 2 ];
int id;
if ( argc < 2 )
Expand All @@ -1520,9 +1516,9 @@ static void debug_parent_clear( int argc, const char * * argv )
printf( "Deleted breakpoint %d\n", id );
}

sprintf( buf, "%d", id );
auto id_s = std::to_string(id);
new_args[ 0 ] = "delete";
new_args[ 1 ] = buf;
new_args[ 1 ] = id_s.c_str();
debug_parent_delete( 2, new_args );
}

Expand Down Expand Up @@ -1577,9 +1573,8 @@ static void debug_parent_backtrace( int argc, const char * * argv )
for ( i = 0; i < depth; ++i )
{
FRAME_INFO frame;
char buf[ 16 ];
sprintf( buf, "%d", i );
new_args[ 2 ] = buf;
auto i_s = std::to_string(i);
new_args[ 2 ] = i_s.c_str();
debug_parent_forward_nowait( 3, new_args, 0, 0 );
debug_frame_read( command_child, &frame );
printf( "#%d in ", i );
Expand Down Expand Up @@ -1937,10 +1932,9 @@ static void debug_mi_break_insert( int argc, const char * * argv )

if ( disabled )
{
char buf[ 80 ];
sprintf( buf, "%d", num_breakpoints );
auto num_breakpoints_s = std::to_string(num_breakpoints);
inner_argv[ 0 ] = "disable";
inner_argv[ 1 ] = buf;
inner_argv[ 1 ] = num_breakpoints_s.c_str();
debug_child_disable( 2, inner_argv );
debug_parent_forward_nowait( 2, inner_argv, 1, 0 );
}
Expand Down
6 changes: 2 additions & 4 deletions src/engine/execcmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,21 @@ static int intr;
void argv_from_shell( char const * * argv, LIST * shell, char const * command,
int32_t const slot )
{
static char jobno[ 12 ];

int i;
int gotpercent = 0;
LISTITER iter = list_begin( shell );
LISTITER end = list_end( shell );

assert( 0 <= slot );
assert( slot < 999 );
sprintf( jobno, "%d", slot + 1 );
auto jobno = b2::value::as_string( slot + 1 );

for ( i = 0; iter != end && i < MAXARGC; ++i, iter = list_next( iter ) )
{
switch ( object_str( list_item( iter ) )[ 0 ] )
{
case '%': argv[ i ] = command; ++gotpercent; break;
case '!': argv[ i ] = jobno; break;
case '!': argv[ i ] = jobno->str(); break;
default : argv[ i ] = object_str( list_item( iter ) );
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/engine/execnt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1219,9 +1219,8 @@ static FILE * open_command_file( int32_t const slot )
DWORD const procID = GetCurrentProcessId();
string const * const tmpdir = path_tmpdir();
string_new( command_file );
string_reserve( command_file, tmpdir->size + 64 );
command_file->size = sprintf( command_file->value,
"%s\\jam%lu-%02d-##.bat", tmpdir->value, procID, slot );
string_copy( command_file, b2::value::format( "%s\\jam%lu-%02d-##.bat",
tmpdir->value, procID, slot )->str() );
}

/* For some reason opening a command file can fail intermittently. But doing
Expand Down Expand Up @@ -1321,7 +1320,6 @@ static void string_new_from_argv( string * result, char const * const * argv )
static void reportWindowsError( char const * const apiName, int32_t slot )
{
char * errorMessage;
char buf[24];
string * err_buf;
timing_info time;
DWORD const errorCode = GetLastError();
Expand All @@ -1343,8 +1341,8 @@ static void reportWindowsError( char const * const apiName, int32_t slot )
err_buf = cmdtab[ slot ].buffer_out;
string_append( err_buf, apiName );
string_append( err_buf, "() Windows API failed: " );
sprintf( buf, "%lu", errorCode );
string_append( err_buf, buf );
auto errorCode_s = std::to_string( errorCode );
string_append( err_buf, errorCode_s.c_str() );

if ( !apiResult )
string_append( err_buf, ".\n" );
Expand Down
14 changes: 6 additions & 8 deletions src/engine/filent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,19 +367,17 @@ void file_archscan( char const * arch, scanback func, void * closure )
{
FILELISTITER iter = filelist_begin( archive->members );
FILELISTITER const end = filelist_end( archive->members );
char buf[ MAXJPATH ];

for ( ; iter != end ; iter = filelist_next( iter ) )
{
file_info_t * member_file = filelist_item( iter );

/* Construct member path: 'archive-path(member-name)'
*/
sprintf( buf, "%s(%s)",
object_str( archive->file->name ),
object_str( member_file->name ) );
{
OBJECT * member = object_new( buf );
OBJECT * member = b2::value::format( "%s(%s)",
object_str( archive->file->name ),
object_str( member_file->name ) );
(*func)( closure, member, 1 /* time valid */, &member_file->time );
object_free( member );
}
Expand Down Expand Up @@ -486,9 +484,9 @@ int file_collect_archive_content_( file_archive_info_t * const archive )
name = c + 1;
}

sprintf( buf, "%.*s", int(endname - name), name );
auto name = b2::value::format( "%.*s", int(endname - name), name ) );

if ( strcmp( buf, "") != 0 )
if ( name->as_string().size > 0 )
{
file_info_t * member = 0;

Expand All @@ -497,7 +495,7 @@ int file_collect_archive_content_( file_archive_info_t * const archive )
* Here we reverse the stored sequence by pushing members to front of
* member file list to get the intended members order.
*/
archive->members = filelist_push_front( archive->members, object_new( buf ) );
archive->members = filelist_push_front( archive->members, name );
member = filelist_front( archive->members );
member->is_file = 1;
member->is_dir = 0;
Expand Down
9 changes: 3 additions & 6 deletions src/engine/filesys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ static void file_archivescan_impl( OBJECT * path, archive_scanback func, void *
{
FILELISTITER iter = filelist_begin( archive->members );
FILELISTITER const end = filelist_end( archive->members );
char buf[ MAXJPATH ];

for ( ; iter != end ; iter = filelist_next( iter ) )
{
Expand All @@ -406,12 +405,10 @@ static void file_archivescan_impl( OBJECT * path, archive_scanback func, void *

/* Construct member path: 'archive-path(member-name)'
*/
sprintf( buf, "%s(%s)",
object_str( archive->file->name ),
object_str( member_file->name ) );

{
OBJECT * member = object_new( buf );
OBJECT * member = b2::value::format( "%s(%s)",
object_str( archive->file->name ),
object_str( member_file->name ) );
(*func)( closure, member, symbols, 1, &member_file->time );
object_free( member );
}
Expand Down
26 changes: 12 additions & 14 deletions src/engine/fileunix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,17 @@ void file_archscan( char const * arch, scanback func, void * closure )
{
FILELISTITER iter = filelist_begin( archive->members );
FILELISTITER const end = filelist_end( archive->members );
char buf[ MAXJPATH ];

for ( ; iter != end ; iter = filelist_next( iter ) )
{
file_info_t * member_file = filelist_item( iter );

/* Construct member path: 'archive-path(member-name)'
*/
sprintf( buf, "%s(%s)",
object_str( archive->file->name ),
object_str( member_file->name ) );
{
OBJECT * member = object_new( buf );
OBJECT * member = b2::value::format( "%s(%s)",
object_str( archive->file->name ),
object_str( member_file->name ) );
(*func)( closure, member, 1 /* time valid */, &member_file->time );
object_free( member );
}
Expand Down Expand Up @@ -354,13 +352,13 @@ int file_collect_archive_content_( file_archive_info_t * const archive )
if ( DEBUG_BINDSCAN )
out_printf( "archive name %s found\n", lar_name );

sprintf( buf, "%s", lar_name );
auto name = b2::value::format( "%s", lar_name );

if ( strcmp( buf, "") != 0 )
if ( name->as_string().size > 0 )
{
file_info_t * member = 0;

archive->members = filelist_push_back( archive->members, object_new( buf ) );
archive->members = filelist_push_back( archive->members, name);
member = filelist_back( archive->members );
member->is_file = 1;
member->is_dir = 0;
Expand Down Expand Up @@ -419,13 +417,13 @@ static void collect_archive_content_small( int fd, file_archive_info_t * const a

ar_hdr.hdr._ar_name.ar_name[ lar_namlen ] = '\0';

sprintf( buf, "%s", ar_hdr.hdr._ar_name.ar_name );
auto name = b2::value::format( "%s", ar_hdr.hdr._ar_name.ar_name );

if ( strcmp( buf, "") != 0 )
if ( name->as_string().size > 0 )
{
file_info_t * member = 0;

archive->members = filelist_push_back( archive->members, object_new( buf ) );
archive->members = filelist_push_back( archive->members, name );
member = filelist_back( archive->members );
member->is_file = 1;
member->is_dir = 0;
Expand Down Expand Up @@ -474,13 +472,13 @@ static void collect_archive_content_big( int fd, file_archive_info_t * const arc

ar_hdr.hdr._ar_name.ar_name[ lar_namlen ] = '\0';

sprintf( buf, "%s", ar_hdr.hdr._ar_name.ar_name );
auto name = b2::value::format( "%s", ar_hdr.hdr._ar_name.ar_name );

if ( strcmp( buf, "") != 0 )
if ( name->as_string().size > 0 )
{
file_info_t * member = 0;

archive->members = filelist_push_back( archive->members, object_new( buf ) );
archive->members = filelist_push_back( archive->members, name );
member = filelist_back( archive->members );
member->is_file = 1;
member->is_dir = 0;
Expand Down
Loading

0 comments on commit 33245e2

Please sign in to comment.