From e865ea83bd150c35f8d713e48e54a4ad81c0326f Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Fri, 22 Jul 2022 22:23:54 +0200 Subject: [PATCH] * Don't NULL terminate string if we pass it to strlcpy and/or a file_path function that calls strlcpy under the hood * Replace some snprintf calls to strlcpy/strlcat --- command.c | 27 +++++------------- core_backup.c | 10 +++---- core_info.c | 78 +++++++++++++++++---------------------------------- 3 files changed, 36 insertions(+), 79 deletions(-) diff --git a/command.c b/command.c index b514e9fd31..d1070fb1f3 100644 --- a/command.c +++ b/command.c @@ -191,9 +191,9 @@ static void command_network_poll(command_t *handle) { netcmd->cmd_source_len = sizeof(netcmd->cmd_source); - ret = recvfrom(netcmd->net_fd, buf, sizeof(buf) - 1, 0, - (struct sockaddr*)&netcmd->cmd_source, &netcmd->cmd_source_len); - if (ret <= 0) + if ((ret = recvfrom(netcmd->net_fd, buf, sizeof(buf) - 1, 0, + (struct sockaddr*)&netcmd->cmd_source, + &netcmd->cmd_source_len)) <= 0) return; buf[ret] = '\0'; @@ -1101,8 +1101,6 @@ bool command_event_save_auto_state( return false; #endif - savestate_name_auto[0] = '\0'; - strlcpy(savestate_name_auto, runloop_st->name.savestate, sizeof(savestate_name_auto)); @@ -1172,8 +1170,9 @@ bool command_event_load_entry_state(void) entry_state_path[0] = '\0'; - if (!retroarch_get_entry_state_path(entry_state_path, sizeof(entry_state_path), - runloop_st->entry_state_slot)) + if (!retroarch_get_entry_state_path( + entry_state_path, sizeof(entry_state_path), + runloop_st->entry_state_slot)) return false; entry_path_stats = path_stat(entry_state_path); @@ -1212,8 +1211,6 @@ void command_event_load_auto_state(void) return; #endif - savestate_name_auto[0] = '\0'; - strlcpy(savestate_name_auto, runloop_st->name.savestate, sizeof(savestate_name_auto)); @@ -1253,8 +1250,6 @@ void command_event_set_savestate_auto_index(settings_t *settings) if (!savestate_auto_index) return; - state_dir[0] = state_base[0] = '\0'; - /* Find the file in the same directory as runloop_st->savestate_name * with the largest numeral suffix. * @@ -1317,9 +1312,6 @@ void command_event_set_savestate_garbage_collect( unsigned min_idx = UINT_MAX; const char *oldest_save = NULL; - state_dir[0] = '\0'; - state_base[0] = '\0'; - /* Similar to command_event_set_savestate_auto_index(), * this will find the lowest numbered save-state */ fill_pathname_basedir(state_dir, runloop_st->name.savestate, @@ -1342,8 +1334,6 @@ void command_event_set_savestate_garbage_collect( const char *end = NULL; const char *dir_elem = dir_list->elems[i].data; - elem_base[0] = '\0'; - if (string_is_empty(dir_elem)) continue; @@ -1429,7 +1419,6 @@ bool command_event_save_core_config( runloop_state_t *runloop_st = runloop_state_get_ptr(); msg[0] = '\0'; - config_dir[0] = '\0'; if (!string_is_empty(dir_menu_config)) strlcpy(config_dir, dir_menu_config, sizeof(config_dir)); @@ -1519,8 +1508,7 @@ void command_event_save_current_config(enum override_type type) if (path_is_empty(RARCH_PATH_CONFIG)) { char msg[128]; - msg[0] = '\0'; - strcpy_literal(msg, "[Config]: Config directory not set, cannot save configuration."); + strlcpy(msg, "[Config]: Config directory not set, cannot save configuration.", sizeof(msg)); runloop_msg_queue_push(msg, 1, 180, true, NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO); } else @@ -1537,7 +1525,6 @@ void command_event_save_current_config(enum override_type type) case OVERRIDE_CONTENT_DIR: { char msg[128]; - msg[0] = '\0'; if (config_save_overrides(type, &runloop_st->system)) { strlcpy(msg, msg_hash_to_str(MSG_OVERRIDES_SAVED_SUCCESSFULLY), sizeof(msg)); diff --git a/core_backup.c b/core_backup.c index dcd34a7a89..7f083fdc69 100644 --- a/core_backup.c +++ b/core_backup.c @@ -55,9 +55,6 @@ static bool core_backup_get_backup_dir( char core_file_id[PATH_MAX_LENGTH]; char tmp[PATH_MAX_LENGTH]; - core_file_id[0] = '\0'; - tmp[0] = '\0'; - /* Extract core file 'ID' (name without extension + suffix) * from core path */ if (string_is_empty(dir_libretro) || @@ -113,8 +110,10 @@ static bool core_backup_get_backup_dir( /* Generates a timestamped core backup file path from * the specified core path. Returns true if successful */ bool core_backup_get_backup_path( - const char *core_path, uint32_t crc, enum core_backup_mode backup_mode, - const char *dir_core_assets, char *backup_path, size_t len) + const char *core_path, uint32_t crc, + enum core_backup_mode backup_mode, + const char *dir_core_assets, + char *backup_path, size_t len) { time_t current_time; struct tm time_info; @@ -123,7 +122,6 @@ bool core_backup_get_backup_path( char backup_dir[PATH_MAX_LENGTH]; char backup_filename[PATH_MAX_LENGTH]; - core_dir[0] = '\0'; backup_dir[0] = '\0'; backup_filename[0] = '\0'; diff --git a/core_info.c b/core_info.c index f3a4153aa1..3c1457e04e 100644 --- a/core_info.c +++ b/core_info.c @@ -705,8 +705,6 @@ static core_info_cache_list_t *core_info_cache_read(const char *info_dir) /* Check whether a 'force refresh' file * is present */ - file_path[0] = '\0'; - if (string_is_empty(info_dir)) strlcpy(file_path, FILE_PATH_CORE_INFO_CACHE_REFRESH, sizeof(file_path)); @@ -719,8 +717,6 @@ static core_info_cache_list_t *core_info_cache_read(const char *info_dir) return core_info_cache_list_new(); /* Open info cache file */ - file_path[0] = '\0'; - if (string_is_empty(info_dir)) strlcpy(file_path, FILE_PATH_CORE_INFO_CACHE, sizeof(file_path)); else @@ -740,8 +736,7 @@ static core_info_cache_list_t *core_info_cache_read(const char *info_dir) return core_info_cache_list_new(); /* Parse info cache file */ - parser = rjson_open_stream(file); - if (!parser) + if (!(parser = rjson_open_stream(file))) { RARCH_ERR("[Core Info] Failed to create JSON parser\n"); goto end; @@ -826,8 +821,6 @@ static bool core_info_cache_write(core_info_cache_list_t *list, const char *info char file_path[PATH_MAX_LENGTH]; size_t i, j; - file_path[0] = '\0'; - if (!list) return false; @@ -854,8 +847,7 @@ static bool core_info_cache_write(core_info_cache_list_t *list, const char *info } /* Write info cache */ - writer = rjsonwriter_open_stream(file); - if (!writer) + if (!(writer = rjsonwriter_open_stream(file))) { RARCH_ERR("[Core Info] Failed to create JSON writer\n"); goto end; @@ -1166,8 +1158,6 @@ static bool core_info_cache_write(core_info_cache_list_t *list, const char *info success = true; /* Remove 'force refresh' file, if required */ - file_path[0] = '\0'; - if (string_is_empty(info_dir)) strlcpy(file_path, FILE_PATH_CORE_INFO_CACHE_REFRESH, sizeof(file_path)); @@ -1217,8 +1207,6 @@ bool core_info_cache_force_refresh(const char *path_info) { char file_path[PATH_MAX_LENGTH]; - file_path[0] = '\0'; - /* Get 'force refresh' file path */ if (string_is_empty(path_info)) strlcpy(file_path, @@ -1341,14 +1329,11 @@ static core_path_list_t *core_info_path_list_new(const char *core_dir, char exts[32]; size_t i; - exts[0] = '\0'; - if (string_is_empty(core_exts) || !path_list) goto error; - core_ext_list = string_split(core_exts, "|"); - if (!core_ext_list) + if (!(core_ext_list = string_split(core_exts, "|"))) goto error; /* Allocate list containers */ @@ -1478,13 +1463,13 @@ static bool core_info_path_is_locked( uint32_t hash; char lock_filename[NAME_MAX_LENGTH]; - lock_filename[0] = '\0'; - if (lock_list->size < 1) return false; - snprintf(lock_filename, sizeof(lock_filename), - "%s" FILE_PATH_LOCK_EXTENSION, core_file_name); + strlcpy(lock_filename, core_file_name, + sizeof(lock_filename)); + strlcat(lock_filename, FILE_PATH_LOCK_EXTENSION, + sizeof(lock_filename)); hash = core_info_hash_string(lock_filename); @@ -1508,14 +1493,13 @@ static bool core_info_path_is_standalone_exempt( uint32_t hash; char exempt_filename[NAME_MAX_LENGTH]; - exempt_filename[0] = '\0'; - if (exempt_list->size < 1) return false; - snprintf(exempt_filename, sizeof(exempt_filename), - "%s" FILE_PATH_STANDALONE_EXEMPT_EXTENSION, - core_file_name); + strlcpy(exempt_filename, core_file_name, + sizeof(exempt_filename)); + strlcat(exempt_filename, FILE_PATH_STANDALONE_EXEMPT_EXTENSION, + sizeof(exempt_filename)); hash = core_info_hash_string(exempt_filename); @@ -1564,8 +1548,6 @@ static core_info_t *core_info_find_internal( uint32_t hash; char core_file_id[256]; - core_file_id[0] = '\0'; - if (!list || string_is_empty(core_path) || !core_info_get_file_id(path_basename_nocompression(core_path), @@ -1653,7 +1635,6 @@ static config_file_t *core_info_get_config_file( "%s" ".info", core_file_id); else { - info_path[0] = '\0'; fill_pathname_join(info_path, info_dir, core_file_id, sizeof(info_path)); strlcat(info_path, ".info", sizeof(info_path)); @@ -2027,8 +2008,6 @@ static core_info_list_t *core_info_list_new(const char *path, config_file_t *conf = NULL; char core_file_id[256]; - core_file_id[0] = '\0'; - if (!core_info_get_file_id(core_filename, core_file_id, sizeof(core_file_id))) continue; @@ -2496,9 +2475,6 @@ bool core_info_core_file_id_is_equal(const char *core_path_a, char core_file_id_a[256]; char core_file_id_b[256]; - core_file_id_a[0] = '\0'; - core_file_id_b[0] = '\0'; - if ( string_is_empty(core_path_a) || string_is_empty(core_path_b) || !core_info_get_file_id( @@ -2900,8 +2876,6 @@ bool core_info_set_core_lock(const char *core_path, bool lock) core_info_t *core_info = NULL; char lock_file_path[PATH_MAX_LENGTH]; - lock_file_path[0] = '\0'; - #if defined(ANDROID) /* Play Store builds do not support * core locking */ @@ -2916,8 +2890,8 @@ bool core_info_set_core_lock(const char *core_path, bool lock) return false; /* Get lock file path */ - snprintf(lock_file_path, sizeof(lock_file_path), - "%s" FILE_PATH_LOCK_EXTENSION, core_info->path); + strlcpy(lock_file_path, core_info->path, sizeof(lock_file_path)); + strlcat(lock_file_path, FILE_PATH_LOCK_EXTENSION, sizeof(lock_file_path)); /* Create or delete lock file, as required */ if (!core_info_update_core_aux_file(lock_file_path, lock)) @@ -2946,8 +2920,6 @@ bool core_info_get_core_lock(const char *core_path, bool validate_path) bool is_locked = false; char lock_file_path[PATH_MAX_LENGTH]; - lock_file_path[0] = '\0'; - #if defined(ANDROID) /* Play Store builds do not support * core locking */ @@ -2973,8 +2945,10 @@ bool core_info_get_core_lock(const char *core_path, bool validate_path) return false; /* Get lock file path */ - snprintf(lock_file_path, sizeof(lock_file_path), - "%s" FILE_PATH_LOCK_EXTENSION, core_file_path); + strlcpy(lock_file_path, core_file_path, + sizeof(lock_file_path)); + strlcat(lock_file_path, FILE_PATH_LOCK_EXTENSION, + sizeof(lock_file_path)); /* Check whether lock file exists */ is_locked = path_is_valid(lock_file_path); @@ -3003,8 +2977,6 @@ bool core_info_set_core_standalone_exempt(const char *core_path, bool exempt) core_info_t *core_info = NULL; char exempt_file_path[PATH_MAX_LENGTH]; - exempt_file_path[0] = '\0'; - /* Search for specified core */ if (string_is_empty(core_path) || !core_info_find(core_path, &core_info) || @@ -3013,9 +2985,10 @@ bool core_info_set_core_standalone_exempt(const char *core_path, bool exempt) return false; /* Get 'standalone exempt' file path */ - snprintf(exempt_file_path, sizeof(exempt_file_path), - "%s" FILE_PATH_STANDALONE_EXEMPT_EXTENSION, - core_info->path); + strlcpy(exempt_file_path, core_info->path, + sizeof(exempt_file_path)); + strlcat(exempt_file_path, FILE_PATH_STANDALONE_EXEMPT_EXTENSION, + sizeof(exempt_file_path)); /* Create or delete 'standalone exempt' file, as required */ if (!core_info_update_core_aux_file(exempt_file_path, exempt)) @@ -3045,8 +3018,6 @@ bool core_info_get_core_standalone_exempt(const char *core_path) bool is_exempt = false; char exempt_file_path[PATH_MAX_LENGTH]; - exempt_file_path[0] = '\0'; - /* Search for specified core */ if (string_is_empty(core_path) || !core_info_find(core_path, &core_info) || @@ -3055,9 +3026,10 @@ bool core_info_get_core_standalone_exempt(const char *core_path) return false; /* Get 'standalone exempt' file path */ - snprintf(exempt_file_path, sizeof(exempt_file_path), - "%s" FILE_PATH_STANDALONE_EXEMPT_EXTENSION, - core_info->path); + strlcpy(exempt_file_path, core_info->path, + sizeof(exempt_file_path)); + strlcat(exempt_file_path, FILE_PATH_STANDALONE_EXEMPT_EXTENSION, + sizeof(exempt_file_path)); /* Check whether 'standalone exempt' file exists */ is_exempt = path_is_valid(exempt_file_path);