Fix resource leaks

This commit is contained in:
twinaphex 2017-09-18 15:57:05 +02:00
parent 21ff58f66e
commit 49cf80db10
4 changed files with 73 additions and 51 deletions

View File

@ -388,9 +388,13 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint
int nextalloc; int nextalloc;
int maxbits = 0; int maxbits = 0;
/* make a list of all non-zero nodes */ /* 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; 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++) for (curcode = 0; curcode < decoder->numcodes; curcode++)
if (decoder->datahisto[curcode] != 0) 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) if (decoder->huffnode[curcode].weight == 0)
decoder->huffnode[curcode].weight = 1; decoder->huffnode[curcode].weight = 1;
} }
/*
fprintf(stderr, "Pre-sort:\n"); #if 0
for (int i = 0; i < listitems; i++) { {
fprintf(stderr, "weight: %d code: %d\n", list[i]->m_weight, list[i]->m_bits); unsigned i;
} fprintf(stderr, "Pre-sort:\n");
*/ for (i = 0; i < listitems; i++)
/* sort the list by weight, largest weight first */ 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); qsort(&list[0], listitems, sizeof(list[0]), huffman_tree_node_compare);
/*
fprintf(stderr, "Post-sort:\n"); #if 0
for (int i = 0; i < listitems; i++) { fprintf(stderr, "Post-sort:\n");
fprintf(stderr, "weight: %d code: %d\n", list[i]->m_weight, list[i]->m_bits); 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"); }
*/ fprintf(stderr, "===================\n");
#endif
/* now build the tree */ /* now build the tree */
nextalloc = decoder->numcodes; nextalloc = decoder->numcodes;
@ -425,27 +436,32 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint
{ {
int curitem; int curitem;
/* remove lowest two items */ /* remove lowest two items */
struct node_t* node1 = &(*list[--listitems]); struct node_t* node1 = &(*list[--listitems]);
struct node_t* node0 = &(*list[--listitems]); struct node_t* node0 = &(*list[--listitems]);
/* create new node */ /* create new node */
struct node_t* newnode = &decoder->huffnode[nextalloc++]; struct node_t* newnode = &decoder->huffnode[nextalloc++];
newnode->parent = NULL; newnode->parent = NULL;
node0->parent = node1->parent = newnode; node0->parent =
newnode->weight = node0->weight + node1->weight; node1->parent = newnode;
newnode->weight =
node0->weight + node1->weight;
/* insert into list at appropriate location */ /* insert into list at appropriate location */
for (curitem = 0; curitem < listitems; curitem++) for (curitem = 0; curitem < listitems; curitem++)
if (newnode->weight > list[curitem]->weight) 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; break;
} }
list[curitem] = newnode; list[curitem] = newnode;
listitems++; 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++) for (curcode = 0; curcode < decoder->numcodes; curcode++)
{ {
struct node_t *curnode; struct node_t *curnode;
@ -457,7 +473,8 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint
if (node->weight > 0) if (node->weight > 0)
{ {
/* determine the number of bits for this node */ /* 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++; node->numbits++;
if (node->numbits == 0) if (node->numbits == 0)
node->numbits = 1; 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)); maxbits = MAX(maxbits, ((int)node->numbits));
} }
} }
free(list);
return maxbits; return maxbits;
} }

View File

@ -1085,8 +1085,11 @@ static int file_load_with_detect_core_wrapper(
free(menu_path_new); free(menu_path_new);
if (enum_label_idx == MENU_ENUM_LABEL_COLLECTION) if (enum_label_idx == MENU_ENUM_LABEL_COLLECTION)
{
free(new_core_path);
return generic_action_ok_displaylist_push(path, NULL, return generic_action_ok_displaylist_push(path, NULL,
NULL, 0, idx, entry_idx, ACTION_OK_DL_DEFERRED_CORE_LIST_SET); NULL, 0, idx, entry_idx, ACTION_OK_DL_DEFERRED_CORE_LIST_SET);
}
switch (ret) switch (ret)
{ {
@ -1109,19 +1112,19 @@ static int file_load_with_detect_core_wrapper(
return -1; return -1;
} }
free(new_core_path); ret = 0;
return 0; break;
} }
case 0: case 0:
free(new_core_path); ret = generic_action_ok_displaylist_push(path, NULL, label, type,
return generic_action_ok_displaylist_push(path, NULL, label, type,
idx, entry_idx, ACTION_OK_DL_DEFERRED_CORE_LIST); idx, entry_idx, ACTION_OK_DL_DEFERRED_CORE_LIST);
break;
default: default:
free(new_core_path);
break; break;
} }
} }
free(new_core_path);
return ret; return ret;
} }

View File

@ -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); int detect_serial_ascii_game(intfstream_t *fd, char *game_id);
static intfstream_t* static intfstream_t* open_file(const char *path)
open_file(const char *path)
{ {
intfstream_info_t info; intfstream_info_t info;
intfstream_t *fd = NULL; intfstream_t *fd = NULL;
info.type = INTFSTREAM_FILE; info.type = INTFSTREAM_FILE;
fd = intfstream_init(&info);
fd = intfstream_init(&info);
if (!fd) if (!fd)
{
return NULL; return NULL;
}
if (!intfstream_open(fd, path, RFILE_MODE_READ, -1)) if (!intfstream_open(fd, path, RFILE_MODE_READ, -1))
{ {
@ -102,20 +99,18 @@ open_file(const char *path)
return fd; return fd;
} }
static intfstream_t* static intfstream_t *open_chd_track(const char *path, int32_t track)
open_chd_track(const char *path, int32_t track)
{ {
intfstream_info_t info; intfstream_info_t info;
intfstream_t *fd = NULL; intfstream_t *fd = NULL;
info.type = INTFSTREAM_CHD; info.type = INTFSTREAM_CHD;
info.chd.track = track; info.chd.track = track;
fd = intfstream_init(&info);
fd = intfstream_init(&info);
if (!fd) if (!fd)
{
return NULL; return NULL;
}
if (!intfstream_open(fd, path, RFILE_MODE_READ, -1)) if (!intfstream_open(fd, path, RFILE_MODE_READ, -1))
{ {
@ -126,28 +121,33 @@ open_chd_track(const char *path, int32_t track)
return fd; 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) if (!handle)
return; return;
handle->type = type; 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) if (!handle)
return DATABASE_TYPE_NONE; return DATABASE_TYPE_NONE;
return handle->type; 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) if (!handle || !handle->list)
return NULL; return NULL;
return handle->list->elems[handle->list_index].data; 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) if (!handle || !handle->list)
return NULL; return NULL;
@ -198,7 +198,8 @@ static int stream_get_serial(database_state_handle_t *db_state,
if (detect_serial_ascii_game(fd, serial)) if (detect_serial_ascii_game(fd, serial))
{ {
/* ASCII serial (Wii) was detected. */ /* 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; return 0;
} }
@ -233,12 +234,11 @@ static int iso_get_serial(database_state_handle_t *db_state,
int rv; int rv;
if (!fd) if (!fd)
{
return 0; return 0;
}
rv = stream_get_serial(db_state, db, fd, serial); rv = stream_get_serial(db_state, db, fd, serial);
intfstream_close(fd); intfstream_close(fd);
free(fd);
return rv; return rv;
} }

View File

@ -406,15 +406,16 @@ int find_first_data_track(const char *cue_path,
int32_t *offset, char *track_path, size_t max_len) int32_t *offset, char *track_path, size_t max_len)
{ {
int rv; int rv;
char * tmp_token = malloc(MAX_TOKEN_LEN * sizeof(char));
intfstream_info_t info; intfstream_info_t info;
intfstream_t *fd = NULL; 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); fd = intfstream_init(&info);
if (!fd) if (!fd)
{ {
free(tmp_token);
return -errno; return -errno;
} }