[ltt-dev] [lttng-tools PATCH] lttng command line UI: fix allocation/free

Mathieu Desnoyers mathieu.desnoyers at efficios.com
Sat Jul 16 12:06:21 EDT 2011


Freeing non-dynamically allocated memory is never a good idea.
Also fixing 2 memory leaks.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers at efficios.com>
---
 lttng/commands/destroy.c        |    4 +++-
 lttng/commands/disable_events.c |    5 ++++-
 lttng/commands/enable_events.c  |    5 ++++-
 lttng/commands/start.c          |    4 +++-
 lttng/commands/stop.c           |    4 +++-
 lttng/utils.c                   |    9 +++++----
 6 files changed, 22 insertions(+), 9 deletions(-)

Index: lttng-tools/lttng/commands/destroy.c
===================================================================
--- lttng-tools.orig/lttng/commands/destroy.c
+++ lttng-tools/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;
 }
Index: lttng-tools/lttng/utils.c
===================================================================
--- lttng-tools.orig/lttng/utils.c
+++ lttng-tools/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;
 
Index: lttng-tools/lttng/commands/disable_events.c
===================================================================
--- lttng-tools.orig/lttng/commands/disable_events.c
+++ lttng-tools/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;
 }
 
Index: lttng-tools/lttng/commands/enable_events.c
===================================================================
--- lttng-tools.orig/lttng/commands/enable_events.c
+++ lttng-tools/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;
 }
 
Index: lttng-tools/lttng/commands/start.c
===================================================================
--- lttng-tools.orig/lttng/commands/start.c
+++ lttng-tools/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;
 }
Index: lttng-tools/lttng/commands/stop.c
===================================================================
--- lttng-tools.orig/lttng/commands/stop.c
+++ lttng-tools/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;
 }

-- 
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com




More information about the lttng-dev mailing list