Skip to content

Commit

Permalink
bpftool: Be more portable by using POSIX's basename()
Browse files Browse the repository at this point in the history
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

Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
Acked-by: Jiri Olsa <[email protected]>
Acked-by: Quentin Monnet <[email protected]>
Link: https://lore.kernel.org/lkml/[email protected]
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
acmel authored and borkmann committed Jan 29, 2024
1 parent 723de3e commit 29788f3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions tools/bpf/bpftool/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -56,9 +57,11 @@ 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);
Expand Down

0 comments on commit 29788f3

Please sign in to comment.