[PATCH v1 25/48] perf help: Silence -Wshorten-64-to-32 warnings
From: Ian Rogers
Date: Tue Apr 01 2025 - 14:31:58 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/builtin-help.c | 4 ++--
tools/perf/util/help-unknown-cmd.c | 14 ++++++++------
tools/perf/util/levenshtein.c | 18 +++++++++---------
3 files changed, 19 insertions(+), 17 deletions(-)
diff --git a/tools/perf/builtin-help.c b/tools/perf/builtin-help.c
index 7be6fb6df595..05151c68191f 100644
--- a/tools/perf/builtin-help.c
+++ b/tools/perf/builtin-help.c
@@ -301,7 +301,7 @@ static struct cmdnames main_cmds, other_cmds;
void list_common_cmds_help(void)
{
- unsigned int i, longest = 0;
+ size_t i, longest = 0;
for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
if (longest < strlen(common_cmds[i].name))
@@ -310,7 +310,7 @@ void list_common_cmds_help(void)
puts(" The most commonly used perf commands are:");
for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
- printf(" %-*s ", longest, common_cmds[i].name);
+ printf(" %-*s ", (int)longest, common_cmds[i].name);
puts(common_cmds[i].help);
}
}
diff --git a/tools/perf/util/help-unknown-cmd.c b/tools/perf/util/help-unknown-cmd.c
index a0a46e34f8d1..f0a0b8c13a92 100644
--- a/tools/perf/util/help-unknown-cmd.c
+++ b/tools/perf/util/help-unknown-cmd.c
@@ -24,14 +24,15 @@ static int levenshtein_compare(const void *p1, const void *p2)
{
const struct cmdname *const *c1 = p1, *const *c2 = p2;
const char *s1 = (*c1)->name, *s2 = (*c2)->name;
- int l1 = (*c1)->len;
- int l2 = (*c2)->len;
- return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
+ ssize_t l1 = (*c1)->len;
+ ssize_t l2 = (*c2)->len;
+
+ return l1 != l2 ? (int)(l1 - l2) : (int)strcmp(s1, s2);
}
static int add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
{
- unsigned int i, nr = cmds->cnt + old->cnt;
+ size_t nr = cmds->cnt + old->cnt;
void *tmp;
if (nr > cmds->alloc) {
@@ -45,7 +46,7 @@ static int add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
return -1;
cmds->names = tmp;
}
- for (i = 0; i < old->cnt; i++)
+ for (size_t i = 0; i < old->cnt; i++)
cmds->names[cmds->cnt++] = old->names[i];
zfree(&old->names);
old->cnt = 0;
@@ -54,7 +55,8 @@ static int add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
const char *help_unknown_cmd(const char *cmd, struct cmdnames *main_cmds)
{
- unsigned int i, n = 0, best_similarity = 0;
+ unsigned int i, n = 0;
+ size_t best_similarity = 0;
struct cmdnames other_cmds;
memset(&other_cmds, 0, sizeof(other_cmds));
diff --git a/tools/perf/util/levenshtein.c b/tools/perf/util/levenshtein.c
index 6a6712635aa4..f6912b848c4a 100644
--- a/tools/perf/util/levenshtein.c
+++ b/tools/perf/util/levenshtein.c
@@ -44,19 +44,19 @@
int levenshtein(const char *string1, const char *string2,
int w, int s, int a, int d)
{
- int len1 = strlen(string1), len2 = strlen(string2);
+ size_t len1 = strlen(string1), len2 = strlen(string2);
int *row0 = malloc(sizeof(int) * (len2 + 1));
int *row1 = malloc(sizeof(int) * (len2 + 1));
int *row2 = malloc(sizeof(int) * (len2 + 1));
- int i, j;
+ int ret;
- for (j = 0; j <= len2; j++)
- row1[j] = j * a;
- for (i = 0; i < len1; i++) {
+ for (size_t j = 0; j <= len2; j++)
+ row1[j] = (int)j * a;
+ for (size_t i = 0; i < len1; i++) {
int *dummy;
- row2[0] = (i + 1) * d;
- for (j = 0; j < len2; j++) {
+ row2[0] = ((int)i + 1) * d;
+ for (size_t j = 0; j < len2; j++) {
/* substitution */
row2[j + 1] = row1[j] + s * (string1[i] != string2[j]);
/* swap */
@@ -78,10 +78,10 @@ int levenshtein(const char *string1, const char *string2,
row2 = dummy;
}
- i = row1[len2];
+ ret = row1[len2];
free(row0);
free(row1);
free(row2);
- return i;
+ return ret;
}
--
2.49.0.504.g3bcea36a83-goog