Minor memory leak fixes

This commit is contained in:
John Jones 2017-09-28 15:43:03 -05:00
parent 7aa81936ec
commit 27d36d8320
5 changed files with 25 additions and 7 deletions

View file

@ -511,7 +511,7 @@ void *api_connection_thread (void *ptr)
// 404 // 404
write_str(s, HTTP_404); write_str(s, HTTP_404);
} else { } else {
snprintf(resp, sizeof(resp), "%s 200 OK\r\n" \ snprintf(resp, MAX_READ+1, "%s 200 OK\r\n" \
"Content-Type: %s\r\n" "Content-Type: %s\r\n"
"Server: c-ipfs/0.0.0-dev\r\n" "Server: c-ipfs/0.0.0-dev\r\n"
"X-Chunked-Output: 1\r\n" "X-Chunked-Output: 1\r\n"
@ -522,11 +522,11 @@ void *api_connection_thread (void *ptr)
"%s\r\n" "%s\r\n"
"0\r\n\r\n" "0\r\n\r\n"
,req.buf + req.http_ver, http_response->content_type, (unsigned int)http_response->bytes_size, http_response->bytes); ,req.buf + req.http_ver, http_response->content_type, (unsigned int)http_response->bytes_size, http_response->bytes);
ipfs_core_http_response_free(http_response);
write_str (s, resp); write_str (s, resp);
libp2p_logger_debug("api", "resp = {\n%s\n}\n", resp); libp2p_logger_debug("api", "resp = {\n%s\n}\n", resp);
} }
ipfs_core_http_request_free(http_request); ipfs_core_http_request_free(http_request);
ipfs_core_http_response_free(http_response);
} else { } else {
// uh oh... something went wrong converting to the HttpRequest struct // uh oh... something went wrong converting to the HttpRequest struct
libp2p_logger_error("api", "Unable to build HttpRequest struct.\n"); libp2p_logger_error("api", "Unable to build HttpRequest struct.\n");
@ -686,6 +686,8 @@ int api_start (struct IpfsNode* local_node, int max_conns, int timeout)
} }
local_node->api_context->ipv4 = hostname_to_ip(ip); // api is listening only on loopback. local_node->api_context->ipv4 = hostname_to_ip(ip); // api is listening only on loopback.
if (ip != NULL)
free(ip);
local_node->api_context->port = port; local_node->api_context->port = port;
if ((s = socket_listen(socket_tcp4(), &(local_node->api_context->ipv4), &(local_node->api_context->port))) <= 0) { if ((s = socket_listen(socket_tcp4(), &(local_node->api_context->ipv4), &(local_node->api_context->port))) <= 0) {

View file

@ -28,6 +28,8 @@ int api_running(struct IpfsNode* local_node) {
portno = multiaddress_get_ip_port(my_multiaddress); portno = multiaddress_get_ip_port(my_multiaddress);
multiaddress_get_ip_address(my_multiaddress, &ip); multiaddress_get_ip_address(my_multiaddress, &ip);
multiaddress_free(my_multiaddress);
if (ip == NULL) if (ip == NULL)
return 0; return 0;

View file

@ -48,10 +48,9 @@ void ipfs_core_http_request_free(struct HttpRequest* request) {
libp2p_utils_vector_free(request->params); libp2p_utils_vector_free(request->params);
} }
if (request->arguments != NULL) { if (request->arguments != NULL) {
// arguments should not be dynamically allocated for(int i = 0; i < request->arguments->total; i++) {
//for(int i = 0; i < request->arguments->total; i++) { free((char*)libp2p_utils_vector_get(request->arguments, i));
// free((char*)libp2p_utils_vector_get(request->arguments, i)); }
//}
libp2p_utils_vector_free(request->arguments); libp2p_utils_vector_free(request->arguments);
} }
free(request); free(request);
@ -178,13 +177,18 @@ int ipfs_core_http_process_dht_provide(struct IpfsNode* local_node, struct HttpR
char* hash = (char*)libp2p_utils_vector_get(request->arguments, i); char* hash = (char*)libp2p_utils_vector_get(request->arguments, i);
struct Cid* cid; struct Cid* cid;
if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hash, strlen(hash), &cid)) { if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hash, strlen(hash), &cid)) {
ipfs_cid_free(cid);
cid = NULL;
failedCount++; failedCount++;
continue; continue;
} }
if (!local_node->routing->Provide(local_node->routing, cid->hash, cid->hash_length)) { if (!local_node->routing->Provide(local_node->routing, cid->hash, cid->hash_length)) {
ipfs_cid_free(cid);
cid = NULL;
failedCount++; failedCount++;
continue; continue;
} }
ipfs_cid_free(cid);
} }
*response = ipfs_core_http_response_new(); *response = ipfs_core_http_response_new();
struct HttpResponse* res = *response; struct HttpResponse* res = *response;
@ -241,13 +245,18 @@ int ipfs_core_http_process_dht_get(struct IpfsNode* local_node, struct HttpReque
char* hash = (char*)libp2p_utils_vector_get(request->arguments, i); char* hash = (char*)libp2p_utils_vector_get(request->arguments, i);
struct Cid* cid; struct Cid* cid;
if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hash, strlen(hash), &cid)) { if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hash, strlen(hash), &cid)) {
ipfs_cid_free(cid);
cid = NULL;
failedCount++; failedCount++;
continue; continue;
} }
if (!local_node->routing->GetValue(local_node->routing, cid->hash, cid->hash_length, (void**)&res->bytes, &res->bytes_size)) { if (!local_node->routing->GetValue(local_node->routing, cid->hash, cid->hash_length, (void**)&res->bytes, &res->bytes_size)) {
ipfs_cid_free(cid);
cid = NULL;
failedCount++; failedCount++;
continue; continue;
} }
ipfs_cid_free(cid);
//TODO: we need to handle multiple arguments //TODO: we need to handle multiple arguments
} }
return failedCount < request->arguments->total; return failedCount < request->arguments->total;

