* Re: [PATCH] tools/lib/bpf: fix -Wdiscarded-qualifiers under C23
2025-11-28 0:22 [PATCH] tools/lib/bpf: fix -Wdiscarded-qualifiers under C23 Mikhail Gavrilov
@ 2025-11-28 9:26 ` Florian Weimer
2025-11-28 13:26 ` [PATCH v2 bpf-next] " Mikhail Gavrilov
2025-11-28 14:59 ` Mikhail Gavrilov
2 siblings, 0 replies; 9+ messages in thread
From: Florian Weimer @ 2025-11-28 9:26 UTC (permalink / raw)
To: Mikhail Gavrilov; +Cc: bpf, andrii, ast, daniel, netdev
* Mikhail Gavrilov:
> glibc ≥ 2.42 (GCC 15) defaults to -std=gnu23, which promotes
> -Wdiscarded-qualifiers to an error in the default hardening flags
> of Fedora Rawhide, Arch Linux, openSUSE Tumbleweed, Gentoo, etc.
>
> In C23, strstr() and strchr() return "const char *" in most cases,
> making implicit casts from const to non-const invalid.
>
> This breaks the build of tools/bpf/resolve_btfids on pristine
> upstream kernel when using GCC 15 + glibc 2.42+.
>
> Fix the three remaining instances with explicit casts.
>
> No functional changes.
>
> Link: https://bugzilla.redhat.com/show_bug.cgi?id=2417601
> Signed-off-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
> ---
> tools/lib/bpf/libbpf.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index dd3b2f57082d..dd11feef3adf 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -8247,7 +8247,7 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
> struct extern_desc *ext;
> char *res;
>
> - res = strstr(sym_name, ".llvm.");
> + res = (char *)strstr(sym_name, ".llvm.");
> if (sym_type == 'd' && res)
> ext = find_extern_by_name_with_len(obj, sym_name, res - sym_name);
> else
> @@ -11576,7 +11576,7 @@ static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type,
> */
> char sym_trim[256], *psym_trim = sym_trim, *sym_sfx;
>
> - if (!(sym_sfx = strstr(sym_name, ".llvm.")))
> + if (!(sym_sfx = (char *)strstr(sym_name, ".llvm.")))
> return 0;
>
> /* psym_trim vs sym_trim dance is done to avoid pointer vs array
> @@ -12164,7 +12164,7 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz)
>
> if (s[0] == ':')
> s++;
> - next_path = strchr(s, ':');
> + next_path = (char *)strchr(s, ':');
> seg_len = next_path ? next_path - s : strlen(s);
> if (!seg_len)
> continue;
I think you should change the type of the relevant variables to const
char *. The kernel coding style does not disallow using const, does it?
Thanks,
Florian
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 bpf-next] tools/lib/bpf: fix -Wdiscarded-qualifiers under C23
2025-11-28 0:22 [PATCH] tools/lib/bpf: fix -Wdiscarded-qualifiers under C23 Mikhail Gavrilov
2025-11-28 9:26 ` Florian Weimer
@ 2025-11-28 13:26 ` Mikhail Gavrilov
2025-11-28 13:38 ` Florian Weimer
2025-11-28 14:59 ` Mikhail Gavrilov
2 siblings, 1 reply; 9+ messages in thread
From: Mikhail Gavrilov @ 2025-11-28 13:26 UTC (permalink / raw)
To: bpf; +Cc: andrii, ast, daniel, netdev, fweimer, Mikhail Gavrilov
glibc ≥ 2.42 (GCC 15) defaults to -std=gnu23, which promotes
-Wdiscarded-qualifiers to an error in the default hardening flags
of Fedora Rawhide, Arch Linux, openSUSE Tumbleweed, Gentoo, etc.
In C23, strstr() and strchr() return "const char *" in most cases,
making previous implicit casts invalid.
This breaks the build of tools/bpf/resolve_btfids on pristine
upstream kernel when using GCC 15 + glibc 2.42+.
Fix the three remaining instances with explicit casts.
No functional changes.
Link: https://bugzilla.redhat.com/show_bug.cgi?id=2417601
Suggested-by: Florian Weimer <fweimer@redhat.com>
Signed-off-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
---
v2:
- Use explicit casts instead of changing variable types to const char *,
because the variables are already declared as char * earlier in the
functions and used in contexts requiring mutability.
This is common practice in the kernel when full const-correctness
cannot be preserved without major refactoring.
---
tools/lib/bpf/libbpf.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index dd3b2f57082d..dd11feef3adf 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -8247,7 +8247,7 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
struct extern_desc *ext;
char *res;
- res = strstr(sym_name, ".llvm.");
+ res = (char *)strstr(sym_name, ".llvm.");
if (sym_type == 'd' && res)
ext = find_extern_by_name_with_len(obj, sym_name, res - sym_name);
else
@@ -11576,7 +11576,7 @@ static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type,
*/
char sym_trim[256], *psym_trim = sym_trim, *sym_sfx;
- if (!(sym_sfx = strstr(sym_name, ".llvm.")))
+ if (!(sym_sfx = (char *)strstr(sym_name, ".llvm.")))
return 0;
/* psym_trim vs sym_trim dance is done to avoid pointer vs array
@@ -12164,7 +12164,7 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz)
if (s[0] == ':')
s++;
- next_path = strchr(s, ':');
+ next_path = (char *)strchr(s, ':');
seg_len = next_path ? next_path - s : strlen(s);
if (!seg_len)
continue;
--
2.52.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v2 bpf-next] tools/lib/bpf: fix -Wdiscarded-qualifiers under C23
2025-11-28 13:26 ` [PATCH v2 bpf-next] " Mikhail Gavrilov
@ 2025-11-28 13:38 ` Florian Weimer
0 siblings, 0 replies; 9+ messages in thread
From: Florian Weimer @ 2025-11-28 13:38 UTC (permalink / raw)
To: Mikhail Gavrilov; +Cc: bpf, andrii, ast, daniel, netdev
* Mikhail Gavrilov:
> - Use explicit casts instead of changing variable types to const char *,
> because the variables are already declared as char * earlier in the
> functions and used in contexts requiring mutability.
> This is common practice in the kernel when full const-correctness
> cannot be preserved without major refactoring.
What kind of mutability is this about? Obviously char *const won't
work, but const char * seems to be fine for these variables?
Thanks,
Florian
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 bpf-next] tools/lib/bpf: fix -Wdiscarded-qualifiers under C23
2025-11-28 0:22 [PATCH] tools/lib/bpf: fix -Wdiscarded-qualifiers under C23 Mikhail Gavrilov
2025-11-28 9:26 ` Florian Weimer
2025-11-28 13:26 ` [PATCH v2 bpf-next] " Mikhail Gavrilov
@ 2025-11-28 14:59 ` Mikhail Gavrilov
2025-12-05 23:51 ` Andrii Nakryiko
2 siblings, 1 reply; 9+ messages in thread
From: Mikhail Gavrilov @ 2025-11-28 14:59 UTC (permalink / raw)
To: bpf; +Cc: andrii, ast, daniel, netdev, fweimer, Mikhail Gavrilov
glibc ≥ 2.42 (GCC 15) defaults to -std=gnu23, which promotes
-Wdiscarded-qualifiers to an error in the default hardening flags
of Fedora Rawhide, Arch Linux, openSUSE Tumbleweed, Gentoo, etc.
In C23, strstr() and strchr() return "const char *" in most cases,
making previous implicit casts invalid.
This breaks the build of tools/bpf/resolve_btfids on pristine
upstream kernel when using GCC 15 + glibc 2.42+.
Fix the three remaining instances with explicit casts.
No functional changes.
Link: https://bugzilla.redhat.com/show_bug.cgi?id=2417601
Suggested-by: Florian Weimer <fweimer@redhat.com>
Signed-off-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
---
v2:
- Declare `res` as `const char *` — never modified.
- Keep `sym_sfx` as `char *` and cast — it is advanced in the loop.
- Cast `next_path` — declared as `char *` earlier in the function.
Changing it to const would require refactoring the whole function,
which is not justified for a tools/ file.
---
tools/lib/bpf/libbpf.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index dd3b2f57082d..22ccd50e9978 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -8245,7 +8245,7 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
struct bpf_object *obj = ctx;
const struct btf_type *t;
struct extern_desc *ext;
- char *res;
+ const char *res;
res = strstr(sym_name, ".llvm.");
if (sym_type == 'd' && res)
@@ -11576,7 +11576,7 @@ static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type,
*/
char sym_trim[256], *psym_trim = sym_trim, *sym_sfx;
- if (!(sym_sfx = strstr(sym_name, ".llvm.")))
+ if (!(sym_sfx = (char *)strstr(sym_name, ".llvm."))) /* needs mutation */
return 0;
/* psym_trim vs sym_trim dance is done to avoid pointer vs array
@@ -12164,7 +12164,7 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz)
if (s[0] == ':')
s++;
- next_path = strchr(s, ':');
+ next_path = (char *)strchr(s, ':'); /* declared as char * above */
seg_len = next_path ? next_path - s : strlen(s);
if (!seg_len)
continue;
--
2.52.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v2 bpf-next] tools/lib/bpf: fix -Wdiscarded-qualifiers under C23
2025-11-28 14:59 ` Mikhail Gavrilov
@ 2025-12-05 23:51 ` Andrii Nakryiko
2025-12-06 8:05 ` [PATCH v3] " Mikhail Gavrilov
2025-12-06 9:28 ` Mikhail Gavrilov
0 siblings, 2 replies; 9+ messages in thread
From: Andrii Nakryiko @ 2025-12-05 23:51 UTC (permalink / raw)
To: Mikhail Gavrilov; +Cc: bpf, andrii, ast, daniel, netdev, fweimer
On Fri, Nov 28, 2025 at 6:59 AM Mikhail Gavrilov
<mikhail.v.gavrilov@gmail.com> wrote:
>
> glibc ≥ 2.42 (GCC 15) defaults to -std=gnu23, which promotes
> -Wdiscarded-qualifiers to an error in the default hardening flags
> of Fedora Rawhide, Arch Linux, openSUSE Tumbleweed, Gentoo, etc.
>
> In C23, strstr() and strchr() return "const char *" in most cases,
> making previous implicit casts invalid.
>
> This breaks the build of tools/bpf/resolve_btfids on pristine
> upstream kernel when using GCC 15 + glibc 2.42+.
>
> Fix the three remaining instances with explicit casts.
>
> No functional changes.
>
> Link: https://bugzilla.redhat.com/show_bug.cgi?id=2417601
> Suggested-by: Florian Weimer <fweimer@redhat.com>
> Signed-off-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
>
> ---
> v2:
> - Declare `res` as `const char *` — never modified.
> - Keep `sym_sfx` as `char *` and cast — it is advanced in the loop.
> - Cast `next_path` — declared as `char *` earlier in the function.
> Changing it to const would require refactoring the whole function,
> which is not justified for a tools/ file.
> ---
> tools/lib/bpf/libbpf.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index dd3b2f57082d..22ccd50e9978 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -8245,7 +8245,7 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
> struct bpf_object *obj = ctx;
> const struct btf_type *t;
> struct extern_desc *ext;
> - char *res;
> + const char *res;
>
> res = strstr(sym_name, ".llvm.");
> if (sym_type == 'd' && res)
> @@ -11576,7 +11576,7 @@ static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type,
> */
> char sym_trim[256], *psym_trim = sym_trim, *sym_sfx;
const char *sym_sfx; instead of unnecessary cast
>
> - if (!(sym_sfx = strstr(sym_name, ".llvm.")))
> + if (!(sym_sfx = (char *)strstr(sym_name, ".llvm."))) /* needs mutation */
> return 0;
>
> /* psym_trim vs sym_trim dance is done to avoid pointer vs array
> @@ -12164,7 +12164,7 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz)
>
> if (s[0] == ':')
> s++;
> - next_path = strchr(s, ':');
> + next_path = (char *)strchr(s, ':'); /* declared as char * above */
same here, next_path should be const char *
pw-bot: cr
> seg_len = next_path ? next_path - s : strlen(s);
> if (!seg_len)
> continue;
> --
> 2.52.0
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v3] tools/lib/bpf: fix -Wdiscarded-qualifiers under C23
2025-12-05 23:51 ` Andrii Nakryiko
@ 2025-12-06 8:05 ` Mikhail Gavrilov
2025-12-06 9:28 ` Mikhail Gavrilov
1 sibling, 0 replies; 9+ messages in thread
From: Mikhail Gavrilov @ 2025-12-06 8:05 UTC (permalink / raw)
To: bpf; +Cc: andrii, ast, daniel, netdev, fweimer, andrii.nakryiko,
Mikhail Gavrilov
glibc ≥ 2.42 (GCC 15) defaults to -std=gnu23, which promotes
-Wdiscarded-qualifiers to an error.
In C23, strstr() and strchr() return "const char *".
Declare `res` and `next_path` as const char * — they are never modified.
Keep `sym_sfx` as char * because it is advanced in a loop.
Suggested-by: Florian Weimer <fweimer@redhat.com>
Suggested-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Signed-off-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
---
v2: use const char * where possible (Florian, Andrii)
v3: declare res/next_path as const char * (never modified)
keep cast for sym_sfx (advanced in loop)
---
tools/lib/bpf/libbpf.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 3dc8a8078815..81782471f1d0 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -8484,7 +8484,7 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
struct bpf_object *obj = ctx;
const struct btf_type *t;
struct extern_desc *ext;
- char *res;
+ const char *res;
res = strstr(sym_name, ".llvm.");
if (sym_type == 'd' && res)
@@ -11820,7 +11820,7 @@ static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type,
*/
char sym_trim[256], *psym_trim = sym_trim, *sym_sfx;
- if (!(sym_sfx = strstr(sym_name, ".llvm.")))
+ if (!(sym_sfx = (char *)strstr(sym_name, ".llvm."))) /* needs mutation */
return 0;
/* psym_trim vs sym_trim dance is done to avoid pointer vs array
@@ -12401,7 +12401,7 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz)
if (!search_paths[i])
continue;
for (s = search_paths[i]; s != NULL; s = strchr(s, ':')) {
- char *next_path;
+ const char *next_path;
int seg_len;
if (s[0] == ':')
--
2.52.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH v3] tools/lib/bpf: fix -Wdiscarded-qualifiers under C23
2025-12-05 23:51 ` Andrii Nakryiko
2025-12-06 8:05 ` [PATCH v3] " Mikhail Gavrilov
@ 2025-12-06 9:28 ` Mikhail Gavrilov
2025-12-10 7:30 ` patchwork-bot+netdevbpf
1 sibling, 1 reply; 9+ messages in thread
From: Mikhail Gavrilov @ 2025-12-06 9:28 UTC (permalink / raw)
To: bpf; +Cc: andrii, ast, daniel, netdev, fweimer, andrii.nakryiko,
Mikhail Gavrilov
glibc ≥ 2.42 (GCC 15) defaults to -std=gnu23, which promotes
-Wdiscarded-qualifiers to an error.
In C23, strstr() and strchr() return "const char *".
Change variable types to const char * where the pointers are never
modified (res, sym_sfx, next_path).
Suggested-by: Florian Weimer <fweimer@redhat.com>
Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
---
v2: use const char * where possible
v3: split declaration of sym_sfx — it is only read, never written
---
tools/lib/bpf/libbpf.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 3dc8a8078815..f4dfd23148a5 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -8484,7 +8484,7 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
struct bpf_object *obj = ctx;
const struct btf_type *t;
struct extern_desc *ext;
- char *res;
+ const char *res;
res = strstr(sym_name, ".llvm.");
if (sym_type == 'd' && res)
@@ -11818,7 +11818,8 @@ static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type,
*
* [0] fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG")
*/
- char sym_trim[256], *psym_trim = sym_trim, *sym_sfx;
+ char sym_trim[256], *psym_trim = sym_trim;
+ const char *sym_sfx;
if (!(sym_sfx = strstr(sym_name, ".llvm.")))
return 0;
@@ -12401,7 +12402,7 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz)
if (!search_paths[i])
continue;
for (s = search_paths[i]; s != NULL; s = strchr(s, ':')) {
- char *next_path;
+ const char *next_path;
int seg_len;
if (s[0] == ':')
--
2.52.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v3] tools/lib/bpf: fix -Wdiscarded-qualifiers under C23
2025-12-06 9:28 ` Mikhail Gavrilov
@ 2025-12-10 7:30 ` patchwork-bot+netdevbpf
0 siblings, 0 replies; 9+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-12-10 7:30 UTC (permalink / raw)
To: Mikhail Gavrilov
Cc: bpf, andrii, ast, daniel, netdev, fweimer, andrii.nakryiko
Hello:
This patch was applied to bpf/bpf.git (master)
by Alexei Starovoitov <ast@kernel.org>:
On Sat, 6 Dec 2025 14:28:25 +0500 you wrote:
> glibc ≥ 2.42 (GCC 15) defaults to -std=gnu23, which promotes
> -Wdiscarded-qualifiers to an error.
>
> In C23, strstr() and strchr() return "const char *".
>
> Change variable types to const char * where the pointers are never
> modified (res, sym_sfx, next_path).
>
> [...]
Here is the summary with links:
- [v3] tools/lib/bpf: fix -Wdiscarded-qualifiers under C23
https://git.kernel.org/bpf/bpf/c/d70f79fef658
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 9+ messages in thread