Skip to contentSkip to conversation

tools/perf/util: Fix memory leak in trace-event-info.c

PATCH Superseded Felipe Pena 2 participants 2 messages Oct 11, 2013 – Oct 14, 2013 perf events

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tools/perf/util: Fix memory leak in trace-event-info.c

Fix for a memory leak on tracing_data_get() function when returning NULL explicitly

---
 tools/perf/util/trace-event-info.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/perf/util/trace-event-info.c +5 -2
index f3c9e55..2a0c3c9 100644
@@ -518,13 +518,15 @@ struct tracing_data *tracing_data_get(struct list_head *pattrs,
 			 "/tmp/perf-XXXXXX");
 		if (!mkstemp(tdata->temp_file)) {
 			pr_debug("Can't make temp file");
-			return NULL;
+			err = -1;
+			goto err_tdata;
 		}
 
 		temp_fd = open(tdata->temp_file, O_RDWR);
 		if (temp_fd < 0) {
 			pr_debug("Can't read '%s'", tdata->temp_file);
-			return NULL;
+			err = -1;
+			goto err_tdata;
 		}
 
 		/*
@@ -562,6 +564,7 @@ out:
 		output_fd = fd;
 	}
 
+err_tdata:
 	if (err) {
 		free(tdata);
 		tdata = NULL;

--
1.7.10.4

maintainer
#1 lore raw

Hi Felipe,

Quoted 16 lines · Felipe Pena

Fix for a memory leak on tracing_data_get() function when returning NULL explicitly

---
 tools/perf/util/trace-event-info.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/perf/util/trace-event-info.c +0 -1
index f3c9e55..2a0c3c9 100644
@@ -518,13 +518,15 @@ struct tracing_data *tracing_data_get(struct list_head *pattrs,
 			 "/tmp/perf-XXXXXX");
 		if (!mkstemp(tdata->temp_file)) {
 			pr_debug("Can't make temp file");
-			return NULL;

tools/perf/util/trace-event-info.c struct tracing_data *tracing_data_get(struct list_head *pattrs, · line 529

Quoted 10 lines
tools/perf/util/trace-event-info.c +4 -1
+			err = -1;
+			goto err_tdata;
 		}
 
 		temp_fd = open(tdata->temp_file, O_RDWR);
 		if (temp_fd < 0) {
 			pr_debug("Can't read '%s'", tdata->temp_file);
-			return NULL;
+			err = -1;
+			goto err_tdata;

I'd rather initialize err to -1 at the beginning. And the malloc of tdata should also be handled like this.

Thanks,
Namhyung

Quoted 13 lines
tools/perf/util/trace-event-info.c +1 -1
 		}
 
 		/*
@@ -562,6 +564,7 @@ out:
 		output_fd = fd;
 	}
 
+err_tdata:
 	if (err) {
 		free(tdata);
 		tdata = NULL;
--

1.7.10.4