Fixed memory leaks around node and allocations

This commit is contained in:
John Jones 2016-12-12 18:27:46 -05:00
parent 363a773a1e
commit 876e2dfcf2
3 changed files with 36 additions and 25 deletions

View file

@ -128,7 +128,7 @@ unsigned char * ipfs_node_get_data(struct Node * N);
* It will take care of the links inside it.
* @param N: the node you want to free. (struct Node *)
*/
void ipfs_node_free(struct Node * N);
int ipfs_node_free(struct Node * N);
/*ipfs_node_get_link_by_name
* Returns a copy of the link with given name

View file

@ -45,9 +45,13 @@ int ipfs_node_link_new(char * name, unsigned char * ahash, struct NodeLink** nod
*/
int ipfs_node_link_free(struct NodeLink * node_link)
{
if (node_link != NULL)
ipfs_cid_free(node_link->cid);
free(node_link);
if (node_link != NULL) {
if (node_link->cid != NULL)
ipfs_cid_free(node_link->cid);
if (node_link->name != NULL)
free(node_link->name);
free(node_link);
}
return 1;
}
@ -389,11 +393,12 @@ int ipfs_node_remove_link(struct Node* node, struct NodeLink* toRemove) {
* It will take care of the links inside it.
* @param N: the node you want to free. (struct Node *)
*/
void ipfs_node_free(struct Node * N)
int ipfs_node_free(struct Node * N)
{
if(N != NULL)
{
struct NodeLink* current = ipfs_node_link_last(N);
// remove links
struct NodeLink* current = N->head_link;
while (current != NULL) {
struct NodeLink* toDelete = current;
current = current->next;
@ -406,8 +411,12 @@ void ipfs_node_free(struct Node * N)
if (N->data) {
free(N->data);
}
free(N);
if (N->encoded != NULL) {
free(N->encoded);
}
free(N);
}
return 1;
}
/*ipfs_node_get_link_by_name

View file

@ -58,27 +58,27 @@ int test_node_link_encode_decode() {
// make a NodeLink
if (ipfs_node_link_new("My Name", "QmMyHash", &control) == 0)
goto exit;
goto l_exit;
// encode it
nl_size = ipfs_node_link_protobuf_encode_size(control);
buffer = malloc(nl_size);
if (buffer == NULL)
goto exit;
goto l_exit;
if (ipfs_node_link_protobuf_encode(control, buffer, nl_size, &nl_size) == 0) {
goto exit;
goto l_exit;
}
// decode it
if (ipfs_node_link_protobuf_decode(buffer, nl_size, &results) == 0) {
goto exit;
goto l_exit;
}
// verify it
if (compare_link(control, results) == 0)
goto exit;
goto l_exit;
retVal = 1;
exit:
l_exit:
if (control != NULL)
ipfs_node_link_free(control);
if (results != NULL)
@ -100,30 +100,32 @@ int test_node_encode_decode() {
// node
if (ipfs_node_new(&control) == 0)
goto exit;
goto ed_exit;
// first link
if (ipfs_node_link_new((char*)"Link1", (unsigned char*)"QmLink1", &link1) == 0)
goto exit;
goto ed_exit;
if ( ipfs_node_add_link(control, link1) == 0)
goto exit;
goto ed_exit;
// second link
// TODO: put here to diagnose a memory leak. Remove the comments!
/*
if (ipfs_node_link_new((char*)"Link2", (unsigned char*)"QmLink2", &link2) == 0)
goto exit;
goto ed_exit;
if ( ipfs_node_add_link(control, link2) == 0)
goto exit;
goto ed_exit;
*/
// encode
buffer_length = ipfs_node_protobuf_encode_size(control);
buffer = (unsigned char*)malloc(buffer_length);
if (ipfs_node_protobuf_encode(control, buffer, buffer_length, &buffer_length) == 0)
goto exit;
goto ed_exit;
// decode
if (ipfs_node_protobuf_decode(buffer, buffer_length, &results) == 0)
goto exit;
goto ed_exit;
// compare results
@ -132,21 +134,21 @@ int test_node_encode_decode() {
while(control_link != NULL) {
if (compare_link(control_link, results_link) == 0) {
printf("Error was on link %s\n", control_link->name);
goto exit;
goto ed_exit;
}
control_link = control_link->next;
results_link = results_link->next;
}
if (control->data_size != results->data_size)
goto exit;
goto ed_exit;
if (memcmp(results->data, control->data, control->data_size) != 0) {
goto exit;
goto ed_exit;
}
retVal = 1;
exit:
ed_exit:
// clean up
if (control != NULL)
ipfs_node_free(control);