Skip to contentSkip to conversation

bpftool: Fix build error with -Werror=type-limits

PATCH In review Yafang Shao 3 participants 5 messages Aug 29, 2023 – Aug 30, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpftool: Fix build error with -Werror=type-limits

Quentin reported a build error as follows,

link.c: In function ‘perf_config_hw_cache_str’:
link.c:86:18: warning: comparison of unsigned expression in ‘>= 0’ is always true [-Wtype-limits]
86 | if ((id) >= 0 && (id) < ARRAY_SIZE(array)) \
| ^~
link.c:320:20: note: in expansion of macro ‘perf_event_name’
320 | hw_cache = perf_event_name(evsel__hw_cache, config & 0xff);
| ^~~~~~~~~~~~~~~
[... more of the same for the other calls to perf_event_name ...]

He also pointed out the reason and the solution:

We're always passing unsigned, so it should be safe to drop the check on (id) >= 0.

---
 tools/bpf/bpftool/link.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/link.c +1 -1
index 0b214f6ab5c8..2e5c231e08ac 100644
@@ -83,7 +83,7 @@ const char *evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX] = {
 #define perf_event_name(array, id) ({			\
 	const char *event_str = NULL;			\
 							\
-	if ((id) >= 0 && (id) < ARRAY_SIZE(array))	\
+	if ((id) < ARRAY_SIZE(array))			\
 		event_str = array[id];			\
 	event_str;					\
 })
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw
Quoted 1 line · Yafang Shao

Quentin reported a build error as follows,

Just a warning :)

Quoted 12 lines

link.c: In function ‘perf_config_hw_cache_str’:
link.c:86:18: warning: comparison of unsigned expression in ‘>= 0’ is always true [-Wtype-limits]
86 | if ((id) >= 0 && (id) < ARRAY_SIZE(array)) \
| ^~
link.c:320:20: note: in expansion of macro ‘perf_event_name’
320 | hw_cache = perf_event_name(evsel__hw_cache, config & 0xff);
| ^~~~~~~~~~~~~~~
[... more of the same for the other calls to perf_event_name ...]

He also pointed out the reason and the solution:

Quoted 8 lines

We're always passing unsigned, so it should be safe to drop the check on (id) >= 0.

Thank you!

#2 lore raw
Quoted 5 lines · Quentin Monnet
Quoted 1 line · Yafang Shao

Quentin reported a build error as follows,

Just a warning :)

Will reword it in the next version.

Quoted 22 lines
Quoted 13 lines

link.c: In function ‘perf_config_hw_cache_str’:
link.c:86:18: warning: comparison of unsigned expression in ‘>= 0’ is always true [-Wtype-limits]
86 | if ((id) >= 0 && (id) < ARRAY_SIZE(array)) \
| ^~
link.c:320:20: note: in expansion of macro ‘perf_event_name’
320 | hw_cache = perf_event_name(evsel__hw_cache, config & 0xff);
| ^~~~~~~~~~~~~~~
[... more of the same for the other calls to perf_event_name ...]

He also pointed out the reason and the solution:

Quoted 8 lines

We're always passing unsigned, so it should be safe to drop the check on (id) >= 0.

Quoted 4 lines

Thank you!

Thanks for your review.

Signature · 2 lines
Regards
Yafang
#3 lore raw

PATCH v2 1/1 bpftool: Fix build warnings with -Wtype-limits

In reply to Quentin Monnet

Quentin reported build warnings when building bpftool :

link.c: In function ‘perf_config_hw_cache_str’:
link.c:86:18: warning: comparison of unsigned expression in ‘>= 0’ is always true [-Wtype-limits]
86 | if ((id) >= 0 && (id) < ARRAY_SIZE(array)) \
| ^~
link.c:320:20: note: in expansion of macro ‘perf_event_name’
320 | hw_cache = perf_event_name(evsel__hw_cache, config & 0xff);
| ^~~~~~~~~~~~~~~
[... more of the same for the other calls to perf_event_name ...]

He also pointed out the reason and the solution:

We're always passing unsigned, so it should be safe to drop the check on (id) >= 0.

---
 tools/bpf/bpftool/link.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/link.c +1 -1
index 0b214f6ab5c8..2e5c231e08ac 100644
@@ -83,7 +83,7 @@ const char *evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX] = {
 #define perf_event_name(array, id) ({			\
 	const char *event_str = NULL;			\
 							\
-	if ((id) >= 0 && (id) < ARRAY_SIZE(array))	\
+	if ((id) < ARRAY_SIZE(array))			\
 		event_str = array[id];			\
 	event_str;					\
 })
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v2 bpf-next] bpftool: Fix build warnings with -Wtype-limits

This patch was applied to bpf/bpf.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf.git (master)
by Daniel Borkmann <[email protected]>:

Quoted 11 lines · On Wed, 30 Aug 2023 03:03:25 +0000 you wrote:

Quentin reported build warnings when building bpftool :

link.c: In function ‘perf_config_hw_cache_str’:
link.c:86:18: warning: comparison of unsigned expression in ‘>= 0’ is always true [-Wtype-limits]
86 | if ((id) >= 0 && (id) < ARRAY_SIZE(array)) \
| ^~
link.c:320:20: note: in expansion of macro ‘perf_event_name’
320 | hw_cache = perf_event_name(evsel__hw_cache, config & 0xff);
| ^~~~~~~~~~~~~~~
[... more of the same for the other calls to perf_event_name ...]

Quoted 1 line

[...]

Here is the summary with links:
- [v2,bpf-next] bpftool: Fix build warnings with -Wtype-limits

    https://git.kernel.org/bpf/bpf/c/6a8faf107091

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html