View file

@ -70,8 +70,10 @@ int ipfs_routing_generic_get_value (ipfs_routing* routing, const unsigned char *
libp2p_utils_vector_add(req->arguments, buffer); libp2p_utils_vector_add(req->arguments, buffer);
if (!ipfs_core_http_request_get(routing->local_node, req, &response)) { if (!ipfs_core_http_request_get(routing->local_node, req, &response)) {
libp2p_logger_error("offline", "Unable to call API for dht get.\n"); libp2p_logger_error("offline", "Unable to call API for dht get.\n");
ipfs_core_http_request_free(req);
return 0; return 0;
} }
ipfs_core_http_request_free(req);
*vlen = strlen(response); *vlen = strlen(response);
if (*vlen > 0) { if (*vlen > 0) {
*val = malloc(*vlen + 1); *val = malloc(*vlen + 1);
@ -142,12 +144,13 @@ int ipfs_routing_offline_provide (ipfs_routing* offlineRouting, const unsigned c
struct HttpRequest* request = ipfs_core_http_request_new(); struct HttpRequest* request = ipfs_core_http_request_new();
request->command = "dht"; request->command = "dht";
request->sub_command = "provide"; request->sub_command = "provide";
request->arguments = libp2p_utils_vector_new(1);
libp2p_utils_vector_add(request->arguments, buffer); libp2p_utils_vector_add(request->arguments, buffer);
if (!ipfs_core_http_request_get(offlineRouting->local_node, request, &response)) { if (!ipfs_core_http_request_get(offlineRouting->local_node, request, &response)) {
libp2p_logger_error("offline", "Unable to call API for dht publish.\n"); libp2p_logger_error("offline", "Unable to call API for dht publish.\n");
ipfs_core_http_request_free(request);
return 0; return 0;
} }
ipfs_core_http_request_free(request);
fprintf(stdout, "%s", response); fprintf(stdout, "%s", response);
return 1; return 1;
} else { } else {

View file

@ -257,6 +257,8 @@ int test_routing_find_peer() {
pthread_join(thread1, NULL); pthread_join(thread1, NULL);
if (thread2_started) if (thread2_started)
pthread_join(thread2, NULL); pthread_join(thread2, NULL);
if (thread3_started)
pthread_join(thread3, NULL);
if (peer_id_1 != NULL) if (peer_id_1 != NULL)
free(peer_id_1); free(peer_id_1);
if (peer_id_2 != NULL) if (peer_id_2 != NULL)