From 031ebcf6065bccc6279a6745676b0e4b9f7fda80 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 19 Jul 2019 14:44:29 +0200 Subject: [PATCH] Fix a small memory leak. When registering ourselves to Catta, we called meshlink_get_fingerprint() without freeing the returned string afterwards. --- src/discovery.c | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/discovery.c b/src/discovery.c index 4aa34d43..67a34949 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -1,3 +1,5 @@ +#include "system.h" + #include #include #include @@ -14,10 +16,6 @@ #include "node.h" #include "connection.h" -#include - -#include - #define MESHLINK_MDNS_SERVICE_TYPE "_%s._tcp" #define MESHLINK_MDNS_NAME_KEY "name" #define MESHLINK_MDNS_FINGERPRINT_KEY "fingerprint" @@ -70,7 +68,9 @@ static void discovery_entry_group_callback(CattaServer *server, CattaSEntryGroup static void discovery_create_services(meshlink_handle_t *mesh) { + char *fingerprint = NULL; char *txt_name = NULL; + char *txt_fingerprint = NULL; // asserts assert(mesh != NULL); @@ -94,23 +94,14 @@ static void discovery_create_services(meshlink_handle_t *mesh) { } /* Create txt records */ - size_t txt_name_len = sizeof(MESHLINK_MDNS_NAME_KEY) + 1 + strlen(mesh->name) + 1; - txt_name = malloc(txt_name_len); - - if(txt_name == NULL) { - logger(mesh, MESHLINK_ERROR, "Could not allocate memory for TXT record\n"); - goto fail; - } - - snprintf(txt_name, txt_name_len, "%s=%s", MESHLINK_MDNS_NAME_KEY, mesh->name); - - char txt_fingerprint[sizeof(MESHLINK_MDNS_FINGERPRINT_KEY) + 1 + MESHLINK_FINGERPRINTLEN + 1]; - snprintf(txt_fingerprint, sizeof(txt_fingerprint), "%s=%s", MESHLINK_MDNS_FINGERPRINT_KEY, meshlink_get_fingerprint(mesh, (meshlink_node_t *)mesh->self)); + fingerprint = meshlink_get_fingerprint(mesh, (meshlink_node_t *)mesh->self); + xasprintf(&txt_name, "%s=%s", MESHLINK_MDNS_NAME_KEY, mesh->name); + xasprintf(&txt_fingerprint, "%s=%s", MESHLINK_MDNS_FINGERPRINT_KEY, fingerprint); /* Add the service */ int ret = 0; - if((ret = catta_server_add_service(mesh->catta_server, mesh->catta_group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0, meshlink_get_fingerprint(mesh, (meshlink_node_t *)mesh->self), mesh->catta_servicetype, NULL, NULL, atoi(mesh->myport), txt_name, txt_fingerprint, NULL)) < 0) { + if((ret = catta_server_add_service(mesh->catta_server, mesh->catta_group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0, fingerprint, mesh->catta_servicetype, NULL, NULL, atoi(mesh->myport), txt_name, txt_fingerprint, NULL)) < 0) { logger(mesh, MESHLINK_ERROR, "Failed to add service: %s\n", catta_strerror(ret)); goto fail; } @@ -127,10 +118,9 @@ fail: catta_simple_poll_quit(mesh->catta_poll); done: - - if(txt_name) { - free(txt_name); - } + free(fingerprint); + free(txt_name); + free(txt_fingerprint); pthread_mutex_unlock(&(mesh->mesh_mutex)); } -- 2.39.2