Skip to contentSkip to conversation

bpftool: fix -Wcast-qual warning

PATCH Superseded Denys Zagorui 2 participants 4 messages Sep 6, 2023 – Sep 7, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpftool: fix -Wcast-qual warning

This cast was made by purpose for older libbpf where the bpf_object_skeleton field is void * instead of const void * to eliminte a warning (as i understand -Wincompatible-pointer-types-discards-qualifiers) but this cast introduces another warnging (-Wcast-qual) for libbpf where data field is const void *

It makes sense for bpftool to be in sync with libbpf from kernel sources

---
 tools/bpf/bpftool/gen.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/gen.c +1 -1
index 2883660d6b67..04c47745b3ea 100644
@@ -1209,7 +1209,7 @@ static int do_skeleton(int argc, char **argv)
 	codegen("\
 		\n\
 									    \n\
-			s->data = (void *)%2$s__elf_bytes(&s->data_sz);	    \n\
+			s->data = %2$s__elf_bytes(&s->data_sz);		    \n\
 									    \n\
 			obj->skeleton = s;				    \n\
 			return 0;					    \n\
Signature · 1 line
2.35.6
#1 lore raw
Quoted 3 lines · Denys Zagorui

This cast was made by purpose for older libbpf where the bpf_object_skeleton field is void * instead of const void * to eliminte a warning (as i understand

s/eliminte/eliminate/

Quoted 2 lines

-Wincompatible-pointer-types-discards-qualifiers) but this cast introduces another warnging (-Wcast-qual) for libbpf

s/warnging/warning/

Quoted 15 lines

where data field is const void *

It makes sense for bpftool to be in sync with libbpf from kernel sources

---
 tools/bpf/bpftool/gen.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/gen.c +0 -0
index 2883660d6b67..04c47745b3ea 100644

tools/bpf/bpftool/gen.c static int do_skeleton(int argc, char **argv) · line 1215

Quoted 8 lines
tools/bpf/bpftool/gen.c +1 -1
@@ -1209,7 +1209,7 @@ static int do_skeleton(int argc, char **argv)
 	codegen("\
 		\n\
 									    \n\
-			s->data = (void *)%2$s__elf_bytes(&s->data_sz);	    \n\
+			s->data = %2$s__elf_bytes(&s->data_sz);		    \n\
 									    \n\
 			obj->skeleton = s;				    \n\
 			return 0;					    \n\

If I follow correctly, the cast was added in bpftool in a6cc6b34b93e ("bpftool: Provide a helper method for accessing skeleton's embedded ELF data"), which mentions indeed:

The assignment to s->data is cast to void * to ensure no warning is issued if compiled with a previous version of libbpf where the bpf_object_skeleton field is void * instead of const void *

but in libbpf, s->data's type had already been changed since commit 08a6f22ef6f8 ("libbpf: Change bpf_object_skeleton data field to const pointer"), part of libbpf 0.6, is this correct?

If this is the case then the commit makes sense to me, I think it's OK to have a warning with libbpf < 1.0 if it helps suppressing one when loading skeletons with the current version of the lib.

Thanks,
Quentin

#2 lore raw
Quoted 11 lines

If I follow correctly, the cast was added in bpftool in a6cc6b34b93e ("bpftool: Provide a helper method for accessing skeleton's embedded ELF data"), which mentions indeed:

The assignment to s->data is cast to void * to ensure no warning is issued if compiled with a previous version of libbpf where the bpf_object_skeleton field is void * instead of const void *

but in libbpf, s->data's type had already been changed since commit 08a6f22ef6f8 ("libbpf: Change bpf_object_skeleton data field to const pointer"), part of libbpf 0.6, is this correct?

yes, this is correct

#3 lore raw
Quoted 4 lines · Denys Zagorui -X (dzagorui - GLOBALLOGIC INC at Cisco)
Quoted 4 lines

If I follow correctly, the cast was added in bpftool in a6cc6b34b93e ("bpftool: Provide a helper method for accessing skeleton's embedded ELF data"), which mentions indeed:

Quoted 9 lines
Quoted 7 lines

The assignment to s->data is cast to void * to ensure no warning is issued if compiled with a previous version of libbpf where the bpf_object_skeleton field is void * instead of const void *

but in libbpf, s->data's type had already been changed since commit 08a6f22ef6f8 ("libbpf: Change bpf_object_skeleton data field to const pointer"), part of libbpf 0.6, is this correct?

yes, this is correct

OK, thanks