Fix of memory leaks

This commit is contained in:
John Jones 2017-08-03 17:46:20 -05:00
parent 2b24b00324
commit 3cc75058f0
7 changed files with 87 additions and 13 deletions

View file

@ -59,6 +59,14 @@ int ipfs_bitswap_free(struct Exchange* exchange) {
struct BitswapContext* bitswapContext = (struct BitswapContext*) exchange->exchangeContext;
if (bitswapContext != NULL)
ipfs_bitswap_engine_stop(bitswapContext);
if (bitswapContext->localWantlist != NULL) {
ipfs_bitswap_wantlist_queue_free(bitswapContext->localWantlist);
bitswapContext->localWantlist = NULL;
}
if (bitswapContext->peerRequestQueue != NULL) {
ipfs_bitswap_peer_request_queue_free(bitswapContext->peerRequestQueue);
bitswapContext->peerRequestQueue = NULL;
}
free(exchange->exchangeContext);
}
free(exchange);

View file

@ -97,6 +97,7 @@ void* ipfs_bitswap_engine_peer_request_processor_start(void* ctx) {
if (current_peer_entry->sessionContext->default_stream->read(current_peer_entry->sessionContext, &buffer, &buffer_len, 1)) {
// handle it
int retVal = ipfs_multistream_marshal(buffer, buffer_len, current_peer_entry->sessionContext, context->ipfsNode);
free(buffer);
did_some_processing = 1;
if (retVal == -1) {
// there was a problem. Clean up

View file

@ -139,10 +139,12 @@ int ipfs_bitswap_network_handle_message(const struct IpfsNode* node, const struc
if (!ipfs_cid_protobuf_decode(entry->block, entry->block_size, &cid) || cid->hash_length == 0) {
libp2p_logger_error("bitswap_network", "Message had invalid CID\n");
ipfs_cid_free(cid);
ipfs_bitswap_message_free(message);
return 0;
}
ipfs_bitswap_network_adjust_cid_queue(queueEntry->current->cids_they_want, cid, entry->cancel);
}
}
ipfs_bitswap_message_free(message);
return 1;
}

View file

@ -57,13 +57,45 @@ struct PeerRequest* ipfs_bitswap_peer_request_new() {
return request;
}
int ipfs_bitswap_cid_entry_free(struct CidEntry* entry) {
if (entry != NULL) {
if (entry->cid != NULL) {
ipfs_cid_free(entry->cid);
entry->cid = NULL;
}
free(entry);
}
return 1;
}
/**
* Free resources from a PeerRequest
* @param request the request to free
* @returns true(1)
*/
int ipfs_bitswap_peer_request_free(struct PeerRequest* request) {
free(request);
if (request != NULL) {
for(int i = 0; i < request->cids_we_want->total; i++) {
struct CidEntry* entry = (struct CidEntry*)libp2p_utils_vector_get(request->cids_we_want, i);
ipfs_bitswap_cid_entry_free(entry);
}
libp2p_utils_vector_free(request->cids_we_want);
request->cids_we_want = NULL;
for(int i = 0; i < request->cids_they_want->total; i++) {
struct CidEntry* entry = (struct CidEntry*)libp2p_utils_vector_get(request->cids_they_want, i);
ipfs_bitswap_cid_entry_free(entry);
}
libp2p_utils_vector_free(request->cids_they_want);
request->cids_they_want = NULL;
for(int i = 0; i < request->blocks_we_want_to_send->total; i++) {
struct Block* block = (struct Block*)libp2p_utils_vector_get(request->blocks_we_want_to_send, i);
ipfs_block_free(block);
}
libp2p_utils_vector_free(request->blocks_we_want_to_send);
request->blocks_we_want_to_send = NULL;
free(request);
}
return 1;
}
@ -341,8 +373,11 @@ int ipfs_bitswap_peer_request_process_entry(const struct BitswapContext* context
// add requests that we would like
ipfs_bitswap_message_add_wantlist_items(msg, request->cids_we_want);
// send message
if (ipfs_bitswap_network_send_message(context, request->peer, msg))
if (ipfs_bitswap_network_send_message(context, request->peer, msg)) {
ipfs_bitswap_message_free(msg);
return 1;
}
ipfs_bitswap_message_free(msg);
}
}
return 0;

View file

