From: Mathieu Desnoyers Date: Sat, 16 Jul 2011 16:06:21 +0000 (-0400) Subject: lttng command line UI: fix allocation/free X-Git-Tag: v2.0-pre1~33 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=b73d0b29491b7d0dc5cde3c4c65a6cd7c80479b4;p=lttng-tools.git lttng command line UI: fix allocation/free Freeing non-dynamically allocated memory is never a good idea. Also fixing 2 memory leaks. Signed-off-by: Mathieu Desnoyers --- diff --git a/lttng/commands/destroy.c b/lttng/commands/destroy.c index d7c01097f..743d89978 100644 --- a/lttng/commands/destroy.c +++ b/lttng/commands/destroy.c @@ -97,7 +97,9 @@ static int destroy_session() ret = CMD_SUCCESS; free_name: - free(session_name); + if (opt_session_name == NULL) { + free(session_name); + } error: return ret; } diff --git a/lttng/commands/disable_events.c b/lttng/commands/disable_events.c index 04a8c0a62..3cd130e57 100644 --- a/lttng/commands/disable_events.c +++ b/lttng/commands/disable_events.c @@ -83,7 +83,7 @@ static void usage(FILE *ofp) static int disable_events(void) { int err, ret = CMD_SUCCESS; - char *event_name, *channel_name; + char *event_name, *channel_name = NULL; struct lttng_event ev; if (set_session_name(opt_session_name) < 0) { @@ -147,6 +147,9 @@ static int disable_events(void) } error: + if (opt_channel_name == NULL) { + free(channel_name); + } return ret; } diff --git a/lttng/commands/enable_events.c b/lttng/commands/enable_events.c index d4af4b3d3..718e4b3c3 100644 --- a/lttng/commands/enable_events.c +++ b/lttng/commands/enable_events.c @@ -152,7 +152,7 @@ error: static int enable_events(void) { int err, ret = CMD_SUCCESS; - char *event_name, *channel_name; + char *event_name, *channel_name = NULL; struct lttng_event ev; if (set_session_name(opt_session_name) < 0) { @@ -235,6 +235,9 @@ static int enable_events(void) } error: + if (opt_channel_name == NULL) { + free(channel_name); + } return ret; } diff --git a/lttng/commands/start.c b/lttng/commands/start.c index 1a3e568ed..e9423170e 100644 --- a/lttng/commands/start.c +++ b/lttng/commands/start.c @@ -85,7 +85,9 @@ static int start_tracing(void) MSG("Tracing started for session %s", session_name); free_name: - free(session_name); + if (opt_session_name == NULL) { + free(session_name); + } error: return ret; } diff --git a/lttng/commands/stop.c b/lttng/commands/stop.c index 56edc2d6d..664637873 100644 --- a/lttng/commands/stop.c +++ b/lttng/commands/stop.c @@ -83,7 +83,9 @@ static int stop_tracing(void) MSG("Tracing stopped for session %s", session_name); free_name: - free(session_name); + if (opt_session_name == NULL) { + free(session_name); + } error: return ret; } diff --git a/lttng/utils.c b/lttng/utils.c index 2cb77568e..1b2138ced 100644 --- a/lttng/utils.c +++ b/lttng/utils.c @@ -58,18 +58,19 @@ int set_session_name(char *name) int ret; char *session_name; - if (name != NULL) { - session_name = name; - } else { + if (!name) { session_name = get_session_name(); if (session_name == NULL) { ret = -1; goto error; } + } else { + session_name = name; } lttng_set_session_name(session_name); - free(session_name); + if (!name) + free(session_name); ret = 0;