From e279592ce697b49e2fdfe117a6f14b542b269869 Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Sat, 15 Jun 2024 01:29:08 +0200 Subject: [PATCH] Cut down on some snprintf calls --- command.c | 18 +++++++++++------- network/netplay/netplay_frontend.c | 25 ++++++++++++++++++------- tasks/task_database.c | 20 +++++++++++++++----- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/command.c b/command.c index 6cf74cced0b..ce838a30550 100644 --- a/command.c +++ b/command.c @@ -413,7 +413,7 @@ bool command_get_config_param(command_t *cmd, const char* arg) (long long)(input_st->bsv_movie_state_handle->identifier), input_st->bsv_movie_state.flags); else - snprintf(value_dynamic, sizeof(value_dynamic), "0 0"); + strlcpy(value_dynamic, "0 0", sizeof(value_dynamic)); } #endif /* TODO: query any string */ @@ -919,7 +919,6 @@ bool command_get_status(command_t *cmd, const char* arg) { /* add some content info */ runloop_state_t *runloop_st = runloop_state_get_ptr(); - const char *status = "PLAYING"; const char *content_name = path_basename(path_get(RARCH_PATH_BASENAME)); /* filename only without ext */ int content_crc32 = content_get_crc(); const char* system_id = NULL; @@ -929,15 +928,20 @@ bool command_get_status(command_t *cmd, const char* arg) core_info_get_current_core(&core_info); - if (runloop_st->flags & RUNLOOP_FLAG_PAUSED) - status = "PAUSED"; if (core_info) system_id = core_info->system_id; if (!system_id) system_id = runloop_st->system.info.library_name; - - _len = snprintf(reply, sizeof(reply), "GET_STATUS %s %s,%s,crc32=%x\n", - status, system_id, content_name, content_crc32); + _len = strlcpy(reply, "GET_STATUS ", sizeof(reply)); + if (runloop_st->flags & RUNLOOP_FLAG_PAUSED) + _len += strlcpy(reply + _len, "PAUSED", sizeof(reply) - _len); + else + _len += strlcpy(reply + _len, "PLAYING", sizeof(reply) - _len); + _len += strlcpy(reply + _len, " ", sizeof(reply) - _len); + _len += strlcpy(reply + _len, system_id, sizeof(reply) - _len); + _len += strlcpy(reply + _len, ",", sizeof(reply) - _len); + _len += strlcpy(reply + _len, content_name, sizeof(reply) - _len); + _len += snprintf(reply + _len, sizeof(reply) - _len, ",crc32=%x\n", content_crc32); } else _len = strlcpy(reply, "GET_STATUS CONTENTLESS", sizeof(reply)); diff --git a/network/netplay/netplay_frontend.c b/network/netplay/netplay_frontend.c index f7c01e8cd6d..9d726439b62 100644 --- a/network/netplay/netplay_frontend.c +++ b/network/netplay/netplay_frontend.c @@ -572,8 +572,16 @@ static bool netplay_lan_ad_server(netplay_t *netplay) frontend_driver_get_cpu_architecture_str(frontend_architecture_tmp, sizeof(frontend_architecture_tmp)); if (frontend_drv) - snprintf(ad_packet_buffer.frontend, sizeof(ad_packet_buffer.frontend), - "%s %s", frontend_drv->ident, frontend_architecture_tmp); + { + size_t _len = strlcpy(ad_packet_buffer.frontend, frontend_drv->ident, + sizeof(ad_packet_buffer.frontend)); + _len += strlcpy(ad_packet_buffer.frontend + _len, + " ", + sizeof(ad_packet_buffer.frontend) - _len); + strlcpy(ad_packet_buffer.frontend + _len, + frontend_architecture_tmp, + sizeof(ad_packet_buffer.frontend) - _len); + } else strlcpy(ad_packet_buffer.frontend, "N/A", sizeof(ad_packet_buffer.frontend)); @@ -1152,9 +1160,12 @@ static void netplay_handshake_ready(netplay_t *netplay, netplay->force_send_savestate = true; } else - snprintf(msg, sizeof(msg), "%s: \"%s\"", - msg_hash_to_str(MSG_CONNECTED_TO), - connection->nick); + { + size_t _len = strlcpy(msg, msg_hash_to_str(MSG_CONNECTED_TO), + sizeof(msg)); + snprintf(msg + _len, sizeof(msg) - _len, ": \"%s\"", + connection->nick); + } RARCH_LOG("[Netplay] %s\n", msg); /* Useful notification to the client in figuring out @@ -4206,8 +4217,8 @@ static void netplay_hangup(netplay_t *netplay, if (netplay->modus != NETPLAY_MODUS_CORE_PACKET_INTERFACE) { - /* This special mode keeps the connection object - alive long enough to send the disconnection + /* This special mode keeps the connection object + alive long enough to send the disconnection message at the correct time */ connection->mode = NETPLAY_CONNECTION_DELAYED_DISCONNECT; connection->delay_frame = netplay->read_frame_count[client_num]; diff --git a/tasks/task_database.c b/tasks/task_database.c index 717aed82650..8e37b47fb46 100644 --- a/tasks/task_database.c +++ b/tasks/task_database.c @@ -122,8 +122,9 @@ static void task_database_scan_console_output(const char *label, const char *db_ unsigned green = FOREGROUND_GREEN; unsigned yellow = FOREGROUND_RED | FOREGROUND_GREEN; unsigned reset = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; - - snprintf(string, sizeof(string), " %s ", prefix); + size_t _len = strlcpy(string, " ", sizeof(string)); + _len += strlcpy(string + _len, prefix, sizeof(string) - _len); + strlcpy(string + _len, " ", sizeof(string) - _len); SetConsoleTextAttribute(con, (add) ? green : (db_name) ? yellow : red); WriteConsole(con, string, strlen(string), NULL, NULL); SetConsoleTextAttribute(con, reset); @@ -135,14 +136,23 @@ static void task_database_scan_console_output(const char *label, const char *db_ const char *green = "\x1B[32m"; const char *yellow = "\x1B[33m"; const char *reset = "\x1B[0m"; - - snprintf(string, sizeof(string), "%s %s %s", (add) ? green : (db_name) ? yellow : red, prefix, reset); + size_t _len = 0; + if (add) + _len += strlcpy(string + _len, green, sizeof(string) - _len); + else + _len += strlcpy(string + _len, (db_name) ? yellow : red, sizeof(string) - _len); + _len += strlcpy(string + _len, " ", sizeof(string) - _len); + _len += strlcpy(string + _len, prefix, sizeof(string) - _len); + _len += strlcpy(string + _len, " ", sizeof(string) - _len); + strlcpy(string + _len, reset, sizeof(string) - _len); fputs(string, stdout); } #endif else { - snprintf(string, sizeof(string), " %s ", prefix); + size_t _len = strlcpy(string, " ", sizeof(string)); + _len += strlcpy(string + _len, prefix, sizeof(string) - _len); + strlcpy(string + _len, " ", sizeof(string) - _len); fputs(string, stdout); }