From 3090f3ff3ad13e92743c480bce1f0ada371e4a48 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 29 Sep 2017 20:47:54 +0200 Subject: [PATCH] (task_database.c) Rewrite char arrays --- input/input_remapping.c | 11 +++-- libretro-db/libretrodb.c | 20 ++++++-- menu/cbs/menu_cbs_get_value.c | 3 +- retroarch.c | 6 +++ retroarch.h | 2 +- tasks/task_database.c | 90 +++++++++++++++++++++-------------- 6 files changed, 87 insertions(+), 45 deletions(-) diff --git a/input/input_remapping.c b/input/input_remapping.c index 9f147bbf8c..ae68ab69e3 100644 --- a/input/input_remapping.c +++ b/input/input_remapping.c @@ -46,8 +46,10 @@ bool input_remapping_load_file(void *data, const char *path) if (!conf || string_is_empty(path)) return false; - strlcpy(global->name.remapfile, path, - sizeof(global->name.remapfile)); + if (global->name.remapfile + && !string_is_empty(global->name.remapfile)) + free(global->name.remapfile); + global->name.remapfile = strdup(path); for (i = 0; i < MAX_USERS; i++) { @@ -257,7 +259,10 @@ void input_remapping_set_defaults(bool deinit) if (deinit) { - global->name.remapfile[0] = '\0'; + if (global->name.remapfile + && !string_is_empty(global->name.remapfile)) + free(global->name.remapfile); + global->name.remapfile = NULL; rarch_ctl(RARCH_CTL_UNSET_REMAPS_CORE_ACTIVE, NULL); rarch_ctl(RARCH_CTL_UNSET_REMAPS_GAME_ACTIVE, NULL); } diff --git a/libretro-db/libretrodb.c b/libretro-db/libretrodb.c index 6667527c05..567d1be803 100644 --- a/libretro-db/libretrodb.c +++ b/libretro-db/libretrodb.c @@ -23,7 +23,7 @@ #include #include #ifdef _WIN32 -#include +#include #else #include #endif @@ -34,6 +34,7 @@ #include #include +#include #include #include "libretrodb.h" @@ -57,7 +58,7 @@ struct libretrodb uint64_t root; uint64_t count; uint64_t first_index_offset; - char path[1024]; + char *path; }; struct libretrodb_index @@ -205,7 +206,10 @@ void libretrodb_close(libretrodb_t *db) { if (db->fd) filestream_close(db->fd); - db->fd = NULL; + if (db->path && !string_is_empty(db->path)) + free(db->path); + db->path = NULL; + db->fd = NULL; } int libretrodb_open(const char *path, libretrodb_t *db) @@ -218,8 +222,11 @@ int libretrodb_open(const char *path, libretrodb_t *db) if (!fd) return -errno; - strlcpy(db->path, path, sizeof(db->path)); - db->root = filestream_seek(fd, 0, SEEK_CUR); + if (db->path && !string_is_empty(db->path)) + free(db->path); + + db->path = strdup(path); + db->root = filestream_seek(fd, 0, SEEK_CUR); if ((rv = (int)filestream_read(fd, &header, sizeof(header))) == -1) { @@ -421,6 +428,9 @@ void libretrodb_cursor_close(libretrodb_cursor_t *cursor) int libretrodb_cursor_open(libretrodb_t *db, libretrodb_cursor_t *cursor, libretrodb_query_t *q) { + if (!db || !db->path || string_is_empty(db->path)) + return -errno; + cursor->fd = filestream_open(db->path, RFILE_MODE_READ | RFILE_HINT_MMAP, -1); if (!cursor->fd) diff --git a/menu/cbs/menu_cbs_get_value.c b/menu/cbs/menu_cbs_get_value.c index d9bdc9a51c..94a3876b52 100644 --- a/menu/cbs/menu_cbs_get_value.c +++ b/menu/cbs/menu_cbs_get_value.c @@ -107,7 +107,8 @@ static void menu_action_setting_disp_set_label_remap_file_load( *w = 19; strlcpy(s2, path, len2); - if (global) + if (global && global->name.remapfile + && !string_is_empty(global->name.remapfile)) fill_pathname_base(s, global->name.remapfile, len); } diff --git a/retroarch.c b/retroarch.c index db45deaea9..08dfa2dd99 100644 --- a/retroarch.c +++ b/retroarch.c @@ -323,6 +323,12 @@ static void global_free(void) global = global_get_ptr(); path_clear_all(); dir_clear_all(); + if (global) + { + if (global->name.remapfile + && !string_is_empty(global->name.remapfile)) + free(global->name.remapfile); + } memset(global, 0, sizeof(struct global)); retroarch_override_setting_free_state(); } diff --git a/retroarch.h b/retroarch.h index 3e25a61630..badb2e000b 100644 --- a/retroarch.h +++ b/retroarch.h @@ -225,7 +225,7 @@ typedef struct global char ups[8192]; char bps[8192]; char ips[8192]; - char remapfile[8192]; + char *remapfile; } name; /* Recording. */ diff --git a/tasks/task_database.c b/tasks/task_database.c index 0d0d66e726..84c17c25b7 100644 --- a/tasks/task_database.c +++ b/tasks/task_database.c @@ -57,15 +57,14 @@ typedef struct database_state_handle typedef struct db_handle { - database_state_handle_t state; - database_info_handle_t *handle; - unsigned status; - char playlist_directory[4096]; - char content_database_path[4096]; - bool is_directory; - char fullpath[4096]; bool scan_started; + unsigned status; + char *playlist_directory; + char *content_database_path; + char *fullpath; + database_info_handle_t *handle; + database_state_handle_t state; } db_handle_t; int cue_find_track(const char *cue_path, bool first, size_t *offset, size_t *size, @@ -787,8 +786,11 @@ static int database_info_list_iterate_found_match( strlcat(db_playlist_base_str, file_path_str(FILE_PATH_LPL_EXTENSION), PATH_MAX_LENGTH * sizeof(char)); - fill_pathname_join(db_playlist_path, _db->playlist_directory, - db_playlist_base_str, PATH_MAX_LENGTH * sizeof(char)); + + if (_db->playlist_directory && + !string_is_empty(_db->playlist_directory)) + fill_pathname_join(db_playlist_path, _db->playlist_directory, + db_playlist_base_str, PATH_MAX_LENGTH * sizeof(char)); playlist = playlist_init(db_playlist_path, COLLECTION_SIZE); @@ -957,10 +959,12 @@ static int task_database_iterate_playlist_lutro( db_playlist_path[0] = '\0'; - fill_pathname_join(db_playlist_path, - _db->playlist_directory, - file_path_str(FILE_PATH_LUTRO_PLAYLIST), - PATH_MAX_LENGTH * sizeof(char)); + if (_db->playlist_directory && + !string_is_empty(_db->playlist_directory)) + fill_pathname_join(db_playlist_path, + _db->playlist_directory, + file_path_str(FILE_PATH_LUTRO_PLAYLIST), + PATH_MAX_LENGTH * sizeof(char)); playlist = playlist_init(db_playlist_path, COLLECTION_SIZE); @@ -1119,10 +1123,13 @@ static void task_database_handler(retro_task_t *task) { db->scan_started = true; - if (db->is_directory) - db->handle = database_info_dir_init(db->fullpath, DATABASE_TYPE_ITERATE, task); - else - db->handle = database_info_file_init(db->fullpath, DATABASE_TYPE_ITERATE, task); + if (db->fullpath && !string_is_empty(db->fullpath)) + { + if (db->is_directory) + db->handle = database_info_dir_init(db->fullpath, DATABASE_TYPE_ITERATE, task); + else + db->handle = database_info_file_init(db->fullpath, DATABASE_TYPE_ITERATE, task); + } task_free_title(task); @@ -1141,20 +1148,26 @@ static void task_database_handler(retro_task_t *task) case DATABASE_STATUS_ITERATE_BEGIN: if (dbstate && !dbstate->list) { - dbstate->list = dir_list_new_special( - db->content_database_path, - DIR_LIST_DATABASES, NULL); + if (db->content_database_path && + !string_is_empty(db->content_database_path)) + dbstate->list = dir_list_new_special( + db->content_database_path, + DIR_LIST_DATABASES, NULL); /* If the scan path matches a database path exactly then * save time by only processing that database. */ if (dbstate->list && db->is_directory) { - size_t i; - char *dirname = find_last_slash(db->fullpath) + 1; + size_t i; + char *dirname = NULL; + + if (db->fullpath && + !string_is_empty(db->fullpath)) + dirname = find_last_slash(db->fullpath) + 1; for (i = 0; i < dbstate->list->size; i++) { - char *dbname; + char *dbname; char *dbpath = strdup(dbstate->list->elems[i].data); path_remove_extension(dbpath); @@ -1166,7 +1179,7 @@ static void task_database_handler(retro_task_t *task) free(dbpath); single_list = string_list_new(); string_list_append(single_list, dbstate->list->elems[i].data, - dbstate->list->elems[i].attr); + dbstate->list->elems[i].attr); dir_list_free(dbstate->list); dbstate->list = single_list; break; @@ -1227,6 +1240,15 @@ task_finished: if (db) { + if (db->playlist_directory && + !string_is_empty(db->playlist_directory)) + free(db->playlist_directory); + if (db->content_database_path && + !string_is_empty(db->content_database_path)) + free(db->content_database_path); + if (db->fullpath && + !string_is_empty(db->fullpath)) + free(db->fullpath); if (db->state.buf) free(db->state.buf); @@ -1251,18 +1273,16 @@ bool task_push_dbscan( if (!t || !db) goto error; - t->handler = task_database_handler; - t->state = db; - t->callback = cb; - t->title = strdup(msg_hash_to_str(MSG_PREPARING_FOR_CONTENT_SCAN)); + t->handler = task_database_handler; + t->state = db; + t->callback = cb; + t->title = strdup(msg_hash_to_str(MSG_PREPARING_FOR_CONTENT_SCAN)); - db->is_directory = directory; - - strlcpy(db->fullpath, fullpath, sizeof(db->fullpath)); - strlcpy(db->playlist_directory, playlist_directory, - sizeof(db->playlist_directory)); - strlcpy(db->content_database_path, content_database, - sizeof(db->content_database_path)); + db->is_directory = directory; + db->playlist_directory = NULL; + db->fullpath = strdup(fullpath); + db->playlist_directory = strdup(playlist_directory); + db->content_database_path = strdup(content_database); task_queue_push(t);