(cheevos) Cleanups - only use snprintf for formatting

This commit is contained in:
libretroadmin 2024-06-16 22:20:15 +02:00
parent 38f278519c
commit 3fc7024df9
7 changed files with 592 additions and 568 deletions

View File

@ -398,15 +398,15 @@ static void rcheevos_show_mastery_placard(void)
#if defined (HAVE_GFX_WIDGETS) #if defined (HAVE_GFX_WIDGETS)
if (gfx_widgets_ready()) if (gfx_widgets_ready())
{ {
char msg[128];
char badge_name[32];
const char* displayname = rc_client_get_user_info(rcheevos_locals.client)->display_name; const char* displayname = rc_client_get_user_info(rcheevos_locals.client)->display_name;
const bool content_runtime_log = settings->bools.content_runtime_log; const bool content_runtime_log = settings->bools.content_runtime_log;
const bool content_runtime_log_aggr = settings->bools.content_runtime_log_aggregate; const bool content_runtime_log_aggr = settings->bools.content_runtime_log_aggregate;
char badge_name[32];
char msg[128];
size_t len = strlcpy(msg, displayname, sizeof(msg)); size_t len = strlcpy(msg, displayname, sizeof(msg));
if (len < sizeof(msg) - 12 && if (len < sizeof(msg) - 12
(content_runtime_log || content_runtime_log_aggr)) && (content_runtime_log || content_runtime_log_aggr))
{ {
const char* content_path = path_get(RARCH_PATH_CONTENT); const char* content_path = path_get(RARCH_PATH_CONTENT);
const char* core_path = path_get(RARCH_PATH_CORE); const char* core_path = path_get(RARCH_PATH_CORE);
@ -422,7 +422,7 @@ static void rcheevos_show_mastery_placard(void)
runtime_log_add_runtime_usec(runtime_log, runtime_log_add_runtime_usec(runtime_log,
runloop_state->core_runtime_usec); runloop_state->core_runtime_usec);
len += snprintf(msg + len, sizeof(msg) - len, " | "); len += strlcpy(msg + len, " | ", sizeof(msg) - len);
runtime_log_get_runtime_str(runtime_log, msg + len, sizeof(msg) - len); runtime_log_get_runtime_str(runtime_log, msg + len, sizeof(msg) - len);
msg[sizeof(msg) - 1] = '\0'; msg[sizeof(msg) - 1] = '\0';
@ -430,7 +430,8 @@ static void rcheevos_show_mastery_placard(void)
} }
} }
snprintf(badge_name, sizeof(badge_name), "i%s", game->badge_name); len = strlcpy(badge_name, "i", sizeof(badge_name));
strlcpy(badge_name + len, game->badge_name, sizeof(badge_name) - len);
gfx_widgets_push_achievement(title, msg, badge_name); gfx_widgets_push_achievement(title, msg, badge_name);
} }
else else
@ -474,8 +475,10 @@ static void rcheevos_award_achievement(const rc_client_achievement_t* cheevo)
#endif #endif
{ {
char buffer[256]; char buffer[256];
snprintf(buffer, sizeof(buffer), "%s: %s", size_t _len = strlcpy(buffer, msg_hash_to_str(MSG_ACHIEVEMENT_UNLOCKED),
msg_hash_to_str(MSG_ACHIEVEMENT_UNLOCKED), cheevo->title); sizeof(buffer));
_len += strlcpy(buffer + _len, ": ", sizeof(buffer) - _len);
strlcpy(buffer + _len, cheevo->title, sizeof(buffer) - _len);
runloop_msg_queue_push(buffer, 0, 2 * 60, false, NULL, runloop_msg_queue_push(buffer, 0, 2 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO); MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
runloop_msg_queue_push(cheevo->description, 0, 3 * 60, false, NULL, runloop_msg_queue_push(cheevo->description, 0, 3 * 60, false, NULL,
@ -558,12 +561,13 @@ static void rcheevos_lboard_started(const rc_client_leaderboard_t* lboard)
if (settings->bools.cheevos_visibility_lboard_start) if (settings->bools.cheevos_visibility_lboard_start)
{ {
char buffer[256]; char buffer[256];
size_t _len = strlcpy(buffer, msg_hash_to_str(MSG_LEADERBOARD_STARTED),
sizeof(buffer));
_len += strlcpy(buffer + _len, ": ", sizeof(buffer) - _len);
_len += strlcpy(buffer + _len, lboard->title, sizeof(buffer) - _len);
if (lboard->description && *lboard->description) if (lboard->description && *lboard->description)
snprintf(buffer, sizeof(buffer), "%s: %s - %s", snprintf(buffer + _len, sizeof(buffer) - _len, "- %s",
msg_hash_to_str(MSG_LEADERBOARD_STARTED), lboard->title, lboard->description); lboard->description);
else
snprintf(buffer, sizeof(buffer), "%s: %s",
msg_hash_to_str(MSG_LEADERBOARD_STARTED), lboard->title);
runloop_msg_queue_push(buffer, 0, 2 * 60, false, NULL, runloop_msg_queue_push(buffer, 0, 2 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO); MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
@ -623,8 +627,9 @@ static void rcheevos_client_log_message(const char* message, const rc_client_t*
static void rcheevos_server_error(const char* api_name, const char* message) static void rcheevos_server_error(const char* api_name, const char* message)
{ {
char buffer[256]; char buffer[256];
snprintf(buffer, sizeof(buffer), "%s failed: %s", api_name, message); size_t _len = strlcpy(buffer, api_name, sizeof(buffer));
_len += strlcpy(buffer + _len, " failed: ", sizeof(buffer) - _len);
_len += strlcpy(buffer + _len, message, sizeof(buffer) - _len);
runloop_msg_queue_push(buffer, 0, 4 * 60, false, NULL, runloop_msg_queue_push(buffer, 0, 4 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_ERROR); MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_ERROR);
} }
@ -786,8 +791,10 @@ void rcheevos_award_achievement(rcheevos_locals_t* locals,
#endif #endif
{ {
char buffer[256]; char buffer[256];
snprintf(buffer, sizeof(buffer), "%s: %s", size_t _len = strlcpy(buffer, msg_hash_to_str(MSG_ACHIEVEMENT_UNLOCKED),
msg_hash_to_str(MSG_ACHIEVEMENT_UNLOCKED), cheevo->title); sizeof(buffer));
_len += strlcpy(buffer + _len, ": ", sizeof(buffer) - _len);
_len += strlcpy(buffer + _len, cheevo->title, sizeof(buffer) - _len);
runloop_msg_queue_push(buffer, 0, 2 * 60, false, NULL, runloop_msg_queue_push(buffer, 0, 2 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO); MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
runloop_msg_queue_push(cheevo->description, 0, 3 * 60, false, NULL, runloop_msg_queue_push(cheevo->description, 0, 3 * 60, false, NULL,
@ -867,7 +874,8 @@ static void rcheevos_assign_leaderboard_tracker_ids(rcheevos_locals_t* locals)
unsigned tracker_id; unsigned tracker_id;
char buffer[32]; char buffer[32];
for (; lboard < end; ++lboard) { for (; lboard < end; ++lboard)
{
if (lboard->active_tracker_id != 0xFF) if (lboard->active_tracker_id != 0xFF)
continue; continue;
@ -875,7 +883,8 @@ static void rcheevos_assign_leaderboard_tracker_ids(rcheevos_locals_t* locals)
if (locals->active_lboard_trackers != 0 && lboard->value_hash != 0) if (locals->active_lboard_trackers != 0 && lboard->value_hash != 0)
{ {
scan = locals->game.leaderboards; scan = locals->game.leaderboards;
for (; scan < end; ++scan) { for (; scan < end; ++scan)
{
if (scan->active_tracker_id == 0 || scan->active_tracker_id == 0xFF) if (scan->active_tracker_id == 0 || scan->active_tracker_id == 0xFF)
continue; continue;
@ -983,7 +992,6 @@ static void rcheevos_lboard_canceled(rcheevos_ralboard_t * lboard,
bool widgets_ready) bool widgets_ready)
{ {
const settings_t *settings = config_get_ptr(); const settings_t *settings = config_get_ptr();
char buffer[256];
if (!lboard) if (!lboard)
return; return;
@ -997,8 +1005,11 @@ static void rcheevos_lboard_canceled(rcheevos_ralboard_t * lboard,
if (settings->bools.cheevos_visibility_lboard_cancel) if (settings->bools.cheevos_visibility_lboard_cancel)
{ {
snprintf(buffer, sizeof(buffer), "%s: %s", char buffer[256];
msg_hash_to_str(MSG_LEADERBOARD_FAILED), lboard->title); size_t _len = strlcpy(buffer, msg_hash_to_str(MSG_LEADERBOARD_FAILED),
sizeof(buffer));
_len += strlcpy(buffer + _len, ": ", sizeof(buffer) - _len);
strlcpy(buffer + _len, lboard->title, sizeof(buffer) - _len);
runloop_msg_queue_push(buffer, 0, 2 * 60, false, NULL, runloop_msg_queue_push(buffer, 0, 2 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO); MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
} }
@ -1032,12 +1043,13 @@ static void rcheevos_lboard_started(
if (settings->bools.cheevos_visibility_lboard_start) if (settings->bools.cheevos_visibility_lboard_start)
{ {
size_t _len = strlcpy(buffer, msg_hash_to_str(MSG_LEADERBOARD_STARTED),
sizeof(buffer));
_len += strlcpy(buffer + _len, ": ", sizeof(buffer) - _len);
_len += strlcpy(buffer + _len, lboard->title, sizeof(buffer) - _len);
if (lboard->description && *lboard->description) if (lboard->description && *lboard->description)
snprintf(buffer, sizeof(buffer), "%s: %s - %s", snprintf(buffer + _len, sizeof(buffer) - _len, "- %s",
msg_hash_to_str(MSG_LEADERBOARD_STARTED), lboard->title, lboard->description); lboard->description);
else
snprintf(buffer, sizeof(buffer), "%s: %s",
msg_hash_to_str(MSG_LEADERBOARD_STARTED), lboard->title);
runloop_msg_queue_push(buffer, 0, 2 * 60, false, NULL, runloop_msg_queue_push(buffer, 0, 2 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO); MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
@ -1782,6 +1794,7 @@ void rcheevos_validate_config_settings(void)
if (!rc_libretro_is_setting_allowed(disallowed_settings, key, val)) if (!rc_libretro_is_setting_allowed(disallowed_settings, key, val))
{ {
char buffer[128]; char buffer[128];
/* TODO/FIXME - localize */
snprintf(buffer, sizeof(buffer), "Hardcore paused. Setting not allowed: %s=%s", key, val); snprintf(buffer, sizeof(buffer), "Hardcore paused. Setting not allowed: %s=%s", key, val);
CHEEVOS_LOG(RCHEEVOS_TAG "%s\n", buffer); CHEEVOS_LOG(RCHEEVOS_TAG "%s\n", buffer);
rcheevos_pause_hardcore(); rcheevos_pause_hardcore();
@ -2344,7 +2357,8 @@ static void rcheevos_show_game_placard(void)
if (gfx_widgets_ready()) if (gfx_widgets_ready())
{ {
char badge_name[32]; char badge_name[32];
snprintf(badge_name, sizeof(badge_name), "i%s", game->badge_name); size_t _len = strlcpy(badge_name, "i", sizeof(badge_name));
_len += strlcpy(badge_name + _len, game->badge_name, sizeof(badge_name) - _len);
gfx_widgets_push_achievement(game->title, msg, badge_name); gfx_widgets_push_achievement(game->title, msg, badge_name);
} }
else else
@ -2407,12 +2421,13 @@ static void rcheevos_client_login_callback(int result,
const char* error_message, rc_client_t* client, void* userdata) const char* error_message, rc_client_t* client, void* userdata)
{ {
const rc_client_user_t* user; const rc_client_user_t* user;
char msg[256] = "";
if (result != RC_OK) if (result != RC_OK)
{ {
snprintf(msg, sizeof(msg), "RetroAchievements login failed: %s", char msg[256];
error_message); size_t _len = strlcpy(msg, "RetroAchievements login failed: ",
sizeof(msg));
_len += strlcpy(msg + _len, error_message, sizeof(msg) - _len);
CHEEVOS_LOG(RCHEEVOS_TAG "%s\n", msg); CHEEVOS_LOG(RCHEEVOS_TAG "%s\n", msg);
runloop_msg_queue_push(msg, 0, 2 * 60, false, NULL, runloop_msg_queue_push(msg, 0, 2 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO); MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
@ -2444,6 +2459,7 @@ static void rcheevos_client_login_callback(int result,
if (settings->bools.cheevos_visibility_account) if (settings->bools.cheevos_visibility_account)
{ {
/* TODO/FIXME - localize */ /* TODO/FIXME - localize */
char msg[256];
snprintf(msg, sizeof(msg), snprintf(msg, sizeof(msg),
"RetroAchievements: Logged in as \"%s\".", "RetroAchievements: Logged in as \"%s\".",
user->display_name); user->display_name);
@ -2595,9 +2611,9 @@ void rcheevos_show_mastery_placard(void)
#if defined (HAVE_GFX_WIDGETS) #if defined (HAVE_GFX_WIDGETS)
if (gfx_widgets_ready()) if (gfx_widgets_ready())
{ {
char msg[128];
const bool content_runtime_log = settings->bools.content_runtime_log; const bool content_runtime_log = settings->bools.content_runtime_log;
const bool content_runtime_log_aggr = settings->bools.content_runtime_log_aggregate; const bool content_runtime_log_aggr = settings->bools.content_runtime_log_aggregate;
char msg[128];
size_t len = strlcpy(msg, rcheevos_locals.displayname, sizeof(msg)); size_t len = strlcpy(msg, rcheevos_locals.displayname, sizeof(msg));
if (len < sizeof(msg) - 12 && if (len < sizeof(msg) - 12 &&

View File

@ -244,8 +244,8 @@ static void frontend_ctr_exec(const char *path, bool should_load_game)
is corrupt so we have to quit */ is corrupt so we have to quit */
{ {
char error[PATH_MAX + 32]; char error[PATH_MAX + 32];
size_t _len = strlcpy(error, "Can't launch core: ", sizeof(error));
snprintf(error, sizeof(error), "Can't launch core: %s", path); strlcpy(error + _len, path, sizeof(error) - _len);
error_and_quit(error); error_and_quit(error);
} }
} }

View File

@ -79,12 +79,10 @@ static void frontend_emscripten_get_env(int *argc, char *argv[],
if (home) if (home)
{ {
base_path[0] = '\0'; size_t _len = strlcpy(base_path, home, sizeof(base_path));
user_path[0] = '\0'; strlcpy(base_path + _len, "/retroarch", sizeof(base_path) - _len);
snprintf(base_path, sizeof(base_path), _len = strlcpy(user_path, home, sizeof(user_path));
"%s/retroarch", home); strlcpy(user_path + _len, "/retroarch/userdata", sizeof(user_path) - _len);
snprintf(user_path, sizeof(user_path),
"%s/retroarch/userdata", home);
} }
else else
{ {

View File

@ -750,8 +750,8 @@ static void frontend_win32_respawn(char *s, size_t len, char *args)
{ {
STARTUPINFO si; STARTUPINFO si;
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
char executable_args[PATH_MAX_LENGTH];
char executable_path[PATH_MAX_LENGTH] = {0}; char executable_path[PATH_MAX_LENGTH] = {0};
char executable_args[PATH_MAX_LENGTH] = {0};
if (win32_fork_mode != FRONTEND_FORK_RESTART) if (win32_fork_mode != FRONTEND_FORK_RESTART)
return; return;
@ -761,7 +761,7 @@ static void frontend_win32_respawn(char *s, size_t len, char *args)
path_set(RARCH_PATH_CORE, executable_path); path_set(RARCH_PATH_CORE, executable_path);
/* Remove executable path from arguments given to CreateProcess */ /* Remove executable path from arguments given to CreateProcess */
snprintf(executable_args, sizeof(executable_args), "%s", strstr(args, ".exe") + 4); strlcpy(executable_args, strstr(args, ".exe") + 4, sizeof(executable_args));
memset(&si, 0, sizeof(si)); memset(&si, 0, sizeof(si));
si.cb = sizeof(si); si.cb = sizeof(si);

View File

@ -269,7 +269,7 @@ static void frontend_xdk_exec(const char *path, bool should_load_content)
memset(&ptr, 0, sizeof(ptr)); memset(&ptr, 0, sizeof(ptr));
if (should_load_content && !path_is_empty(RARCH_PATH_CONTENT)) if (should_load_content && !path_is_empty(RARCH_PATH_CONTENT))
snprintf((char*)ptr.Data, sizeof(ptr.Data), "%s", path_get(RARCH_PATH_CONTENT)); strlcpy((char*)ptr.Data, path_get(RARCH_PATH_CONTENT), sizeof(ptr.Data));
if (!string_is_empty(path)) if (!string_is_empty(path))
XLaunchNewImage(path, !string_is_empty((const char*)ptr.Data) ? &ptr : NULL); XLaunchNewImage(path, !string_is_empty((const char*)ptr.Data) ? &ptr : NULL);

View File

@ -269,12 +269,17 @@ bool recording_init(void)
unsigned video_record_quality = settings->uints.video_record_quality; unsigned video_record_quality = settings->uints.video_record_quality;
unsigned video_stream_port = settings->uints.video_stream_port; unsigned video_stream_port = settings->uints.video_stream_port;
if (recording_st->streaming_enable) if (recording_st->streaming_enable)
{
if (!string_is_empty(stream_url)) if (!string_is_empty(stream_url))
strlcpy(output, stream_url, sizeof(output)); strlcpy(output, stream_url, sizeof(output));
else else
{
/* Fallback, stream locally to 127.0.0.1 */ /* Fallback, stream locally to 127.0.0.1 */
snprintf(output, sizeof(output), "udp://127.0.0.1:%u", size_t _len = strlcpy(output, "udp://127.0.0.1:", sizeof(output));
snprintf(output + _len, sizeof(output) - _len, "%u",
video_stream_port); video_stream_port);
}
}
else else
{ {
const char *game_name = path_basename(path_get(RARCH_PATH_BASENAME)); const char *game_name = path_basename(path_get(RARCH_PATH_BASENAME));
@ -489,13 +494,14 @@ void recording_driver_update_streaming_url(void)
} }
break; break;
case STREAMING_MODE_LOCAL: case STREAMING_MODE_LOCAL:
{
/* TODO: figure out default interface and bind to that instead */ /* TODO: figure out default interface and bind to that instead */
snprintf(settings->paths.path_stream_url, sizeof(settings->paths.path_stream_url), size_t _len = strlcpy(settings->paths.path_stream_url, "udp://127.0.0.1:",
"udp://%s:%u", "127.0.0.1", settings->uints.video_stream_port); sizeof(settings->paths.path_stream_url));
break; snprintf(settings->paths.path_stream_url + _len,
case STREAMING_MODE_CUSTOM: sizeof(settings->paths.path_stream_url) - _len,
default: "%u", settings->uints.video_stream_port);
/* Do nothing, let the user input the URL */ }
break; break;
case STREAMING_MODE_FACEBOOK: case STREAMING_MODE_FACEBOOK:
if (!string_is_empty(settings->arrays.facebook_stream_key)) if (!string_is_empty(settings->arrays.facebook_stream_key))
@ -508,5 +514,9 @@ void recording_driver_update_streaming_url(void)
sizeof(settings->paths.path_stream_url) - _len); sizeof(settings->paths.path_stream_url) - _len);
} }
break; break;
case STREAMING_MODE_CUSTOM:
default:
/* Do nothing, let the user input the URL */
break;
} }
} }

View File

@ -8171,7 +8171,7 @@ int retroarch_get_capabilities(enum rarch_capabilities type,
_MSC_VER); _MSC_VER);
#elif defined(__SNC__) #elif defined(__SNC__)
_len = strlcpy(str_out, msg_hash_to_str(MSG_COMPILER), str_len); _len = strlcpy(str_out, msg_hash_to_str(MSG_COMPILER), str_len);
_len += snprintf(str_out + _len, str_len - _Len, ": SNC (%d)", _len += snprintf(str_out + _len, str_len - _len, ": SNC (%d)",
__SN_VER__); __SN_VER__);
#elif defined(_WIN32) && defined(__GNUC__) #elif defined(_WIN32) && defined(__GNUC__)
_len = strlcpy(str_out, msg_hash_to_str(MSG_COMPILER), str_len); _len = strlcpy(str_out, msg_hash_to_str(MSG_COMPILER), str_len);