[PATCH v1 34/48] perf bpf_counter: Silence -Wshorten-64-to-32 warnings

From: Ian Rogers
Date: Tue Apr 01 2025 - 14:32:11 EST


The clang warning -Wshorten-64-to-32 can be useful to catch
inadvertent truncation. In some instances this truncation can lead to
changing the sign of a result, for example, truncation to return an
int to fit a sort routine. Silence the warning by making the implicit
truncation explicit.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/bpf_counter.c | 6 +++---
tools/perf/util/bpf_counter_cgroup.c | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c
index 73fcafbffc6a..3b1dbf1f5f66 100644
--- a/tools/perf/util/bpf_counter.c
+++ b/tools/perf/util/bpf_counter.c
@@ -177,7 +177,7 @@ static int bpf_program_profiler__load(struct evsel *evsel, struct target *target
return -1;

while ((tok = strtok_r(bpf_str, ",", &saveptr)) != NULL) {
- prog_id = strtoul(tok, &p, 10);
+ prog_id = (unsigned int)strtoul(tok, &p, 10);
if (prog_id == 0 || prog_id == UINT_MAX ||
(*p != '\0' && *p != ',')) {
pr_err("Failed to parse bpf prog ids %s\n",
@@ -418,7 +418,7 @@ static int bperf_reload_leader_program(struct evsel *evsel, int attr_map_fd,
link = bpf_program__attach(skel->progs.on_switch);
if (IS_ERR(link)) {
pr_err("Failed to attach leader program\n");
- err = PTR_ERR(link);
+ err = (int)PTR_ERR(link);
goto out;
}

@@ -459,7 +459,7 @@ static int bperf_attach_follower_program(struct bperf_follower_bpf *skel,
else {
link = bpf_program__attach(skel->progs.fexit_XXX);
if (IS_ERR(link))
- err = PTR_ERR(link);
+ err = (int)PTR_ERR(link);
}

return err;
diff --git a/tools/perf/util/bpf_counter_cgroup.c b/tools/perf/util/bpf_counter_cgroup.c
index 6ff42619de12..35d1b950e803 100644
--- a/tools/perf/util/bpf_counter_cgroup.c
+++ b/tools/perf/util/bpf_counter_cgroup.c
@@ -98,7 +98,7 @@ static int bperf_load_program(struct evlist *evlist)
FD(cgrp_switch, i));
if (IS_ERR(link)) {
pr_err("Failed to attach cgroup program\n");
- err = PTR_ERR(link);
+ err = (int)PTR_ERR(link);
goto out;
}
}
--
2.49.0.504.g3bcea36a83-goog