[PATCH RESEND] perf cputopo: Improve exception handling in build_cpu_topology()

From: Markus Elfring
Date: Tue Mar 04 2025 - 04:56:57 EST


From: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 23 Mar 2023 22:00:07 +0100

The label “done” was used to jump to another pointer check despite of
the detail in the implementation of the function “build_cpu_topology”
that it was determined already that a corresponding variable contained
a null pointer because of a failed call of the function “fopen”.

1. Thus use more appropriate labels instead.

2. Reorder jump targets at the end.

3. Delete a redundant check.


This issue was detected by using the Coccinelle software.

Fixes: 5135d5efcbb4 ("perf tools: Add cpu_topology object")
Signed-off-by: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
---
tools/perf/util/cputopo.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/cputopo.c b/tools/perf/util/cputopo.c
index e08797c3cdbc..fd185951ee2c 100644
--- a/tools/perf/util/cputopo.c
+++ b/tools/perf/util/cputopo.c
@@ -112,10 +112,10 @@ static int build_cpu_topology(struct cpu_topology *tp, int cpu)
}
fp = fopen(filename, "r");
if (!fp)
- goto done;
+ goto exit;

if (getline(&buf, &len, fp) <= 0)
- goto done;
+ goto close_file;

p = strchr(buf, '\n');
if (p)
@@ -131,10 +131,10 @@ static int build_cpu_topology(struct cpu_topology *tp, int cpu)
buf = NULL;
}
ret = 0;
-done:
- if (fp)
- fclose(fp);
free(buf);
+close_file:
+ fclose(fp);
+exit:
return ret;
}

--
2.40.0