Cut down on strlcats

This commit is contained in:
libretroadmin 2024-06-16 19:15:07 +02:00
parent c05ec27334
commit c4685e8c31
5 changed files with 46 additions and 59 deletions

View File

@ -175,7 +175,7 @@ frontend_ctx_driver_t *frontend_ctx_init_first(void)
return frontend_ctx_drivers[0]; return frontend_ctx_drivers[0];
} }
bool frontend_driver_get_core_extension(char *s, size_t len) size_t frontend_driver_get_core_extension(char *s, size_t len)
{ {
#ifdef HAVE_DYNAMIC #ifdef HAVE_DYNAMIC
@ -184,7 +184,7 @@ bool frontend_driver_get_core_extension(char *s, size_t len)
s[1] = 'l'; s[1] = 'l';
s[2] = 'l'; s[2] = 'l';
s[3] = '\0'; s[3] = '\0';
return true; return 3;
#elif defined(IOS) #elif defined(IOS)
s[0] = 'f'; s[0] = 'f';
s[1] = 'r'; s[1] = 'r';
@ -196,7 +196,7 @@ bool frontend_driver_get_core_extension(char *s, size_t len)
s[7] = 'r'; s[7] = 'r';
s[8] = 'k'; s[8] = 'k';
s[9] = '\0'; s[9] = '\0';
return true; return 9;
#elif defined(__APPLE__) || defined(__MACH__) #elif defined(__APPLE__) || defined(__MACH__)
s[0] = 'd'; s[0] = 'd';
s[1] = 'y'; s[1] = 'y';
@ -204,12 +204,12 @@ bool frontend_driver_get_core_extension(char *s, size_t len)
s[3] = 'i'; s[3] = 'i';
s[4] = 'b'; s[4] = 'b';
s[5] = '\0'; s[5] = '\0';
return true; return 5;
#else #else
s[0] = 's'; s[0] = 's';
s[1] = 'o'; s[1] = 'o';
s[2] = '\0'; s[2] = '\0';
return true; return 2;
#endif #endif
#else #else
@ -219,76 +219,60 @@ bool frontend_driver_get_core_extension(char *s, size_t len)
s[1] = 'b'; s[1] = 'b';
s[2] = 'p'; s[2] = 'p';
s[3] = '\0'; s[3] = '\0';
return true; return 3;
#elif defined(ORBIS) || defined(VITA) || defined(__PS3__) #elif defined(ORBIS) || defined(VITA) || defined(__PS3__)
strlcpy(s, "self|bin", len); return strlcpy(s, "self|bin", len);
return true;
#elif defined(PS2) #elif defined(PS2)
s[0] = 'e'; s[0] = 'e';
s[1] = 'l'; s[1] = 'l';
s[2] = 'f'; s[2] = 'f';
s[3] = '\0'; s[3] = '\0';
return true; return 3;
#elif defined(_XBOX1) #elif defined(_XBOX1)
s[0] = 'x'; s[0] = 'x';
s[1] = 'b'; s[1] = 'b';
s[2] = 'e'; s[2] = 'e';
s[3] = '\0'; s[3] = '\0';
return true; return 3;
#elif defined(_XBOX360) #elif defined(_XBOX360)
s[0] = 'x'; s[0] = 'x';
s[1] = 'e'; s[1] = 'e';
s[2] = 'x'; s[2] = 'x';
s[3] = '\0'; s[3] = '\0';
return true; return 3;
#elif defined(GEKKO) #elif defined(GEKKO)
s[0] = 'd'; s[0] = 'd';
s[1] = 'o'; s[1] = 'o';
s[2] = 'l'; s[2] = 'l';
s[3] = '\0'; s[3] = '\0';
return true; return 3;
#elif defined(HW_WUP) #elif defined(HW_WUP)
strlcpy(s, "rpx|elf", len); return strlcpy(s, "rpx|elf", len);
return true;
#elif defined(__linux__) #elif defined(__linux__)
s[0] = 'e'; s[0] = 'e';
s[1] = 'l'; s[1] = 'l';
s[2] = 'f'; s[2] = 'f';
s[3] = '\0'; s[3] = '\0';
return true; return 3;
#elif defined(HAVE_LIBNX) #elif defined(HAVE_LIBNX)
s[0] = 'n'; s[0] = 'n';
s[1] = 'r'; s[1] = 'r';
s[2] = 'o'; s[2] = 'o';
s[3] = '\0'; s[3] = '\0';
return true; return 3;
#elif defined(DJGPP) #elif defined(DJGPP)
s[0] = 'e'; s[0] = 'e';
s[1] = 'x'; s[1] = 'x';
s[2] = 'e'; s[2] = 'e';
s[3] = '\0'; s[3] = '\0';
return true; return 3;
#elif defined(_3DS) #elif defined(_3DS)
if (envIsHomebrew()) if (envIsHomebrew())
{ return strlcpy(s, "3dsx", len);
s[0] = '3'; return strlcpy(s, "cia", len);
s[1] = 'd';
s[2] = 's';
s[3] = 'x';
s[4] = '\0';
}
else
{
s[0] = 'c';
s[1] = 'i';
s[2] = 'a';
s[3] = '\0';
}
return true;
#else #else
return false; return 0;
#endif #endif
#endif #endif
} }
@ -401,7 +385,7 @@ void frontend_driver_init_first(void *args)
frontend_st->current_frontend_ctx = (frontend_ctx_driver_t*) frontend_st->current_frontend_ctx = (frontend_ctx_driver_t*)
frontend_ctx_init_first(); frontend_ctx_init_first();
if ( frontend_st->current_frontend_ctx if ( frontend_st->current_frontend_ctx
&& frontend_st->current_frontend_ctx->init) && frontend_st->current_frontend_ctx->init)
frontend_st->current_frontend_ctx->init(args); frontend_st->current_frontend_ctx->init(args);
} }
@ -586,15 +570,15 @@ void frontend_driver_attach_console(void)
{ {
/* TODO/FIXME - the frontend driver code is garbage and needs to be /* TODO/FIXME - the frontend driver code is garbage and needs to be
redesigned. Apparently frontend_driver_attach_console can be called redesigned. Apparently frontend_driver_attach_console can be called
BEFORE frontend_driver_init_first is called, hence why we need BEFORE frontend_driver_init_first is called, hence why we need
to resort to the check for non-NULL below. This is just awful, to resort to the check for non-NULL below. This is just awful,
BEFORE we make any frontend function call, we should be 100% BEFORE we make any frontend function call, we should be 100%
sure frontend_driver_init_first has already been called first. sure frontend_driver_init_first has already been called first.
For now, we do this hack, but this absolutely should be redesigned For now, we do this hack, but this absolutely should be redesigned
as soon as possible. as soon as possible.
*/ */
if ( frontend_driver_st.current_frontend_ctx if ( frontend_driver_st.current_frontend_ctx
&& frontend_driver_st.current_frontend_ctx->attach_console) && frontend_driver_st.current_frontend_ctx->attach_console)
frontend_driver_st.current_frontend_ctx->attach_console(); frontend_driver_st.current_frontend_ctx->attach_console();
} }

