Skip to content

Commit

Permalink
libmpathutil: really always use glibc basename()
Browse files Browse the repository at this point in the history
Despite 03a5456 ("libmultipath: always use glibc basename()"), we
still call the system library's basename() from libmultipath.
musl libc until 1.24 provided a prototype for basename() in string.h,
which was not correct and was resolved to the destructive POSIX
basename(). musl libc 1.25 removed this prototype.

While the remaining code path doesn't strictly depend on the non-destructive
behavior of glibc's basename(), it's cleaner and safer to use the same
implementation everywhere.

Fixes: 03a5456 ("libmultipath: always use glibc basename()")
Fixes: #84

Signed-off-by: Martin Wilck <mwilck@suse.com>
Reviewed-by: Khem Raj <raj.khem@gmail.com>
Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
  • Loading branch information
mwilck committed Apr 4, 2024
1 parent 1c4301a commit e5004de
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
4 changes: 4 additions & 0 deletions libmpathutil/libmpathutil.version
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,7 @@ LIBMPATHUTIL_2.0 {
vector_move_up;
vector_sort;
};

LIBMPATHUTIL_2.1 {
libmp_basename;
};
5 changes: 1 addition & 4 deletions libmpathutil/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@ strchop(char *str)
return i;
}

#ifndef __GLIBC__
/*
* glibc's non-destructive version of basename()
* License: LGPL-2.1-or-later
*/
static const char *__basename(const char *filename)
const char *libmp_basename(const char *filename)
{
char *p = strrchr(filename, '/');
return p ? p + 1 : filename;
}
#define basename(x) __basename(x)
#endif

int
basenamecpy (const char *src, char *dst, size_t size)
Expand Down
5 changes: 5 additions & 0 deletions libmpathutil/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
#include <stdio.h>

size_t strchop(char *);

const char *libmp_basename(const char *filename);
#ifndef __GLIBC__
#define basename(x) libmp_basename(x)
#endif
int basenamecpy (const char *src, char *dst, size_t size);
int filepresent (const char *run);
char *get_next_string(char **temp, const char *split_char);
Expand Down
31 changes: 31 additions & 0 deletions tests/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*
*/

#define _GNU_SOURCE
#include <stdbool.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdlib.h>
#include <cmocka.h>
#include <endian.h>
#include <string.h>
#include "util.h"

#include "globals.c"
Expand Down Expand Up @@ -163,6 +165,34 @@ static int test_basenamecpy(void)
return cmocka_run_group_tests(tests, NULL, NULL);
}

static void test_basename_01(void **state)
{
const char *path = "/foo/bar";
const char *base;

base = basename(path);
assert_string_equal(base, "bar");
assert_string_equal(path, "/foo/bar");
}

static void test_basename_02(void **state)
{
const char *path = "/foo/bar/";
const char *base;

base = basename(path);
assert_string_equal(base, "");
assert_string_equal(path, "/foo/bar/");
}

static int test_basename(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_basename_01),
cmocka_unit_test(test_basename_02),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

/*
* On big endian systems, if bitfield_t is 32bit, we need
* to swap the two 32 bit parts of a 64bit value to make
Expand Down Expand Up @@ -946,6 +976,7 @@ int main(void)

init_test_verbosity(-1);
ret += test_basenamecpy();
ret += test_basename();
ret += test_bitmasks();
ret += test_strlcpy();
ret += test_strlcat();
Expand Down

0 comments on commit e5004de

Please sign in to comment.