@ -47,6 +47,10 @@ struct WantListQueue* ipfs_bitswap_wantlist_queue_new() {
int ipfs_bitswap_wantlist_queue_free(struct WantListQueue* wantlist) {
if (wantlist != NULL) {
if (wantlist->queue != NULL) {
for(int i = 0; i < wantlist->queue->total; i++) {
struct WantListQueueEntry* entry = (struct WantListQueueEntry*)libp2p_utils_vector_get(wantlist->queue, i);
ipfs_bitswap_wantlist_queue_entry_free(entry);
}
libp2p_utils_vector_free(wantlist->queue);
wantlist->queue = NULL;
}
@ -262,7 +266,9 @@ int ipfs_bitswap_wantlist_get_block_remote(struct BitswapContext* context, struc
libp2p_utils_vector_add(queueEntry->cids_we_want, entry);
// process this queue via bitswap protocol
ipfs_bitswap_peer_request_process_entry(context, queueEntry);
//libp2p_peer_free(current);
}
libp2p_utils_vector_free(providers);
return 1;
}
return 0;

View file

@ -36,16 +36,24 @@ struct Libp2pMessage* ipfs_routing_online_send_receive_message(struct SessionCon
}
// send the message, and expect the same back
sessionContext->default_stream->write(sessionContext, protobuf, protobuf_size);
sessionContext->default_stream->read(sessionContext, &results, &results_size, 5);
if (results_size == 0)
if (!sessionContext->default_stream->write(sessionContext, protobuf, protobuf_size)) {
libp2p_logger_error("online", "Attempted to write to Kademlia stream, but could not.\n");
goto exit;
}
if (!sessionContext->default_stream->read(sessionContext, &results, &results_size, 5)) {
libp2p_logger_error("online", "Attempted to read from Kademlia stream, but could not.\n");
}
if (results_size == 0) {
libp2p_logger_error("online", "reading kademlia response returned nothing.\n");
goto exit;
}
// see if we can unprotobuf
if (!libp2p_message_protobuf_decode(results, results_size, &return_message))
if (!libp2p_message_protobuf_decode(results, results_size, &return_message)) {
libp2p_logger_error("online", "Received kademlia response, but cannot decode it.\n");
goto exit;
}
exit:
if (protobuf != NULL)
@ -464,13 +472,14 @@ int ipfs_routing_online_bootstrap(struct IpfsRouting* routing) {
peer_id_size = strlen(peer_id);
peer = libp2p_peer_new();
peer->id_size = peer_id_size;
peer->id = malloc(peer->id_size);
peer->id = malloc(peer->id_size + 1);
if (peer->id == NULL) { // out of memory?
libp2p_peer_free(peer);
free(peer_id);
return -1;
}
memcpy(peer->id, peer_id, peer->id_size);
peer->id[peer->id_size] = 0;
peer->addr_head = libp2p_utils_linked_list_new();
if (peer->addr_head == NULL) { // out of memory?
libp2p_peer_free(peer);

View file

@ -1,4 +1,5 @@
#include <pthread.h>
#include <signal.h>
#include "../test_helper.h"
#include "../routing/test_routing.h" // for test_routing_daemon_start
#include "libp2p/utils/vector.h"
@ -262,16 +263,17 @@ int test_bitswap_retrieve_file_remote() {
*/
int test_bitswap_retrieve_file_known_remote() {
int retVal = 0;
signal(SIGPIPE, SIG_IGN);
/***
* This assumes a remote server with the hello_world.txt file already in its database
*/
int remote_port = 4001;
// mac
// char* remote_peer_id = "QmZVoAZGFfinB7MQQiDzB84kWaDPQ95GLuXdemJFM2r9b4";
// char* remote_ip = "10.211.55.2";
char* remote_peer_id = "QmZVoAZGFfinB7MQQiDzB84kWaDPQ95GLuXdemJFM2r9b4";
char* remote_ip = "10.211.55.2";
// linux
char* remote_peer_id = "QmRKm1d9kSCRpMFtLYpfhhCQ3DKuSSPJa3qn9wWXfwnWnY";
char* remote_ip = "10.211.55.4";
//char* remote_peer_id = "QmRKm1d9kSCRpMFtLYpfhhCQ3DKuSSPJa3qn9wWXfwnWnY";
//char* remote_ip = "10.211.55.4";
char* hello_world_hash = "QmTUFTVgkHT3Qdd9ospVjSLi2upd6VdkeNXZQH66cVmzja";
/*
@ -317,6 +319,16 @@ int test_bitswap_retrieve_file_known_remote() {
goto exit;
}
if (result == NULL) {
libp2p_logger_error("test_bitswap", "GetBlock returned NULL");
goto exit;
}
if (result->cid == NULL) {
libp2p_logger_error("test_bitswap", "GetBlock returned an object with no CID");
goto exit;
}
if (cid->hash_length != result->cid->hash_length) {
libp2p_logger_error("test_bitswap", "Node hash sizes do not match. Should be %lu but is %lu\n", strlen(hello_world_hash), result->cid->hash_length);
goto exit;
@ -335,6 +347,7 @@ int test_bitswap_retrieve_file_known_remote() {
ipfs_block_free(result);
if (cid != NULL)
ipfs_cid_free(cid);
ipfs_node_free(ipfs_node2);
return retVal;
}