From 27d36d8320164aa448cdd39a796e9993b24cfbea Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 28 Sep 2017 15:43:03 -0500 Subject: [PATCH] Minor memory leak fixes --- core/api.c | 6 ++++-- core/client_api.c | 2 ++ core/http_request.c | 17 +++++++++++++---- routing/offline.c | 5 ++++- test/routing/test_routing.h | 2 ++ 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/core/api.c b/core/api.c index ec7d76f..8e10a2d 100644 --- a/core/api.c +++ b/core/api.c @@ -511,7 +511,7 @@ void *api_connection_thread (void *ptr) // 404 write_str(s, HTTP_404); } 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" "Server: c-ipfs/0.0.0-dev\r\n" "X-Chunked-Output: 1\r\n" @@ -522,11 +522,11 @@ void *api_connection_thread (void *ptr) "%s\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); - ipfs_core_http_response_free(http_response); write_str (s, resp); libp2p_logger_debug("api", "resp = {\n%s\n}\n", resp); } ipfs_core_http_request_free(http_request); + ipfs_core_http_response_free(http_response); } else { // uh oh... something went wrong converting to the HttpRequest struct 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. + if (ip != NULL) + free(ip); local_node->api_context->port = port; if ((s = socket_listen(socket_tcp4(), &(local_node->api_context->ipv4), &(local_node->api_context->port))) <= 0) { diff --git a/core/client_api.c b/core/client_api.c index deac260..da1af47 100644 --- a/core/client_api.c +++ b/core/client_api.c @@ -28,6 +28,8 @@ int api_running(struct IpfsNode* local_node) { portno = multiaddress_get_ip_port(my_multiaddress); multiaddress_get_ip_address(my_multiaddress, &ip); + multiaddress_free(my_multiaddress); + if (ip == NULL) return 0; diff --git a/core/http_request.c b/core/http_request.c index be43c05..b7f8b98 100644 --- a/core/http_request.c +++ b/core/http_request.c @@ -48,10 +48,9 @@ void ipfs_core_http_request_free(struct HttpRequest* request) { libp2p_utils_vector_free(request->params); } if (request->arguments != NULL) { - // arguments should not be dynamically allocated - //for(int i = 0; i < request->arguments->total; i++) { - // free((char*)libp2p_utils_vector_get(request->arguments, i)); - //} + for(int i = 0; i < request->arguments->total; i++) { + free((char*)libp2p_utils_vector_get(request->arguments, i)); + } libp2p_utils_vector_free(request->arguments); } 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); struct Cid* cid; if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hash, strlen(hash), &cid)) { + ipfs_cid_free(cid); + cid = NULL; failedCount++; continue; } if (!local_node->routing->Provide(local_node->routing, cid->hash, cid->hash_length)) { + ipfs_cid_free(cid); + cid = NULL; failedCount++; continue; } + ipfs_cid_free(cid); } *response = ipfs_core_http_response_new(); 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); struct Cid* cid; if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hash, strlen(hash), &cid)) { + ipfs_cid_free(cid); + cid = NULL; failedCount++; continue; } 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++; continue; } + ipfs_cid_free(cid); //TODO: we need to handle multiple arguments } return failedCount < request->arguments->total; diff --git a/routing/offline.c b/routing/offline.c index 9d3f4f3..c8d0028 100644 --- a/routing/offline.c +++ b/routing/offline.c @@ -70,8 +70,10 @@ int ipfs_routing_generic_get_value (ipfs_routing* routing, const unsigned char * libp2p_utils_vector_add(req->arguments, buffer); if (!ipfs_core_http_request_get(routing->local_node, req, &response)) { libp2p_logger_error("offline", "Unable to call API for dht get.\n"); + ipfs_core_http_request_free(req); return 0; } + ipfs_core_http_request_free(req); *vlen = strlen(response); if (*vlen > 0) { *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(); request->command = "dht"; request->sub_command = "provide"; - request->arguments = libp2p_utils_vector_new(1); libp2p_utils_vector_add(request->arguments, buffer); if (!ipfs_core_http_request_get(offlineRouting->local_node, request, &response)) { libp2p_logger_error("offline", "Unable to call API for dht publish.\n"); + ipfs_core_http_request_free(request); return 0; } + ipfs_core_http_request_free(request); fprintf(stdout, "%s", response); return 1; } else { diff --git a/test/routing/test_routing.h b/test/routing/test_routing.h index 38e305e..bf23227 100644 --- a/test/routing/test_routing.h +++ b/test/routing/test_routing.h @@ -257,6 +257,8 @@ int test_routing_find_peer() { pthread_join(thread1, NULL); if (thread2_started) pthread_join(thread2, NULL); + if (thread3_started) + pthread_join(thread3, NULL); if (peer_id_1 != NULL) free(peer_id_1); if (peer_id_2 != NULL)