- (database_info.c) Optimizations for database_info_build_query_enum() -
 it would call strlcat at the very least 3 times, normally more than that.
 Every strlcat call is at least one or more strlens. Reduce it all down
 to manual string concatenation and one strlcat
- (database_info_build_query_enum) - always NULL terminates string so no need for NULL termination done by callee
* (menu_displaylist) use smaller strings for basename paths, remove one unnecessary string
in menu_displaylist.c function
- (disk_index_file.c) Avoid another trivial string concatenation by doing
it manually after fill_pathname_join_special
- (fill_pathname_join_delim) since fill_pathname_join_delim() now has a return value, use it for 
string concatenation
- (platform_unix) - Use BUFSIZ as size for a buffer in system_property_get
- some other trivial strlcat usage replacements
This commit is contained in:
LibretroAdmin 2022-08-06 18:04:40 +02:00 committed by GitHub
parent 72b4cf914e
commit 94ed25be56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 442 additions and 100 deletions

View File

@ -3108,14 +3108,12 @@ static config_file_t *open_default_config_file(void)
fill_pathname_join_special(conf_path, application_data,
FILE_PATH_MAIN_CONFIG, sizeof(conf_path));
conf = config_file_new_from_path_to_string(conf_path);
if (!conf)
if (!(conf = config_file_new_from_path_to_string(conf_path)))
{
bool saved = false;
conf = config_file_new_alloc();
if (conf)
if ((conf = config_file_new_alloc()))
{
config_set_string(conf, "config_save_on_exit", "true");
saved = config_file_write(conf, conf_path, true);
@ -4227,9 +4225,13 @@ static void save_keybind_joykey(config_file_t *conf,
const struct retro_keybind *bind, bool save_empty)
{
char key[64];
fill_pathname_join_delim(key, prefix,
size_t len = fill_pathname_join_delim(key, prefix,
base, '_', sizeof(key));
strlcat(key, "_btn", sizeof(key));
key[len ] = '_';
key[len+1] = 'b';
key[len+2] = 't';
key[len+3] = 'n';
key[len+4] = '\0';
if (bind->joykey == NO_BTN)
{
@ -4248,8 +4250,13 @@ static void save_keybind_axis(config_file_t *conf,
const struct retro_keybind *bind, bool save_empty)
{
char key[64];
fill_pathname_join_delim(key, prefix, base, '_', sizeof(key));
strlcat(key, "_axis", sizeof(key));
size_t len = fill_pathname_join_delim(key, prefix, base, '_', sizeof(key));
key[len ] = '_';
key[len+1] = 'a';
key[len+2] = 'x';
key[len+3] = 'i';
key[len+4] = 's';
key[len+4] = '\0';
if (bind->joyaxis == AXIS_NONE)
{
@ -4278,9 +4285,14 @@ static void save_keybind_mbutton(config_file_t *conf,
const struct retro_keybind *bind, bool save_empty)
{
char key[64];
fill_pathname_join_delim(key, prefix,
size_t len = fill_pathname_join_delim(key, prefix,
base, '_', sizeof(key));
strlcat(key, "_mbtn", sizeof(key));
key[len ] = '_';
key[len+1] = 'm';
key[len+2] = 'b';
key[len+3] = 't';
key[len+4] = 'n';
key[len+4] = '\0';
switch (bind->mbutton)
{

View File

@ -34,94 +34,418 @@ int database_info_build_query_enum(char *s, size_t len,
enum database_query_type type,
const char *path)
{
bool add_quotes = true;
bool add_glob = false;
s[0] = '{';
s[1] = '\'';
s[2] = '\0';
size_t pos = 0;
switch (type)
{
case DATABASE_QUERY_ENTRY:
strlcat(s, "name", len);
s[0] = '{';
s[1] = '\'';
s[2] = 'n';
s[3] = 'a';
s[4] = 'm';
s[5] = 'e';
s[6] = '\'';
s[7] = ':';
s[8] = '"';
s[9] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '"';
s[pos+1] = '}';
s[pos+2] = '\0';
break;
case DATABASE_QUERY_ENTRY_PUBLISHER:
strlcat(s, "publisher", len);
s[0] = '{';
s[1] = '\'';
s[2] = 'p';
s[3] = 'u';
s[4] = 'b';
s[5] = 'l';
s[6] = 'i';
s[7] = 's';
s[8] = 'h';
s[9] = 'e';
s[10] = 'r';
s[11] = '\'';
s[12] = ':';
s[13] = '"';
s[14] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '"';
s[pos+1] = '}';
s[pos+2] = '\0';
break;
case DATABASE_QUERY_ENTRY_DEVELOPER:
strlcat(s, "developer", len);
add_glob = true;
add_quotes = false;
s[0] = '{';
s[1] = '\'';
s[2] = 'd';
s[3] = 'e';
s[4] = 'v';
s[5] = 'e';
s[6] = 'l';
s[7] = 'o';
s[8] = 'p';
s[9] = 'e';
s[10] = 'r';
s[11] = '\'';
s[12] = ':';
s[13] = 'g';
s[14] = 'l';
s[15] = 'o';
s[16] = 'b';
s[17] = '(';
s[18] = '\'';
s[19] = '*';
s[20] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '*';
s[pos+1] = '\'';
s[pos+2] = ')';
s[pos+3] = '}';
s[pos+4] = '\0';
break;
case DATABASE_QUERY_ENTRY_ORIGIN:
strlcat(s, "origin", len);
s[0] = '{';
s[1] = '\'';
s[2] = 'o';
s[3] = 'r';
s[4] = 'i';
s[5] = 'g';
s[6] = 'i';
s[7] = 'n';
s[8] = '\'';
s[9] = ':';
s[10] = '"';
s[11] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '"';
s[pos+1] = '}';
s[pos+2] = '\0';
break;
case DATABASE_QUERY_ENTRY_FRANCHISE:
strlcat(s, "franchise", len);
s[0] = '{';
s[1] = '\'';
s[2] = 'f';
s[3] = 'r';
s[4] = 'a';
s[5] = 'n';
s[6] = 'c';
s[7] = 'h';
s[8] = 'i';
s[9] = 's';
s[10] = 'e';
s[11] = '\'';
s[12] = ':';
s[13] = '"';
s[14] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '"';
s[pos+1] = '}';
s[pos+2] = '\0';
break;
case DATABASE_QUERY_ENTRY_RATING:
strlcat(s, "esrb_rating", len);
s[0] = '{';
s[1] = '\'';
s[2] = 'e';
s[3] = 's';
s[4] = 'r';
s[5] = 'b';
s[6] = '_';
s[7] = 'r';
s[8] = 'a';
s[9] = 't';
s[10] = 'i';
s[11] = 'n';
s[12] = 'g';
s[13] = '\'';
s[14] = ':';
s[15] = '"';
pos = strlcat(s, path, len);
s[pos ] = '"';
s[pos+1] = '}';
s[pos+2] = '\0';
break;
case DATABASE_QUERY_ENTRY_BBFC_RATING:
strlcat(s, "bbfc_rating", len);
s[0] = '{';
s[1] = '\'';
s[2] = 'b';
s[3] = 'b';
s[4] = 'f';
s[5] = 'c';
s[6] = '_';
s[7] = 'r';
s[8] = 'a';
s[9] = 't';
s[10] = 'i';
s[11] = 'n';
s[12] = 'g';
s[13] = '\'';
s[14] = ':';
s[15] = '"';
s[16] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '"';
s[pos+1] = '}';
s[pos+2] = '\0';
break;
case DATABASE_QUERY_ENTRY_ELSPA_RATING:
strlcat(s, "elspa_rating", len);
s[0] = '{';
s[1] = '\'';
s[2] = 'e';
s[3] = 'l';
s[4] = 's';
s[5] = 'p';
s[6] = 'a';
s[7] = '_';
s[8] = 'r';
s[9] = 'a';
s[10] = 't';
s[11] = 'i';
s[12] = 'n';
s[13] = 'g';
s[14] = '\'';
s[15] = ':';
s[16] = '"';
s[17] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '"';
s[pos+1] = '}';
s[pos+2] = '\0';
break;
case DATABASE_QUERY_ENTRY_ESRB_RATING:
strlcat(s, "esrb_rating", len);
s[0] = '{';
s[1] = '\'';
s[2] = 'e';
s[3] = 's';
s[4] = 'r';
s[5] = 'b';
s[6] = '_';
s[7] = 'r';
s[8] = 'a';
s[9 ] = 't';
s[10] = 'i';
s[11] = 'n';
s[12] = 'g';
s[13] = '\'';
s[14] = ':';
s[15] = '"';
s[16] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '"';
s[pos+1] = '}';
s[pos+2] = '\0';
break;
case DATABASE_QUERY_ENTRY_PEGI_RATING:
strlcat(s, "pegi_rating", len);
s[0] = '{';
s[1] = '\'';
s[2] = 'p';
s[3] = 'e';
s[4] = 'g';
s[5] = 'i';
s[6] = '_';
s[7] = 'r';
s[8] = 'a';
s[9] = 't';
s[10] = 'i';
s[11] = 'n';
s[12] = 'g';
s[13] = '\'';
s[14] = ':';
s[15] = '"';
s[16] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '"';
s[pos+1] = '}';
s[pos+2] = '\0';
break;
case DATABASE_QUERY_ENTRY_CERO_RATING:
strlcat(s, "cero_rating", len);
s[0] = '{';
s[1] = '\'';
s[2] = 'c';
s[3] = 'e';
s[4] = 'r';
s[5] = 'o';
s[6] = '_';
s[7] = 'r';
s[8] = 'a';
s[9] = 't';
s[10] = 'i';
s[11] = 'n';
s[12] = 'g';
s[13] = '\'';
s[14] = ':';
s[15] = '"';
s[16] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '"';
s[pos+1] = '}';
s[pos+2] = '\0';
break;
case DATABASE_QUERY_ENTRY_ENHANCEMENT_HW:
strlcat(s, "enhancement_hw", len);
s[0] = '{';
s[1] = '\'';
s[2] = 'e';
s[3] = 'n';
s[4] = 'h';
s[5] = 'a';
s[6] = 'n';
s[7] = 'c';
s[8] = 'e';
s[9] = 'm';
s[10] = 'e';
s[11] = 'n';
s[12] = 't';
s[13] = '_';
s[14] = 'h';
s[15] = 'w';
s[16] = '\'';
s[17] = ':';
s[18] = '"';
s[19] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '"';
s[pos+1] = '}';
s[pos+2] = '\0';
break;
case DATABASE_QUERY_ENTRY_EDGE_MAGAZINE_RATING:
strlcat(s, "edge_rating", len);
add_quotes = false;
s[0] = '{';
s[1] = '\'';
s[2] = 'e';
s[3] = 'd';
s[4] = 'g';
s[5] = 'e';
s[6] = '_';
s[7] = 'r';
s[8] = 'a';
s[9] = 't';
s[10] = 'i';
s[11] = 'n';
s[12] = 'g';
s[13] = '\'';
s[14] = ':';
s[15] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '}';
s[pos+1] = '\0';
break;
case DATABASE_QUERY_ENTRY_EDGE_MAGAZINE_ISSUE:
strlcat(s, "edge_issue", len);
add_quotes = false;
s[0] = '{';
s[1] = '\'';
s[2] = 'e';
s[3] = 'd';
s[4] = 'g';
s[5] = 'e';
s[6] = '_';
s[7] = 'i';
s[8] = 's';
s[9] = 's';
s[10] = 'u';
s[11] = 'e';
s[12] = '\'';
s[13] = ':';
s[14] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '}';
s[pos+1] = '\0';
break;
case DATABASE_QUERY_ENTRY_FAMITSU_MAGAZINE_RATING:
strlcat(s, "famitsu_rating", len);
add_quotes = false;
s[0] = '{';
s[1] = '\'';
s[2] = 'f';
s[3] = 'a';
s[4] = 'm';
s[5] = 'i';
s[6] = 't';
s[7] = 's';
s[8] = 'u';
s[9] = '_';
s[10] = 'r';
s[11] = 'a';
s[12] = 't';
s[13] = 'i';
s[14] = 'n';
s[15] = 'g';
s[16] = '\'';
s[17] = ':';
s[18] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '}';
s[pos+1] = '\0';
break;
case DATABASE_QUERY_ENTRY_RELEASEDATE_MONTH:
strlcat(s, "releasemonth", len);
add_quotes = false;
s[0] = '{';
s[1] = '\'';
s[2] = 'r';
s[3] = 'e';
s[4] = 'l';
s[5] = 'e';
s[6] = 'a';
s[7] = 's';
s[8] = 'e';
s[9] = 'm';
s[10] = 'o';
s[11] = 'n';
s[12] = 't';
s[13] = 'h';
s[14] = '\'';
s[15] = ':';
s[16] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '}';
s[pos+1] = '\0';
break;
case DATABASE_QUERY_ENTRY_RELEASEDATE_YEAR:
strlcat(s, "releaseyear", len);
add_quotes = false;
s[0] = '{';
s[1] = '\'';
s[2] = 'r';
s[3] = 'e';
s[4] = 'l';
s[5] = 'e';
s[6] = 'a';
s[7] = 's';
s[8] = 'e';
s[9] = 'y';
s[10] = 'e';
s[11] = 'a';
s[12] = 'r';
s[13] = '\'';
s[14] = ':';
s[15] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '}';
s[pos+1] = '\0';
break;
case DATABASE_QUERY_ENTRY_MAX_USERS:
strlcat(s, "users", len);
add_quotes = false;
s[0] = '{';
s[1] = '\'';
s[2] = 'u';
s[3] = 's';
s[4] = 'e';
s[5] = 'r';
s[6] = 's';
s[7] = '\'';
s[8] = ':';
s[9] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '}';
s[pos+1] = '\0';
break;
case DATABASE_QUERY_NONE:
s[0] = '{';
s[1] = '\'';
s[2] = '\'';
s[3] = ':';
s[4] = '\'';
s[5] = ':';
s[6] = '"';
s[7] = '\0';
pos = strlcat(s, path, len);
s[pos ] = '"';
s[pos+1] = '}';
s[pos+2] = '\0';
break;
}
strlcat(s, "':", len);
if (add_glob)
strlcat(s, "glob('*", len);
if (add_quotes)
strlcat(s, "\"", len);
strlcat(s, path, len);
if (add_glob)
strlcat(s, "*')", len);
if (add_quotes)
strlcat(s, "\"", len);
strlcat(s, "}", len);
return 0;
}

View File

@ -217,8 +217,9 @@ bool disk_index_file_init(
const char *content_path,
const char *dir_savefile)
{
size_t len;
const char *content_file = NULL;
char content_name[PATH_MAX_LENGTH];
char content_name[256];
char disk_index_file_dir[PATH_MAX_LENGTH];
char disk_index_file_path[PATH_MAX_LENGTH];
@ -267,13 +268,15 @@ bool disk_index_file_init(
}
/* > Generate final path */
fill_pathname_join_special(
len = fill_pathname_join_special(
disk_index_file_path, disk_index_file_dir,
content_name, sizeof(disk_index_file_path));
strlcat(
disk_index_file_path,
FILE_PATH_DISK_CONTROL_INDEX_EXTENSION,
sizeof(disk_index_file_path));
disk_index_file_path[len ] = '.';
disk_index_file_path[len+1] = 'l';
disk_index_file_path[len+2] = 'd';
disk_index_file_path[len+3] = 'c';
disk_index_file_path[len+4] = 'i';
disk_index_file_path[len+5] = '\0';
if (string_is_empty(disk_index_file_path))
goto error;

View File

@ -161,9 +161,9 @@ int system_property_get(const char *command,
const char *args, char *value)
{
FILE *pipe;
char buffer[BUFSIZ];
char cmd[NAME_MAX_LENGTH];
int length = 0;
char buffer[PATH_MAX_LENGTH] = {0};
char *curpos = NULL;
size_t buf_pos = strlcpy(cmd, command, sizeof(cmd));
@ -172,16 +172,17 @@ int system_property_get(const char *command,
buf_pos = strlcat(cmd, args, sizeof(cmd));
pipe = popen(cmd, "r");
if (!pipe)
goto error;
if (!(pipe = popen(cmd, "r")))
{
RARCH_ERR("Could not create pipe.\n");
return 0;
}
curpos = value;
while (!feof(pipe))
{
if (fgets(buffer, 128, pipe))
if (fgets(buffer, sizeof(buffer), pipe))
{
int curlen = strlen(buffer);
@ -197,10 +198,6 @@ int system_property_get(const char *command,
pclose(pipe);
return length;
error:
RARCH_ERR("Could not create pipe.\n");
return 0;
}
#ifdef ANDROID

View File

@ -274,14 +274,10 @@ static bool android_gfx_ctx_get_metrics(void *data,
if (dpi == -1)
{
char density[PROP_VALUE_MAX];
density[0] = '\0';
android_dpi_get_density(density, sizeof(density));
if (string_is_empty(density))
goto dpi_fallback;
dpi = atoi(density);
if (dpi <= 0)
if ((dpi = atoi(density)) <= 0)
goto dpi_fallback;
}
*value = (float)dpi;

View File

@ -226,14 +226,10 @@ static bool android_gfx_ctx_vk_get_metrics(void *data,
if (dpi == -1)
{
char density[PROP_VALUE_MAX];
density[0] = '\0';
android_dpi_get_density(density, sizeof(density));
if (string_is_empty(density))
goto dpi_fallback;
dpi = atoi(density);
if (dpi <= 0)
if ((dpi = atoi(density)) <= 0)
goto dpi_fallback;
}
*value = (float)dpi;

View File

@ -2209,9 +2209,15 @@ void input_config_get_bind_string_joykey(
!string_is_empty(bind->joykey_label)
&& input_descriptor_label_show)
{
fill_pathname_join_delim(buf, prefix,
size_t len = fill_pathname_join_delim(buf, prefix,
bind->joykey_label, ' ', size);
strlcat(buf, " (hat)", size);
buf[len ] = ' ';
buf[len+1] = '(';
buf[len+2] = 'h';
buf[len+3] = 'a';
buf[len+4] = 't';
buf[len+5] = ')';
buf[len+6] = '\0';
}
else
{
@ -2245,9 +2251,15 @@ void input_config_get_bind_string_joykey(
!string_is_empty(bind->joykey_label)
&& input_descriptor_label_show)
{
fill_pathname_join_delim(buf, prefix,
size_t len = fill_pathname_join_delim(buf, prefix,
bind->joykey_label, ' ', size);
strlcat(buf, " (btn)", size);
buf[len ] = ' ';
buf[len+1] = '(';
buf[len+2] = 'b';
buf[len+3] = 't';
buf[len+4] = 'n';
buf[len+5] = ')';
buf[len+6] = '\0';
}
else
snprintf(buf, size, "%s%u (%s)", prefix, (unsigned)bind->joykey,
@ -2264,9 +2276,16 @@ void input_config_get_bind_string_joyaxis(
!string_is_empty(bind->joyaxis_label)
&& input_descriptor_label_show)
{
fill_pathname_join_delim(buf, prefix,
size_t len = fill_pathname_join_delim(buf, prefix,
bind->joyaxis_label, ' ', size);
strlcat(buf, " (axis)", size);
buf[len ] = ' ';
buf[len+1] = '(';
buf[len+2] = 'a';
buf[len+3] = 'x';
buf[len+4] = 'i';
buf[len+5] = 's';
buf[len+6] = ')';
buf[len+7] = '\0';
}
else
{

View File

@ -342,7 +342,7 @@ static int deferred_push_cursor_manager_list_deferred(
static int deferred_push_cursor_manager_list_generic(
menu_displaylist_info_t *info, enum database_query_type type)
{
char query[PATH_MAX_LENGTH];
char query[256];
int ret = -1;
const char *path = info->path;
struct string_list str_list = {0};
@ -354,8 +354,6 @@ static int deferred_push_cursor_manager_list_generic(
string_list_initialize(&str_list);
string_split_noalloc(&str_list, path, "|");
query[0] = '\0';
database_info_build_query_enum(query, sizeof(query), type,
str_list.elems[0].data);

View File

@ -2420,9 +2420,9 @@ static int menu_displaylist_parse_database_entry(menu_handle_t *menu,
menu_displaylist_info_t *info)
{
unsigned i, j, k;
char query[256];
char path_playlist[PATH_MAX_LENGTH];
char path_base[PATH_MAX_LENGTH];
char query[PATH_MAX_LENGTH];
char path_base[NAME_MAX_LENGTH];
playlist_config_t playlist_config;
playlist_t *playlist = NULL;
database_info_list_t *db_info = NULL;
@ -2435,8 +2435,6 @@ static int menu_displaylist_parse_database_entry(menu_handle_t *menu,
playlist_config.fuzzy_archive_match = settings->bools.playlist_fuzzy_archive_match;
playlist_config_set_base_content_directory(&playlist_config, settings->bools.playlist_portable_paths ? settings->paths.directory_menu_content : NULL);
query[0] = '\0';
database_info_build_query_enum(query, sizeof(query),
DATABASE_QUERY_ENTRY, info->path_b);
@ -2462,12 +2460,11 @@ static int menu_displaylist_parse_database_entry(menu_handle_t *menu,
for (i = 0; i < db_info->count; i++)
{
char tmp[PATH_MAX_LENGTH];
char thumbnail_content[PATH_MAX_LENGTH];
char crc_str[20];
char tmp[PATH_MAX_LENGTH];
database_info_t *db_info_entry = &db_info->list[i];
crc_str[0] = tmp[0] = thumbnail_content[0] = '\0';
crc_str[0] = tmp[0] = '\0';
snprintf(crc_str, sizeof(crc_str), "%08lX", (unsigned long)db_info_entry->crc32);

View File

@ -5494,11 +5494,11 @@ bool runloop_event_init_core(
sizeof(video_st->title_buf));
video_st->title_buf[len ] = ' ';
video_st->title_buf[len+1] = '\0';
strlcat(video_st->title_buf,
len = strlcat(video_st->title_buf,
sys_info->info.library_name,
sizeof(video_st->title_buf));
strlcat(video_st->title_buf, " ",
sizeof(video_st->title_buf));
video_st->title_buf[len ] = ' ';
video_st->title_buf[len+1] = '\0';
strlcat(video_st->title_buf,
sys_info->info.library_version,
sizeof(video_st->title_buf));