From 0ea8920e86e3232c56dc812c1f363fd20fce46c6 Mon Sep 17 00:00:00 2001 From: Ian Rogers Date: Wed, 5 Apr 2023 23:52:24 -0700 Subject: [PATCH] perf pmu: Fix a few potential fd leaks Ensure fd is closed on error paths. Signed-off-by: Ian Rogers Acked-by: Jiri Olsa Acked-by: Namhyung Kim Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Gaosheng Cui Cc: German Gomez Cc: Ingo Molnar Cc: James Clark Cc: Jing Zhang Cc: Leo Yan Cc: Mark Rutland Cc: Peter Zijlstra Cc: Ravi Bangoria Cc: Rob Herring Cc: Sean Christopherson Cc: Suzuki Poulouse Link: https://lore.kernel.org/r/20230406065224.2553640-1-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/pmu.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 96ef317bac41..9eedbfc9e863 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -494,9 +494,13 @@ static int pmu_aliases_parse(int dirfd, struct list_head *head) continue; fd = openat(dirfd, name, O_RDONLY); + if (fd == -1) { + pr_debug("Cannot open %s\n", name); + continue; + } file = fdopen(fd, "r"); if (!file) { - pr_debug("Cannot open %s\n", name); + close(fd); continue; } @@ -1882,9 +1886,13 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) continue; fd = openat(caps_fd, name, O_RDONLY); - file = fdopen(fd, "r"); - if (!file) + if (fd == -1) continue; + file = fdopen(fd, "r"); + if (!file) { + close(fd); + continue; + } if (!fgets(value, sizeof(value), file) || (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {