Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of sprintf. #328

Merged
merged 12 commits into from
Aug 28, 2023
1 change: 1 addition & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ freebsd_task:
"CXX_PATH=`which ${CXX}`",
"cd test",
"echo \"using ${TEST_TOOLSET} : : ${CXX_PATH} ;\" > ${HOME}/user-config.jam",
"python3 grep.py ${TEST_TOOLSET}",
"python3 test_all.py ${TEST_TOOLSET}",
"cd ..",
]
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Continuously tested on:
* Linux GCC 9, 10, 11, 12
* macOS Xcode 11.7, 12.4, 12.5.1, 13.4.1, 14.0.1, 14.1, 14.2
* Windows MinGW 8.1.0
* Windows VS 2013, 2015, 2017, 2019, 2022
* Windows 2015, 2017, 2019, 2022
* Cygwin 3.1.7 x64
* Ubuntu 20.04 GCC 9 (armhf, arm64, ppc64el)
* Debian 11 GCC 10 (armhf)
Expand Down
16 changes: 10 additions & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# subject to the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#
# Copyright Rene Ferdinand Rivera Morell 2015-2020.
# Copyright Rene Ferdinand Rivera Morell 2015-2023.

branches:
only:
Expand All @@ -11,6 +11,15 @@ branches:
- /feature\/.*/
- /backport-.*/

skip_commits:
files:
- ".circleci/*"
- ".cirrus.yml"
- ".drone.star"
- ".github/workflows/*"
- ".semaphore/*"
- "azure-pipelines.yml"

environment:
matrix:
- job_name: 'Visual Studio 2017, Test'
Expand All @@ -23,11 +32,6 @@ environment:
TOOLSET: vc14
TEST_TOOLSET: msvc-14.0
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
- job_name: 'Visual Studio 2013, Test'
job_group: 'Test'
TOOLSET: vc12
TEST_TOOLSET: msvc-12.0
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
- job_name: 'Clang 9 (VS2015), Test'
job_group: 'Test'
TOOLSET: clang-win
Expand Down
1 change: 1 addition & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ stages:
Clang ${{variables.clang_latest}} (VS${{variables.vs_latest}}): {TOOLSET: clang-win, TEST_TOOLSET: clang-win, VM_IMAGE: "${{variables.windows_latest_vm}}"}
pool:
vmImage: $(VM_IMAGE)
continueOnError: 'true'
steps:
- template: .ci/azp-windows-test.yml

Expand Down
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
27 changes: 1 addition & 26 deletions src/engine/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define B2_CONFIG_H

/*
Copyright 2002-2021 Rene Rivera.
Copyright 2002-2023 Rene Rivera.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or copy at
https://www.bfgroup.xyz/b2/LICENSE.txt)
Expand Down Expand Up @@ -42,38 +42,13 @@ Distributed under the Boost Software License, Version 1.0.
#define _FILE_OFFSET_BITS 64
#endif

// Correct missing types in some earlier compilers..

#include <stdint.h>
#ifndef INT32_MIN

// VS 2013 is barely C++11/C99. And opts to not provide specific sized int
// types. Provide a generic implementation of the sizes we use.
#if UINT_MAX == 0xffffffff
typedef int int32_t;
#elif (USHRT_MAX == 0xffffffff)
typedef short int32_t;
#elif ULONG_MAX == 0xffffffff
typedef long int32_t;
#endif

#endif

// Account for incomplete C++ standard implementations.

// VS 2012 doesn't implement noexcept.
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define B2_NOEXCEPT
#endif

#ifndef B2_NOEXCEPT
#define B2_NOEXCEPT noexcept
#endif

// Indicate if we can use std::thread and friends.
#if defined(_MSC_VER) && _MSC_VER <= 1800
#define B2_USE_STD_THREADS 0
#endif
#ifndef B2_USE_STD_THREADS
#define B2_USE_STD_THREADS 1
#endif
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 member_name = b2::value::format( "%.*s", int(endname - name), name );

if ( strcmp( buf, "") != 0 )
if ( member_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, member_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
Loading
Loading