View File

@ -177,7 +177,7 @@ void frontend_driver_exitspawn(char *s, size_t len, char *args);
bool frontend_driver_has_fork(void); bool frontend_driver_has_fork(void);
bool frontend_driver_get_core_extension(char *s, size_t len); size_t frontend_driver_get_core_extension(char *s, size_t len);
bool frontend_driver_get_salamander_basename(char *s, size_t len); bool frontend_driver_get_salamander_basename(char *s, size_t len);

View File

@ -577,14 +577,15 @@ bool nested_list_item_get_address(nested_list_item_t *list_item,
/* Build address string */ /* Build address string */
for (i = id_list.size; i > 0; i--) for (i = id_list.size; i > 0; i--)
{ {
size_t _len;
const char *id = id_list.elems[i - 1].data; const char *id = id_list.elems[i - 1].data;
if (string_is_empty(id)) if (string_is_empty(id))
goto end; goto end;
strlcat(address, id, len); _len = strlcat(address, id, len);
if (i > 1) if (i > 1)
strlcat(address, delim, len); strlcpy(address + _len, delim, len - _len);
} }
success = true; success = true;

View File

@ -270,9 +270,9 @@ void string_list_join_concat(char *buffer, size_t size,
for (i = 0; i < list->size; i++) for (i = 0; i < list->size; i++)
{ {
strlcat(buffer, list->elems[i].data, size); size_t _len = strlcat(buffer, list->elems[i].data, size);
if ((i + 1) < list->size) if ((i + 1) < list->size)
strlcat(buffer, delim, size); strlcpy(buffer + _len, delim, size - _len);
} }
} }

View File

