replace sprintf with snprintf

This commit is contained in:
Jamiras 2021-05-18 08:31:44 -06:00
parent 7bbdd6d18a
commit 216deda910
2 changed files with 36 additions and 32 deletions

View File

@ -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);
}
}

View File

@ -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)
{