* [PATCH 1/1] bpftool: Be more portable by using POSIX's basename()
@ 2024-01-29 14:33 Arnaldo Carvalho de Melo
2024-01-29 15:17 ` Daniel Borkmann
0 siblings, 1 reply; 2+ messages in thread
From: Arnaldo Carvalho de Melo @ 2024-01-29 14:33 UTC (permalink / raw)
To: Quentin Monnet
Cc: Jiri Olsa, Adrian Hunter, Andrii Nakryiko, Ian Rogers,
Namhyung Kim, bpf, linux-perf-users
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
Acked-by: Jiri Olsa <olsajiri@gmail.com>
Acked-by: Quentin Monnet <quentin@isovalent.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/ZZhsPs00TI75RdAr@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
tools/bpf/bpftool/gen.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index ee3ce2b8000d75d2..a5cc5938c3d7951e 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -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);
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH 1/1] bpftool: Be more portable by using POSIX's basename()
2024-01-29 14:33 [PATCH 1/1] bpftool: Be more portable by using POSIX's basename() Arnaldo Carvalho de Melo
@ 2024-01-29 15:17 ` Daniel Borkmann
0 siblings, 0 replies; 2+ messages in thread
From: Daniel Borkmann @ 2024-01-29 15:17 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Quentin Monnet
Cc: Jiri Olsa, Adrian Hunter, Andrii Nakryiko, Ian Rogers,
Namhyung Kim, bpf, linux-perf-users
On 1/29/24 3:33 PM, Arnaldo Carvalho de Melo wrote:
> 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
>
> Acked-by: Jiri Olsa <olsajiri@gmail.com>
> Acked-by: Quentin Monnet <quentin@isovalent.com>
> Cc: Adrian Hunter <adrian.hunter@intel.com>
> Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
> Cc: Ian Rogers <irogers@google.com>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Link: https://lore.kernel.org/lkml/ZZhsPs00TI75RdAr@kernel.org
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> ---
> tools/bpf/bpftool/gen.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
> index ee3ce2b8000d75d2..a5cc5938c3d7951e 100644
> --- a/tools/bpf/bpftool/gen.c
> +++ b/tools/bpf/bpftool/gen.c
> @@ -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];
Added a newline in here while applying, otherwise lgtm, thanks!
> + /* 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);
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-01-29 15:17 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-29 14:33 [PATCH 1/1] bpftool: Be more portable by using POSIX's basename() Arnaldo Carvalho de Melo
2024-01-29 15:17 ` Daniel Borkmann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox