From 8539125677fb0761db1fa2e4b2118eddd5765868 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Fri, 20 Dec 2019 01:39:17 -0500 Subject: [PATCH] Fix: lttng-clear: invalid free of session name MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Valgrind reports an invalid free of session_name when it is sourced from the popt args. Only free it when it comes from the .lttngrc file. This also caused spurious core dumps when running the tests on one of my machines. Signed-off-by: Jérémie Galarneau Change-Id: I194cd36147a1028aad503b4e5d3ea23732a30bc5 --- src/bin/lttng/commands/clear.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/bin/lttng/commands/clear.c b/src/bin/lttng/commands/clear.c index 353c855c6..24db9f37e 100644 --- a/src/bin/lttng/commands/clear.c +++ b/src/bin/lttng/commands/clear.c @@ -168,7 +168,7 @@ int cmd_clear(int argc, const char **argv) static poptContext pc; char *session_name = NULL; const char *leftover = NULL; - + bool free_session_name = false; struct lttng_session *sessions = NULL; int count; int found; @@ -224,13 +224,7 @@ int cmd_clear(int argc, const char **argv) } if (!opt_clear_all) { - /* - * popt expects us to free this even if it returns a const char *. - * See https://www.mail-archive.com/popt-devel@rpm5.org/msg00193.html - * Force cast to char * allowing later freeing if necessary. - */ session_name = (char *) poptGetArg(pc); - if (!session_name) { /* No session name specified, lookup default */ session_name = get_session_name(); @@ -239,6 +233,7 @@ int cmd_clear(int argc, const char **argv) success = 0; goto mi_closing; } + free_session_name = true; } } else { session_name = NULL; @@ -323,7 +318,9 @@ end: } free(sessions); - free(session_name); + if (free_session_name) { + free(session_name); + } /* Overwrite ret if an error occurred during clear_session/all */ ret = command_ret ? command_ret : ret; -- 2.34.1