From 7317fa9ee90aafe5471088d176d4804373006fbb Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Mon, 23 Dec 2024 06:24:01 +0100 Subject: [PATCH] Cut down on some strlen calls where possible --- manual_content_scan.c | 11 +++++------ menu/cbs/menu_cbs_get_value.c | 14 ++++++++------ menu/cbs/menu_cbs_ok.c | 25 ++++++------------------- menu/cbs/menu_cbs_sublabel.c | 10 ++++++---- 4 files changed, 25 insertions(+), 35 deletions(-) diff --git a/manual_content_scan.c b/manual_content_scan.c index 712c4fd8e7..1f75205294 100644 --- a/manual_content_scan.c +++ b/manual_content_scan.c @@ -283,7 +283,7 @@ enum manual_content_scan_dat_file_path_status * Returns true if content directory is valid. */ bool manual_content_scan_set_menu_content_dir(const char *content_dir) { - size_t len; + size_t _len; const char *dir_name = NULL; /* Sanity check */ @@ -294,18 +294,17 @@ bool manual_content_scan_set_menu_content_dir(const char *content_dir) goto error; /* Copy directory path to settings struct */ - strlcpy( + _len = strlcpy( scan_settings.content_dir, content_dir, sizeof(scan_settings.content_dir)); /* Remove trailing slash, if required */ - len = strlen(scan_settings.content_dir); - if (len <= 0) + if (_len <= 0) goto error; - if (scan_settings.content_dir[len - 1] == PATH_DEFAULT_SLASH_C()) - scan_settings.content_dir[len - 1] = '\0'; + if (scan_settings.content_dir[_len - 1] == PATH_DEFAULT_SLASH_C()) + scan_settings.content_dir[_len - 1] = '\0'; /* Handle case where path was a single slash... */ if (string_is_empty(scan_settings.content_dir)) diff --git a/menu/cbs/menu_cbs_get_value.c b/menu/cbs/menu_cbs_get_value.c index e5cbfdb9f2..222c564027 100644 --- a/menu/cbs/menu_cbs_get_value.c +++ b/menu/cbs/menu_cbs_get_value.c @@ -756,6 +756,7 @@ static void menu_action_setting_disp_set_label_core_lock( const char *path, char *s2, size_t len2) { + size_t _len; core_info_t *core_info = NULL; const char *alt = list->list[i].alt ? list->list[i].alt @@ -770,11 +771,11 @@ static void menu_action_setting_disp_set_label_core_lock( * don't want to perform disk access every frame */ if ( core_info_find(path, &core_info) && core_info->is_locked) - strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_ON), len); + _len = strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_ON), len); else - strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_OFF), len); + _len = strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_OFF), len); - *w = (unsigned)strlen(s); + *w = (unsigned)_len; } static void menu_action_setting_disp_set_label_core_set_standalone_exempt( @@ -785,6 +786,7 @@ static void menu_action_setting_disp_set_label_core_set_standalone_exempt( const char *path, char *s2, size_t len2) { + size_t _len; core_info_t *core_info = NULL; const char *alt = list->list[i].alt ? list->list[i].alt @@ -802,11 +804,11 @@ static void menu_action_setting_disp_set_label_core_set_standalone_exempt( if ( core_info_find(path, &core_info) && core_info->supports_no_game && core_info->is_standalone_exempt) - strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_ON), len); + _len = strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_ON), len); else - strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_OFF), len); + _len = strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_OFF), len); - *w = (unsigned)strlen(s); + *w = (unsigned)_len; } static void menu_action_setting_disp_set_label_input_desc( diff --git a/menu/cbs/menu_cbs_ok.c b/menu/cbs/menu_cbs_ok.c index 706b1afc7e..8dcbd6859a 100644 --- a/menu/cbs/menu_cbs_ok.c +++ b/menu/cbs/menu_cbs_ok.c @@ -3683,6 +3683,7 @@ static int generic_action_ok_remap_file_operation(const char *path, { #ifdef HAVE_CONFIGFILE char remap_file_path[PATH_MAX_LENGTH]; + char remap_path[PATH_MAX_LENGTH]; struct menu_state *menu_st = menu_state_get_ptr(); rarch_system_info_t *sys_info = &runloop_state_get_ptr()->system; const char *core_name = sys_info ? sys_info->info.library_name : NULL; @@ -3693,9 +3694,7 @@ static int generic_action_ok_remap_file_operation(const char *path, unsigned joypad_port = settings->uints.input_joypad_index[0]; const char *input_device_name = input_config_get_device_display_name(joypad_port); const char *input_device_dir = NULL; - char *remap_path = NULL; bool sort_remaps_by_controller = settings->bools.input_remap_sort_by_controller_enable; - size_t remap_path_total_len = 0; size_t _len = 0; remap_file_path[0] = '\0'; @@ -3713,27 +3712,17 @@ static int generic_action_ok_remap_file_operation(const char *path, input_device_dir = sanitize_path_part(input_device_name, strlen(input_device_name)); /* Allocate memory for the new path */ - remap_path_total_len = strlen(core_name) + strlen(input_device_dir) + 2; - remap_path = (char *)malloc(remap_path_total_len); - /* Build the new path with the controller name */ - _len = strlcpy(remap_path, core_name, remap_path_total_len); - _len += strlcpy(remap_path + _len, PATH_DEFAULT_SLASH(), remap_path_total_len - _len); - _len += strlcpy(remap_path + _len, input_device_dir, remap_path_total_len - _len); + _len = strlcpy(remap_path, core_name, sizeof(remap_path)); + _len += strlcpy(remap_path + _len, PATH_DEFAULT_SLASH(), sizeof(remap_path) - _len); + _len += strlcpy(remap_path + _len, input_device_dir, sizeof(remap_path) - _len); /* Deallocate as we no longer this */ free((char*)input_device_dir); input_device_dir = NULL; } - else - { - /* Allocate memory for the new path */ - remap_path_total_len = strlen(core_name) + 1; - remap_path = (char *)malloc(remap_path_total_len); - - /* We're not using controller path, just use core name */ - strlcpy(remap_path, core_name, remap_path_total_len); - } + else /* We're not using controller path, just use core name */ + strlcpy(remap_path, core_name, sizeof(remap_path)); switch (action_type) { @@ -3777,8 +3766,6 @@ static int generic_action_ok_remap_file_operation(const char *path, break; } - free(remap_path); - if (action_type < ACTION_OK_REMAP_FILE_REMOVE_CORE) { if ( !string_is_empty(remap_file_path) diff --git a/menu/cbs/menu_cbs_sublabel.c b/menu/cbs/menu_cbs_sublabel.c index 356bbeee5d..d4cf8f6c7e 100644 --- a/menu/cbs/menu_cbs_sublabel.c +++ b/menu/cbs/menu_cbs_sublabel.c @@ -5744,14 +5744,16 @@ int menu_cbs_init_bind_sublabel(menu_file_list_cbs_t *cbs, const char* idx_placeholder = "%u"; for (i = 0; i < ARRAY_SIZE(info_list); i++) { - int idxpos = string_find_index_substring_string(msg_hash_to_str((enum msg_hash_enums)info_list[i].label_idx), idx_placeholder); + int idxpos = string_find_index_substring_string(msg_hash_to_str((enum msg_hash_enums)info_list[i].label_idx), idx_placeholder); + size_t _lbl_len = strlen(msg_hash_to_str((enum msg_hash_enums)info_list[i].label_idx)); if ( (idxpos > 0) && string_starts_with_size(label, msg_hash_to_str((enum msg_hash_enums)info_list[i].label_idx), idxpos) - && (((size_t)idxpos == strlen(msg_hash_to_str((enum msg_hash_enums)info_list[i].label_idx)) - 2) - || ((size_t)idxpos < strlen(msg_hash_to_str((enum msg_hash_enums)info_list[i].label_idx)) - 2 && string_ends_with_size(label, + && (((size_t)idxpos == _lbl_len - 2) + || ((size_t)idxpos < _lbl_len - 2 + && string_ends_with_size(label, msg_hash_to_str((enum msg_hash_enums)info_list[i].label_idx) + idxpos + 2, lbl_len, - strlen(msg_hash_to_str((enum msg_hash_enums)info_list[i].label_idx))-idxpos-2)))) + _lbl_len - idxpos - 2)))) { BIND_ACTION_SUBLABEL(cbs, info_list[i].cb); return 0;