bpftool: Be more portable by using POSIX's basename()
PATCH 1/1 bpftool: Be more portable by using POSIX's basename()
musl libc had the basename() prototype in string.h, but this is a glibc-ism, now they removed the _GNU_SOURCE bits in their devel distro, Alpine Linux edge:
https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7
So lets use the POSIX version, the whole rationale is spelled out at:
https://gitlab.alpinelinux.org/alpine/aports/-/issues/15643
--- tools/bpf/bpftool/gen.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
tools/bpf/bpftool/gen.c +5 -3
@@ -7,6 +7,7 @@#include <ctype.h> #include <errno.h> #include <fcntl.h> +#include <libgen.h> #include <linux/err.h> #include <stdbool.h> #include <stdio.h>
@@ -56,9 +57,10 @@ static bool str_has_suffix(const char *str, const char *suffix)static void get_obj_name (char *name,const char *file) { -/* Using basename() GNU version which doesn't modify arg. */ -strncpy (name,basename (file),MAX_OBJ_NAME_LEN -1 ); - name[MAX_OBJ_NAME_LEN -1 ] ='\0' ; +char file_copy[PATH_MAX ]; +/* Using basename() POSIX version to be more portable. */ +strncpy (file_copy, file,PATH_MAX -1 )[PATH_MAX -1 ] ='\0' ; +strncpy (name,basename (file_copy),MAX_OBJ_NAME_LEN -1 )[MAX_OBJ_NAME_LEN -1 ] ='\0' ;if (str_has_suffix (name,".o" )) name[strlen (name) -2 ] ='\0' ;sanitize_identifier (name);
Signature · 1 line
2.43.0
Daniel Borkmann
maintainerQuoted 35 lines · Arnaldo Carvalho de Melo
musl libc had the
basename()prototype instring.h, but this is a glibc-ism, now they removed the_GNU_SOURCEbits in their devel distro, Alpine Linux edge:https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7So lets use the POSIX version, the whole rationale is spelled out at:
https://gitlab.alpinelinux.org/alpine/aports/-/issues/15643Acked-by Jiri Olsa <[email protected]>Acked-by Quentin Monnet <[email protected]>Cc Adrian Hunter <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Ian Rogers <[email protected]>Cc Namhyung Kim <[email protected]>Signed-off-by Arnaldo Carvalho de Melo <[email protected]>--- tools/bpf/bpftool/gen.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)tools/bpf/bpftool/gen.c +1
-0@@ -7,6 +7,7 @@#include <ctype.h> #include <errno.h> #include <fcntl.h> +#include <libgen.h> #include <linux/err.h> #include <stdbool.h> #include <stdio.h>
tools/bpf/bpftool/gen.c static bool str_has_suffix(const char *str, const char *suffix) · line 60
Quoted 7 lines
tools/bpf/bpftool/gen.c +1
-3@@ -56,9 +57,10 @@ static bool str_has_suffix(const char *str, const char *suffix)static void get_obj_name (char *name,const char *file) { -/* Using basename() GNU version which doesn't modify arg. */ -strncpy (name,basename (file),MAX_OBJ_NAME_LEN -1 ); - name[MAX_OBJ_NAME_LEN -1 ] ='\0' ; +char file_copy[PATH_MAX ];
Added a newline in here while applying, otherwise lgtm, thanks!
Quoted 7 lines
tools/bpf/bpftool/gen.c +3
-0+/* Using basename() POSIX version to be more portable. */ +strncpy (file_copy, file,PATH_MAX -1 )[PATH_MAX -1 ] ='\0' ; +strncpy (name,basename (file_copy),MAX_OBJ_NAME_LEN -1 )[MAX_OBJ_NAME_LEN -1 ] ='\0' ;if (str_has_suffix (name,".o" )) name[strlen (name) -2 ] ='\0' ;sanitize_identifier (name);