Skip to contentSkip to conversation

perf tools: fix thread_map__new_by_pid_str() memory leak in error path

PATCH Applied Franck Bui-Huu 2 participants 3 messages May 25, 2012 – May 30, 2012 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf tools: fix thread_map__new_by_pid_str() memory leak in error path

namelist array (including its content) was not freed if we fail to realloc a new 'threads' structure.

---
 tools/perf/util/thread_map.c |   21 ++++++++++-----------
 1 files changed, 10 insertions(+), 11 deletions(-)
tools/perf/util/thread_map.c +10 -11
index 84d9bd78..9b5f856 100644
@@ -188,28 +188,27 @@ static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
 		nt = realloc(threads, (sizeof(*threads) +
 				       sizeof(pid_t) * total_tasks));
 		if (nt == NULL)
-			goto out_free_threads;
+			goto out_free_namelist;
 
 		threads = nt;
 
-		if (threads) {
-			for (i = 0; i < items; i++)
-				threads->map[j++] = atoi(namelist[i]->d_name);
-			threads->nr = total_tasks;
-		}
-
-		for (i = 0; i < items; i++)
+		for (i = 0; i < items; i++) {
+			threads->map[j++] = atoi(namelist[i]->d_name);
 			free(namelist[i]);
+		}
+		threads->nr = total_tasks;
 		free(namelist);
-
-		if (!threads)
-			break;
 	}
 
 out:
 	strlist__delete(slist);
 	return threads;
 
+out_free_namelist:
+	for (i = 0; i < items; i++)
+		free(namelist[i]);
+	free(namelist);
+
 out_free_threads:
 	free(threads);
 	threads = NULL;
Signature · 1 line
1.7.7.4
#2 lore raw
In reply to Franck Bui-Huu
Commit-ID:  e8cdd947776300f962d5b699c34087af45a8aea7
Gitweb:     http://git.kernel.org/tip/e8cdd947776300f962d5b699c34087af45a8aea7
Author:     Franck Bui-Huu <[email protected]>
AuthorDate: Fri, 25 May 2012 15:21:49 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 25 May 2012 11:40:59 -0300

perf tools: fix thread_map__new_by_pid_str() memory leak in error path

The namelist array (including its content) was not freed if we fail to realloc a new 'threads' structure.

---
 tools/perf/util/thread_map.c |   21 ++++++++++-----------
 1 files changed, 10 insertions(+), 11 deletions(-)
tools/perf/util/thread_map.c +10 -11
index 84d9bd78..9b5f856 100644
@@ -188,28 +188,27 @@ static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
 		nt = realloc(threads, (sizeof(*threads) +
 				       sizeof(pid_t) * total_tasks));
 		if (nt == NULL)
-			goto out_free_threads;
+			goto out_free_namelist;
 
 		threads = nt;
 
-		if (threads) {
-			for (i = 0; i < items; i++)
-				threads->map[j++] = atoi(namelist[i]->d_name);
-			threads->nr = total_tasks;
-		}
-
-		for (i = 0; i < items; i++)
+		for (i = 0; i < items; i++) {
+			threads->map[j++] = atoi(namelist[i]->d_name);
 			free(namelist[i]);
+		}
+		threads->nr = total_tasks;
 		free(namelist);
-
-		if (!threads)
-			break;
 	}
 
 out:
 	strlist__delete(slist);
 	return threads;
 
+out_free_namelist:
+	for (i = 0; i < items; i++)
+		free(namelist[i]);
+	free(namelist);
+
 out_free_threads:
 	free(threads);
 	threads = NULL;