@ -499,9 +499,9 @@ static int filebrowser_parse(
allow_parent_directory = false; allow_parent_directory = false;
else else
{ {
fill_pathname_home_dir(dir, sizeof(dir)); size_t _len = fill_pathname_home_dir(dir, sizeof(dir));
if (string_ends_with(full_path, "/") && !string_ends_with(dir, "/")) if (string_ends_with(full_path, "/") && !string_ends_with(dir, "/"))
strlcat(dir, "/", sizeof(dir)); strlcpy(dir + _len, "/", sizeof(dir) - _len);
if (string_is_equal(dir, full_path)) if (string_is_equal(dir, full_path))
allow_parent_directory = false; allow_parent_directory = false;
} }
@ -12115,8 +12115,8 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type,
const struct retro_keybind *auto_bind = const struct retro_keybind *auto_bind =
(const struct retro_keybind*) (const struct retro_keybind*)
input_config_get_bind_auto(port, retro_id); input_config_get_bind_auto(port, retro_id);
size_t desc_len = input_config_get_bind_string(
input_config_get_bind_string(settings, descriptor, settings, descriptor,
keybind, auto_bind, sizeof(descriptor)); keybind, auto_bind, sizeof(descriptor));
if (!strstr(descriptor, "Auto")) if (!strstr(descriptor, "Auto"))
@ -12131,7 +12131,7 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type,
desc_label[++_len] = ' '; desc_label[++_len] = ' ';
desc_label[++_len] = '\0'; desc_label[++_len] = '\0';
strlcpy(desc_label + _len, descriptor, sizeof(desc_label) - _len); strlcpy(desc_label + _len, descriptor, sizeof(desc_label) - _len);
strlcpy(descriptor, desc_label, sizeof(descriptor)); desc_len = strlcpy(descriptor, desc_label, sizeof(descriptor));
} }
/* Add user index when display driver == rgui and sublabels /* Add user index when display driver == rgui and sublabels
@ -12140,8 +12140,8 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type,
&& (max_users > 1) && (max_users > 1)
&& !settings->bools.menu_show_sublabels) && !settings->bools.menu_show_sublabels)
{ {
size_t _len = strlcat(descriptor, " [", sizeof(descriptor)); desc_len += strlcpy(descriptor + desc_len, " [", sizeof(descriptor) - desc_len);
snprintf(descriptor + _len, sizeof(descriptor) - _len, "%s %u]", snprintf(descriptor + desc_len, sizeof(descriptor) - desc_len, "%s %u]",
msg_val_port, port + 1); msg_val_port, port + 1);
} }
@ -12168,8 +12168,8 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type,
const struct retro_keybind *auto_bind = const struct retro_keybind *auto_bind =
(const struct retro_keybind*) (const struct retro_keybind*)
input_config_get_bind_auto(port, retro_id); input_config_get_bind_auto(port, retro_id);
size_t desc_len = input_config_get_bind_string(
input_config_get_bind_string(settings, descriptor, settings, descriptor,
keybind, auto_bind, sizeof(descriptor)); keybind, auto_bind, sizeof(descriptor));
if (!strstr(descriptor, "Auto")) if (!strstr(descriptor, "Auto"))
@ -12184,7 +12184,7 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type,
desc_label[++_len] = ' '; desc_label[++_len] = ' ';
desc_label[++_len] = '\0'; desc_label[++_len] = '\0';
strlcpy(desc_label + _len, descriptor, sizeof(desc_label) - _len); strlcpy(desc_label + _len, descriptor, sizeof(desc_label) - _len);
strlcpy(descriptor, desc_label, sizeof(descriptor)); desc_len = strlcpy(descriptor, desc_label, sizeof(descriptor));
} }
/* Add user index when display driver == rgui and sublabels /* Add user index when display driver == rgui and sublabels
@ -12193,8 +12193,8 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type,
&& (max_users > 1) && (max_users > 1)
&& !settings->bools.menu_show_sublabels) && !settings->bools.menu_show_sublabels)
{ {
size_t _len = strlcat(descriptor, " [", sizeof(descriptor)); desc_len += strlcpy(descriptor + desc_len, " [", sizeof(descriptor) - desc_len);
snprintf(descriptor + _len, sizeof(descriptor) - _len, "%s %u]", snprintf(descriptor + desc_len, sizeof(descriptor) - desc_len, "%s %u]",
val_port, port + 1); val_port, port + 1);
} }
@ -15225,14 +15225,16 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type,
break; break;
case DISPLAYLIST_FILE_BROWSER_SELECT_SIDELOAD_CORE: case DISPLAYLIST_FILE_BROWSER_SELECT_SIDELOAD_CORE:
{ {
size_t _len;
char ext_names[32]; char ext_names[32];
info->type_default = FILE_TYPE_SIDELOAD_CORE; info->type_default = FILE_TYPE_SIDELOAD_CORE;
if (frontend_driver_get_core_extension(ext_names, sizeof(ext_names))) _len = frontend_driver_get_core_extension(ext_names, sizeof(ext_names));
if (_len > 0)
{ {
strlcat(ext_names, "|", sizeof(ext_names)); _len += strlcpy(ext_names + _len, "|", sizeof(ext_names) - _len);
strlcat(ext_names, FILE_PATH_CORE_BACKUP_EXTENSION_NO_DOT, sizeof(ext_names)); _len += strlcpy(ext_names + _len, FILE_PATH_CORE_BACKUP_EXTENSION_NO_DOT, sizeof(ext_names) - _len);
} }
else else
strlcpy(ext_names, FILE_PATH_CORE_BACKUP_EXTENSION_NO_DOT, sizeof(ext_names)); strlcpy(ext_names, FILE_PATH_CORE_BACKUP_EXTENSION_NO_DOT, sizeof(ext_names));