From 49cf80db109682db6afaf48a13b9e914648a135b Mon Sep 17 00:00:00 2001 From: twinaphex Date: Mon, 18 Sep 2017 15:57:05 +0200 Subject: [PATCH] Fix resource leaks --- libretro-common/formats/libchdr/huffman.c | 66 ++++++++++++++--------- menu/cbs/menu_cbs_ok.c | 13 +++-- tasks/task_database.c | 40 +++++++------- tasks/task_database_cue.c | 5 +- 4 files changed, 73 insertions(+), 51 deletions(-) diff --git a/libretro-common/formats/libchdr/huffman.c b/libretro-common/formats/libchdr/huffman.c index 932d45b281..fb5445f68f 100644 --- a/libretro-common/formats/libchdr/huffman.c +++ b/libretro-common/formats/libchdr/huffman.c @@ -388,9 +388,13 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint int nextalloc; int maxbits = 0; /* make a list of all non-zero nodes */ - struct node_t** list = (struct node_t**)malloc(sizeof(struct node_t*) * decoder->numcodes * 2); + struct node_t** list = (struct node_t**) + malloc(sizeof(struct node_t*) * decoder->numcodes * 2); int curcode, listitems = 0; - memset(decoder->huffnode, 0, decoder->numcodes * sizeof(decoder->huffnode[0])); + + memset(decoder->huffnode, 0, + decoder->numcodes * sizeof(decoder->huffnode[0])); + for (curcode = 0; curcode < decoder->numcodes; curcode++) if (decoder->datahisto[curcode] != 0) { @@ -403,21 +407,28 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint if (decoder->huffnode[curcode].weight == 0) decoder->huffnode[curcode].weight = 1; } -/* - fprintf(stderr, "Pre-sort:\n"); - for (int i = 0; i < listitems; i++) { - fprintf(stderr, "weight: %d code: %d\n", list[i]->m_weight, list[i]->m_bits); - } -*/ - /* sort the list by weight, largest weight first */ + +#if 0 + { + unsigned i; + fprintf(stderr, "Pre-sort:\n"); + for (i = 0; i < listitems; i++) + fprintf(stderr, "weight: %d code: %d\n", + list[i]->m_weight, list[i]->m_bits); + */ sort the list by weight, largest weight first */ + } +#endif + qsort(&list[0], listitems, sizeof(list[0]), huffman_tree_node_compare); -/* - fprintf(stderr, "Post-sort:\n"); - for (int i = 0; i < listitems; i++) { - fprintf(stderr, "weight: %d code: %d\n", list[i]->m_weight, list[i]->m_bits); - } - fprintf(stderr, "===================\n"); -*/ + +#if 0 + fprintf(stderr, "Post-sort:\n"); + for (int i = 0; i < listitems; i++) { + fprintf(stderr, "weight: %d code: %d\n", list[i]->m_weight, list[i]->m_bits); + } + fprintf(stderr, "===================\n"); +#endif + /* now build the tree */ nextalloc = decoder->numcodes; @@ -425,27 +436,32 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint { int curitem; /* remove lowest two items */ - struct node_t* node1 = &(*list[--listitems]); - struct node_t* node0 = &(*list[--listitems]); + struct node_t* node1 = &(*list[--listitems]); + struct node_t* node0 = &(*list[--listitems]); /* create new node */ struct node_t* newnode = &decoder->huffnode[nextalloc++]; - newnode->parent = NULL; - node0->parent = node1->parent = newnode; - newnode->weight = node0->weight + node1->weight; + newnode->parent = NULL; + node0->parent = + node1->parent = newnode; + newnode->weight = + node0->weight + node1->weight; /* insert into list at appropriate location */ for (curitem = 0; curitem < listitems; curitem++) if (newnode->weight > list[curitem]->weight) { - memmove(&list[curitem+1], &list[curitem], (listitems - curitem) * sizeof(list[0])); + memmove(&list[curitem+1], + &list[curitem], + (listitems - curitem) * sizeof(list[0])); break; } list[curitem] = newnode; listitems++; } - /* compute the number of bits in each code, and fill in another histogram */ + /* compute the number of bits in each code, + * and fill in another histogram */ for (curcode = 0; curcode < decoder->numcodes; curcode++) { struct node_t *curnode; @@ -457,7 +473,8 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint if (node->weight > 0) { /* determine the number of bits for this node */ - for (curnode = node; curnode->parent != NULL; curnode = curnode->parent) + for (curnode = node; + curnode->parent != NULL; curnode = curnode->parent) node->numbits++; if (node->numbits == 0) node->numbits = 1; @@ -466,6 +483,7 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint maxbits = MAX(maxbits, ((int)node->numbits)); } } + free(list); return maxbits; } diff --git a/menu/cbs/menu_cbs_ok.c b/menu/cbs/menu_cbs_ok.c index 9b2c920698..4ef4865be2 100644 --- a/menu/cbs/menu_cbs_ok.c +++ b/menu/cbs/menu_cbs_ok.c @@ -1085,8 +1085,11 @@ static int file_load_with_detect_core_wrapper( free(menu_path_new); if (enum_label_idx == MENU_ENUM_LABEL_COLLECTION) + { + free(new_core_path); return generic_action_ok_displaylist_push(path, NULL, NULL, 0, idx, entry_idx, ACTION_OK_DL_DEFERRED_CORE_LIST_SET); + } switch (ret) { @@ -1109,19 +1112,19 @@ static int file_load_with_detect_core_wrapper( return -1; } - free(new_core_path); - return 0; + ret = 0; + break; } case 0: - free(new_core_path); - return generic_action_ok_displaylist_push(path, NULL, label, type, + ret = generic_action_ok_displaylist_push(path, NULL, label, type, idx, entry_idx, ACTION_OK_DL_DEFERRED_CORE_LIST); + break; default: - free(new_core_path); break; } } + free(new_core_path); return ret; } diff --git a/tasks/task_database.c b/tasks/task_database.c index 04c1faf397..ae8697c8ca 100644 --- a/tasks/task_database.c +++ b/tasks/task_database.c @@ -79,19 +79,16 @@ int detect_psp_game(intfstream_t *fd, char *game_id); int detect_serial_ascii_game(intfstream_t *fd, char *game_id); -static intfstream_t* -open_file(const char *path) +static intfstream_t* open_file(const char *path) { intfstream_info_t info; intfstream_t *fd = NULL; - info.type = INTFSTREAM_FILE; + info.type = INTFSTREAM_FILE; + fd = intfstream_init(&info); - fd = intfstream_init(&info); if (!fd) - { return NULL; - } if (!intfstream_open(fd, path, RFILE_MODE_READ, -1)) { @@ -102,20 +99,18 @@ open_file(const char *path) return fd; } -static intfstream_t* -open_chd_track(const char *path, int32_t track) +static intfstream_t *open_chd_track(const char *path, int32_t track) { intfstream_info_t info; intfstream_t *fd = NULL; - info.type = INTFSTREAM_CHD; - info.chd.track = track; + info.type = INTFSTREAM_CHD; + info.chd.track = track; + + fd = intfstream_init(&info); - fd = intfstream_init(&info); if (!fd) - { return NULL; - } if (!intfstream_open(fd, path, RFILE_MODE_READ, -1)) { @@ -126,28 +121,33 @@ open_chd_track(const char *path, int32_t track) return fd; } -static void database_info_set_type(database_info_handle_t *handle, enum database_type type) +static void database_info_set_type( + database_info_handle_t *handle, + enum database_type type) { if (!handle) return; handle->type = type; } -static enum database_type database_info_get_type(database_info_handle_t *handle) +static enum database_type database_info_get_type( + database_info_handle_t *handle) { if (!handle) return DATABASE_TYPE_NONE; return handle->type; } -static const char *database_info_get_current_name(database_state_handle_t *handle) +static const char *database_info_get_current_name( + database_state_handle_t *handle) { if (!handle || !handle->list) return NULL; return handle->list->elems[handle->list_index].data; } -static const char *database_info_get_current_element_name(database_info_handle_t *handle) +static const char *database_info_get_current_element_name( + database_info_handle_t *handle) { if (!handle || !handle->list) return NULL; @@ -198,7 +198,8 @@ static int stream_get_serial(database_state_handle_t *db_state, if (detect_serial_ascii_game(fd, serial)) { /* ASCII serial (Wii) was detected. */ - RARCH_LOG("%s '%s'\n", msg_hash_to_str(MSG_FOUND_DISK_LABEL), serial); + RARCH_LOG("%s '%s'\n", + msg_hash_to_str(MSG_FOUND_DISK_LABEL), serial); return 0; } @@ -233,12 +234,11 @@ static int iso_get_serial(database_state_handle_t *db_state, int rv; if (!fd) - { return 0; - } rv = stream_get_serial(db_state, db, fd, serial); intfstream_close(fd); + free(fd); return rv; } diff --git a/tasks/task_database_cue.c b/tasks/task_database_cue.c index 648ad47a95..94e7e5e5a0 100644 --- a/tasks/task_database_cue.c +++ b/tasks/task_database_cue.c @@ -406,15 +406,16 @@ int find_first_data_track(const char *cue_path, int32_t *offset, char *track_path, size_t max_len) { int rv; - char * tmp_token = malloc(MAX_TOKEN_LEN * sizeof(char)); intfstream_info_t info; intfstream_t *fd = NULL; + char * tmp_token = malloc(MAX_TOKEN_LEN * sizeof(char)); - info.type = INTFSTREAM_FILE; + info.type = INTFSTREAM_FILE; fd = intfstream_init(&info); if (!fd) { + free(tmp_token); return -errno; }