Fixed memory leaks in node and test

This commit is contained in:
John Jones 2016-12-07 11:53:17 -05:00
parent 75bee8d1ea
commit e69f10a68f
3 changed files with 33 additions and 10 deletions

View file

@ -50,5 +50,7 @@ int ipfs_merkledag_get(const struct Cid* cid, struct Node** node, const struct F
*node = N_Create_From_Data(block->data, block->data_length);
Node_Set_Cached(*node, cid);
ipfs_blocks_block_free(block);
return retVal;
}

View file

@ -155,7 +155,10 @@ void Node_Delete(struct Node * N)
}
if(N->cached)
{
free(N->cached);
ipfs_cid_free(N->cached);
}
if (N->data) {
free(N->data);
}
free(N);
}
@ -264,14 +267,15 @@ struct Node * N_Create_From_Data(unsigned char * data, size_t data_size)
{
struct Node * mynode;
mynode = (struct Node *) malloc(sizeof(struct Node));
mynode->data = data;
mynode->data = malloc(sizeof(unsigned char) * data_size);
memcpy(mynode->data, data, data_size);
mynode->data_size = data_size;
mynode->link_ammount=0;
mynode->encoded = NULL;
mynode->cached = NULL;
return mynode;
}
return NULL;
return NULL;
}
/*N_Create_From_Encoded
* @param data: encoded bytes buffer you want to create the node from

View file

@ -35,29 +35,46 @@ int test_merkledag_get_data() {
}
// create a node
struct Node* node1 = N_Create_From_Data(binary_data, 256);
struct Node* node1 = N_Create_From_Data(binary_data, binary_data_size);
retVal = ipfs_merkledag_add(node1, fs_repo);
if (retVal == 0) {
Node_Delete(node1);
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
// now retrieve it
struct Node* results_node;
retVal = ipfs_merkledag_get(node1->cached, &results_node, fs_repo);
if (retVal == 0)
if (retVal == 0) {
Node_Delete(node1);
Node_Delete(results_node);
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
if (results_node->data_size != 256)
if (results_node->data_size != 256) {
Node_Delete(node1);
Node_Delete(results_node);
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
// the data should be the same
for(int i = 0; i < results_node->data_size; i++) {
if (results_node->data[i] != node1->data[i])
if (results_node->data[i] != node1->data[i]) {
Node_Delete(node1);
Node_Delete(results_node);
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
}
Node_Delete(node1);
Node_Delete(results_node);
ipfs_repo_fsrepo_free(fs_repo);
return retVal;
}
@ -97,7 +114,7 @@ int test_merkledag_add_data() {
}
// create a node
struct Node* node1 = N_Create_From_Data(binary_data, 256);
struct Node* node1 = N_Create_From_Data(binary_data, binary_data_size);
retVal = ipfs_merkledag_add(node1, fs_repo);
if (retVal == 0) {
@ -116,7 +133,7 @@ int test_merkledag_add_data() {
}
// adding the same binary again should do nothing (the hash should be the same)
struct Node* node2 = N_Create_From_Data(binary_data, 255);
struct Node* node2 = N_Create_From_Data(binary_data, binary_data_size);
retVal = ipfs_merkledag_add(node2, fs_repo);
if (retVal == 0) {
Node_Delete(node1);
@ -150,7 +167,7 @@ int test_merkledag_add_data() {
// now change 1 byte, which should change the hash
binary_data[10] = 0;
// create a node
struct Node* node3 = N_Create_From_Data(binary_data, 255);
struct Node* node3 = N_Create_From_Data(binary_data, binary_data_size);
retVal = ipfs_merkledag_add(node3, fs_repo);
if (retVal == 0) {