From 216deda91074de958f3ca420d5e8f7f761f06635 Mon Sep 17 00:00:00 2001 From: Jamiras Date: Tue, 18 May 2021 08:31:44 -0600 Subject: [PATCH] replace sprintf with snprintf --- cheevos/cheevos.c | 66 +++++++++++++++++++----------------- deps/rcheevos/src/rurl/url.c | 2 +- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/cheevos/cheevos.c b/cheevos/cheevos.c index 84cf2d0087..062a8b146b 100644 --- a/cheevos/cheevos.c +++ b/cheevos/cheevos.c @@ -199,73 +199,77 @@ void rcheevos_log(const char *fmt, ...) } #endif +static int append_no_spaces(char* buffer, size_t len, const char* text) +{ + char* ptr = buffer; + char* stop = buffer + len - 1; + + while (ptr < stop && *text) + { + if (*text == ' ') + { + *ptr++ = '_'; + ++text; + } + else + { + *ptr++ = *text++; + } + } + + *ptr = '\0'; + return (ptr - buffer); +} + static void rcheevos_get_user_agent( rcheevos_locals_t *locals, char *buffer, size_t len) { struct retro_system_info *system = runloop_get_libretro_system_info(); - const char* scan; char* ptr; + /* if we haven't calculated the non-changing portion yet, do so now [retroarch version + os version] */ if (!locals->user_agent_prefix[0]) { const frontend_ctx_driver_t *frontend = frontend_get_ptr(); int major, minor; char tmp[64]; - ptr = locals->user_agent_prefix + snprintf(locals->user_agent_prefix, sizeof(locals->user_agent_prefix), "RetroArch/%s", PACKAGE_VERSION); + ptr = locals->user_agent_prefix + + strlcpy(locals->user_agent_prefix, "RetroArch/" PACKAGE_VERSION, + sizeof(locals->user_agent_prefix)); if (frontend && frontend->get_os) { frontend->get_os(tmp, sizeof(tmp), &major, &minor); - ptr += sprintf(ptr, " (%s %d.%d)", tmp, major, minor); - (void)ptr; + snprintf(ptr, len - (ptr - locals->user_agent_prefix), " (%s %d.%d)", tmp, major, minor); } } - ptr = buffer + snprintf(buffer, len, "%s", locals->user_agent_prefix); + /* append the non-changing portion */ + ptr = buffer + strlcpy(buffer, locals->user_agent_prefix, len); + /* if a core is loaded, append its information */ if (system && !string_is_empty(system->library_name)) { + *ptr++ = ' '; + const char* path = path_get(RARCH_PATH_CORE); if (!string_is_empty(path)) { - sprintf(ptr, " %s", path_basename(path)); + append_no_spaces(ptr, len - (ptr - buffer), path_basename(path)); path_remove_extension(ptr); ptr += strlen(ptr); } else { - *ptr++ = ' '; - - scan = system->library_name; - while (*scan) - { - if (*scan == ' ') - { - *ptr++ = '_'; - ++scan; - } - else - *ptr++ = *scan++; - } + ptr += append_no_spaces(ptr, len - (ptr - buffer), system->library_name); } if (system->library_version) { *ptr++ = '/'; - - scan = system->library_version; - while (*scan) - { - if (*scan == ' ') - { - *ptr++ = '_'; - ++scan; - } - else - *ptr++ = *scan++; - } + ptr += append_no_spaces(ptr, len - (ptr - buffer), system->library_version); } } diff --git a/deps/rcheevos/src/rurl/url.c b/deps/rcheevos/src/rurl/url.c index 6b7c4607b5..1e37cbd569 100644 --- a/deps/rcheevos/src/rurl/url.c +++ b/deps/rcheevos/src/rurl/url.c @@ -298,7 +298,7 @@ static int rc_url_append_unum(char* buffer, size_t buffer_size, size_t* buffer_o int written = rc_url_append_param_equals(buffer, buffer_size, *buffer_offset, param); if (written > 0) { char num[16]; - int chars = sprintf(num, "%u", value); + int chars = snprintf(num, sizeof(num), "%u", value); if (chars + written < (int)buffer_size) {