From 8e030dfbea87944a5d9a054116f8323d436d06a3 Mon Sep 17 00:00:00 2001 From: Martin Kaiser Date: Sun, 20 Dec 2020 19:56:36 +0100 Subject: [PATCH] tap-simple_stattable: fix a memory leak for tshark -z Displaying statistics with tshark results in a memory leak. tshark -r -z dhcp,stat -q ==26971==ERROR: LeakSanitizer: detected memory leaks Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x7f89a4bae518 in calloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9518) #1 0x7f8989af2918 in g_malloc0 (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x53918) init_stat_table allocates a table_stat_t. This is used as private data while the tap listener is running but it's not freed afterwards. This patch adds a finish callback for the tap listener where the table_stat_t is freed. --- ui/cli/tap-simple_stattable.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ui/cli/tap-simple_stattable.c b/ui/cli/tap-simple_stattable.c index ee8ec79e79..014d0c908a 100644 --- a/ui/cli/tap-simple_stattable.c +++ b/ui/cli/tap-simple_stattable.c @@ -89,6 +89,13 @@ simple_draw(void *arg) printf("=====================================================================================================\n"); } +static void simple_finish(void *tapdata) +{ + stat_data_t *stat_data = (stat_data_t *)tapdata; + + g_free(stat_data->user_data); +} + static void init_stat_table(stat_tap_table_ui *stat_tap, const char *filter) { @@ -102,7 +109,9 @@ init_stat_table(stat_tap_table_ui *stat_tap, const char *filter) stat_tap->stat_tap_init_cb(stat_tap); - error_string = register_tap_listener(stat_tap->tap_name, &ui->stats, filter, 0, NULL, stat_tap->packet_func, simple_draw, NULL); + error_string = register_tap_listener(stat_tap->tap_name, &ui->stats, + filter, 0, NULL, stat_tap->packet_func, simple_draw, + simple_finish); if (error_string) { /* free_rtd_table(&ui->rtd.stat_table); */ cmdarg_err("Couldn't register tap: %s", error_string->str);