diff mbox series

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

Message ID cd5f7cd2-b80d-6add-20a1-32f4f43e0744@huawei.com
State Not Applicable
Delegated to: BPF Maintainers
Headers show
Series perf tools: fix resource leak of closedir() on the error paths | expand

Commit Message

Yunfeng Ye Oct. 15, 2019, 8:30 a.m. UTC
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")
Signed-off-by: Yunfeng Ye <yeyunfeng@huawei.com>
---
 tools/perf/util/header.c | 4 +++-
 tools/perf/util/util.c   | 6 ++++--
 2 files changed, 7 insertions(+), 3 deletions(-)

Comments

Jiri Olsa Oct. 15, 2019, 8:44 a.m. UTC | #1
On Tue, Oct 15, 2019 at 04:30:08PM +0800, Yunfeng Ye wrote:
> 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 ;-)

Acked-by: Jiri Olsa <jolsa@kernel.org>

thanks,
jirka


> Signed-off-by: Yunfeng Ye <yeyunfeng@huawei.com>
> ---
>  tools/perf/util/header.c | 4 +++-
>  tools/perf/util/util.c   | 6 ++++--
>  2 files changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> index 86d9396..becc2d1 100644
> --- a/tools/perf/util/header.c
> +++ b/tools/perf/util/header.c
> @@ -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);
>  	}
> diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
> index 5eda6e1..ae56c76 100644
> --- a/tools/perf/util/util.c
> +++ b/tools/perf/util/util.c
> @@ -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);
> -- 
> 2.7.4.huawei.3
>
Arnaldo Carvalho de Melo Oct. 15, 2019, 2:58 p.m. UTC | #2
Em Tue, Oct 15, 2019 at 10:44:51AM +0200, Jiri Olsa escreveu:
> On Tue, Oct 15, 2019 at 04:30:08PM +0800, Yunfeng Ye wrote:
> > 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 ;-)
> 
> Acked-by: Jiri Olsa <jolsa@kernel.org>

Thanks, applied to perf/urgent.

- Arnaldo
diff mbox series

Patch

diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 86d9396..becc2d1 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -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);
 	}
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 5eda6e1..ae56c76 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -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);