From 9e8e77dba3462c4a7f7e758ade4d16bc669fc4a7 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 4 Oct 2019 21:10:10 +0200 Subject: [PATCH] Clean up resources in the test cases. Not doing so prevents tools such as AddressSanitizer and Valgrind from declaring the tests free of memory leaks. --- test/channels-cornercases.c | 5 +++++ test/channels-udp.c | 1 + test/invite-join.c | 1 + test/trio.c | 10 +++++----- test/trio2.c | 10 +++++----- test/utils.c | 6 ++++++ 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/test/channels-cornercases.c b/test/channels-cornercases.c index d7ef274c..f2a01ba0 100644 --- a/test/channels-cornercases.c +++ b/test/channels-cornercases.c @@ -123,6 +123,7 @@ int main() { assert(wait_sync_flag(&channel_opened, 20)); // Re-initialize everything + meshlink_channel_close(a, channel); close_meshlink_pair(a, b, "channels-cornercases"); b_responded = false; b_closed = false; @@ -172,5 +173,9 @@ int main() { assert(0 == a_poll_cb_len); + meshlink_channel_close(a, channel); + meshlink_channel_close(a, channel2); + close_meshlink_pair(a, b, "channels-cornercases"); + return 0; } diff --git a/test/channels-udp.c b/test/channels-udp.c index 8b9e163f..383c824e 100644 --- a/test/channels-udp.c +++ b/test/channels-udp.c @@ -155,6 +155,7 @@ int main() { // Stream packets from server to clients for 5 seconds at 40 Mbps (1 kB * 500 Hz) char data[1000]; + memset(data, 'U', sizeof(data)); for(int j = 0; j < 2500; j++) { for(int i = 0; i < 3; i++) { diff --git a/test/invite-join.c b/test/invite-join.c index f76dc536..dbf55a22 100644 --- a/test/invite-join.c +++ b/test/invite-join.c @@ -219,6 +219,7 @@ int main() { // The invitation should work assert(meshlink_join(mesh3, quux_url)); + free(quux_url); // Clean up. diff --git a/test/trio.c b/test/trio.c index 1dc33aa4..41aa4fb6 100644 --- a/test/trio.c +++ b/test/trio.c @@ -1,6 +1,7 @@ #define _GNU_SOURCE #include +#include #include #include #include @@ -52,14 +53,12 @@ int main() { char *data[3]; for(int i = 0; i < 3; i++) { - char *path; - int ret_val; - (void)ret_val; - ret_val = asprintf(&path, "trio_conf.%d", i); - assert(path); + char *path = NULL; + assert(asprintf(&path, "trio_conf.%d", i) != -1 && path); mesh[i] = meshlink_open(path, name[i], "trio", DEV_CLASS_BACKBONE); assert(mesh[i]); + free(path); meshlink_add_address(mesh[i], "localhost"); @@ -104,6 +103,7 @@ int main() { devtool_edge_t *edges = NULL; size_t nedges = 0; assert_after((edges = devtool_get_all_edges(mesh[1], edges, &nedges), nedges == 3), 15); + free(edges); // Stop the first node diff --git a/test/trio2.c b/test/trio2.c index 299a5582..b55fde73 100644 --- a/test/trio2.c +++ b/test/trio2.c @@ -1,6 +1,7 @@ #define _GNU_SOURCE #include +#include #include #include #include @@ -52,14 +53,12 @@ int main() { char *data[3]; for(int i = 0; i < 3; i++) { - char *path; - int ret_val; - (void)ret_val; - ret_val = asprintf(&path, "trio2_conf.%d", i); - assert(path); + char *path = NULL; + assert(asprintf(&path, "trio2_conf.%d", i) != -1 && path); mesh[i] = meshlink_open(path, name[i], "trio2", DEV_CLASS_BACKBONE); assert(mesh[i]); + free(path); meshlink_add_address(mesh[i], "localhost"); @@ -101,6 +100,7 @@ int main() { devtool_edge_t *edges = NULL; size_t nedges = 0; assert_after((edges = devtool_get_all_edges(mesh[1], edges, &nedges), nedges == 3), 15); + free(edges); // Stop the nodes nodes diff --git a/test/utils.c b/test/utils.c index 21d3b149..46e147a5 100644 --- a/test/utils.c +++ b/test/utils.c @@ -79,6 +79,9 @@ void open_meshlink_pair(meshlink_handle_t **pa, meshlink_handle_t **pb, const ch meshlink_handle_t *b = meshlink_open(b_name, "b", prefix, DEV_CLASS_BACKBONE); assert(b); + free(a_name); + free(b_name); + meshlink_enable_discovery(a, false); meshlink_enable_discovery(b, false); @@ -131,5 +134,8 @@ void close_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b, const char ret_val = asprintf(&b_name, "%s_conf.2", prefix); assert(b_name); assert(meshlink_destroy(b_name)); + + free(a_name); + free(b_name); } } -- 2.39.5