Skip to contentSkip to conversation

libbpf: constify string pointers assigned to from str* functions

PATCH In review Emil Tsalapatis 2 participants 2 messages Dec 16, 2025

Subsystems (2)
lore
Share & tools
lore

Emil Tsalapatis

author reviewer
#0 lore raw

PATCH 1/1 libbpf: constify string pointers assigned to from str* functions

For recent LLVM versions (e.g,. 4ed494e7282c3b36a35b8e0930fd2e14b7038167) compiling libbpf fails because it triggers -Werror=discarded-qualifiers. Constify the stack char * variables used to store the pointers returned from strstr() and strchr() to fix this.

No functional changes.

---
 tools/lib/bpf/libbpf.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +4 -3
index 6fba879492a8..1a52d818a76c 100644
@@ -8490,7 +8490,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)
@@ -11824,7 +11824,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;
@@ -12407,7 +12408,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] == ':')
Signature · 1 line
2.49.0
#1 lore raw
Quoted 6 lines · Emil Tsalapatis

For recent LLVM versions (e.g,. 4ed494e7282c3b36a35b8e0930fd2e14b7038167) compiling libbpf fails because it triggers -Werror=discarded-qualifiers. Constify the stack char * variables used to store the pointers returned from strstr() and strchr() to fix this.

Quoted 7 lines

No functional changes.

---
 tools/lib/bpf/libbpf.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

the fix is already in bpf/master as

d70f79fef658 ("libbpf: Fix -Wdiscarded-qualifiers under C23")

Github version of libbpf has this already synced.

pw-bot: cr

Quoted 35 lines
tools/lib/bpf/libbpf.c +4 -4
index 6fba879492a8..1a52d818a76c 100644
@@ -8490,7 +8490,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)
@@ -11824,7 +11824,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;
@@ -12407,7 +12408,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.49.0