Skip to contentSkip to conversation

perf tools: fix resource leak of closedir() on the error paths

PATCH Applied Yunfeng Ye 4 participants 4 messages Oct 15, 2019 – Oct 21, 2019

Subsystems (4)
lore
Share & tools
lore

Yunfeng Ye

author
#0 lore raw

PATCH 1/1 perf tools: fix resource leak of closedir() on the error paths

Both build_mem_topology() and rm_rf_depth_pat() have resource leak of closedir() on the error paths.

Fix this by calling closedir() before function returns.

Fixes e2091cedd51b ("perf tools: Add MEM_TOPOLOGY feature to perf data file")
Fixes cdb6b0235f17 ("perf tools: Add pattern name checking to rm_rf")
---
 tools/perf/util/header.c | 4 +++-
 tools/perf/util/util.c   | 6 ++++--
 2 files changed, 7 insertions(+), 3 deletions(-)
tools/perf/util/header.c +3 -1
index 86d9396..becc2d1 100644
@@ -1296,8 +1296,10 @@ static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
 			continue;
 
 		if (WARN_ONCE(cnt >= size,
-			      "failed to write MEM_TOPOLOGY, way too many nodes\n"))
+			"failed to write MEM_TOPOLOGY, way too many nodes\n")) {
+			closedir(dir);
 			return -1;
+		}
 
 		ret = memory_node__read(&nodes[cnt++], idx);
 	}
tools/perf/util/util.c +4 -2
index 5eda6e1..ae56c76 100644
@@ -154,8 +154,10 @@ static int rm_rf_depth_pat(const char *path, int depth, const char **pat)
 		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
 			continue;
 
-		if (!match_pat(d->d_name, pat))
-			return -2;
+		if (!match_pat(d->d_name, pat)) {
+			ret =  -2;
+			break;
+		}
 
 		scnprintf(namebuf, sizeof(namebuf), "%s/%s",
 			  path, d->d_name);
Signature · 1 line
2.7.4.huawei.3
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Yunfeng Ye

Both build_mem_topology() and rm_rf_depth_pat() have resource leak of closedir() on the error paths.

Fix this by calling closedir() before function returns.

Fixes e2091cedd51b ("perf tools: Add MEM_TOPOLOGY feature to perf data file")
Fixes cdb6b0235f17 ("perf tools: Add pattern name checking to rm_rf")

guilty as charged ;-)

thanks,
jirka

Quoted 42 lines
---
 tools/perf/util/header.c | 4 +++-
 tools/perf/util/util.c   | 6 ++++--
 2 files changed, 7 insertions(+), 3 deletions(-)
tools/perf/util/header.c +3 -1
index 86d9396..becc2d1 100644
@@ -1296,8 +1296,10 @@ static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
 			continue;
 
 		if (WARN_ONCE(cnt >= size,
-			      "failed to write MEM_TOPOLOGY, way too many nodes\n"))
+			"failed to write MEM_TOPOLOGY, way too many nodes\n")) {
+			closedir(dir);
 			return -1;
+		}
 
 		ret = memory_node__read(&nodes[cnt++], idx);
 	}
tools/perf/util/util.c +4 -2
index 5eda6e1..ae56c76 100644
@@ -154,8 +154,10 @@ static int rm_rf_depth_pat(const char *path, int depth, const char **pat)
 		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
 			continue;
 
-		if (!match_pat(d->d_name, pat))
-			return -2;
+		if (!match_pat(d->d_name, pat)) {
+			ret =  -2;
+			break;
+		}
 
 		scnprintf(namebuf, sizeof(namebuf), "%s/%s",
 			  path, d->d_name);
Signature · 1 line
2.7.4.huawei.3
#2 lore raw

Em Tue, Oct 15, 2019 at 10:44:51AM +0200, Jiri Olsa escreveu:

Quoted 12 lines · Yunfeng Ye
Quoted 7 lines

Both build_mem_topology() and rm_rf_depth_pat() have resource leak of closedir() on the error paths.

Fix this by calling closedir() before function returns.

Fixes e2091cedd51b ("perf tools: Add MEM_TOPOLOGY feature to perf data file")
Fixes cdb6b0235f17 ("perf tools: Add pattern name checking to rm_rf")

guilty as charged ;-)

Thanks, applied to perf/urgent.

- Arnaldo

merge bot Applied
#3 lore raw

[tip: perf/urgent] perf tools: Fix resource leak of closedir() on the error paths

In reply to Yunfeng Ye
The following commit has been merged into the perf/urgent branch of tip:

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     6080728ff8e9c9116e52e6f840152356ac2fea56
Gitweb:        https://git.kernel.org/tip/6080728ff8e9c9116e52e6f840152356ac2fea56
Author:        Yunfeng Ye <[email protected]>
AuthorDate:    Tue, 15 Oct 2019 16:30:08 +08:00
Committer:     Arnaldo Carvalho de Melo <[email protected]>
CommitterDate: Tue, 15 Oct 2019 11:54:11 -03:00

perf tools: Fix resource leak of closedir() on the error paths

Both build_mem_topology() and rm_rf_depth_pat() have resource leaks of closedir() on the error paths.

Fix this by calling closedir() before function returns.

Fixes e2091cedd51b ("perf tools: Add MEM_TOPOLOGY feature to perf data file")
Fixes cdb6b0235f17 ("perf tools: Add pattern name checking to rm_rf")
Cc Alexander Shishkin <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Alexey Budankov <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Feilong Lin <[email protected]>
Cc Hu Shiyuan <[email protected]>
Cc Igor Lubashev <[email protected]>
Cc Kan Liang <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
---
 tools/perf/util/header.c | 4 +++-
 tools/perf/util/util.c   | 6 ++++--
 2 files changed, 7 insertions(+), 3 deletions(-)
tools/perf/util/header.c +3 -1
index 86d9396..becc2d1 100644
@@ -1296,8 +1296,10 @@ static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
 			continue;
 
 		if (WARN_ONCE(cnt >= size,
-			      "failed to write MEM_TOPOLOGY, way too many nodes\n"))
+			"failed to write MEM_TOPOLOGY, way too many nodes\n")) {
+			closedir(dir);
 			return -1;
+		}
 
 		ret = memory_node__read(&nodes[cnt++], idx);
 	}
tools/perf/util/util.c +4 -2
index 5eda6e1..ae56c76 100644
@@ -154,8 +154,10 @@ static int rm_rf_depth_pat(const char *path, int depth, const char **pat)
 		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
 			continue;
 
-		if (!match_pat(d->d_name, pat))
-			return -2;
+		if (!match_pat(d->d_name, pat)) {
+			ret =  -2;
+			break;
+		}
 
 		scnprintf(namebuf, sizeof(namebuf), "%s/%s",
 			  path, d->d_name);