Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shared: use memdup over stdndupa #92

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ CC_CHECK_FUNC_BUILTIN([__builtin_uaddll_overflow], [ ], [ ])
# dietlibc doesn't have st.st_mtim struct member
AC_CHECK_MEMBERS([struct stat.st_mtim], [], [], [#include <sys/stat.h>])

# musl 1.0 and bionic 4.4 don't have strndupa
# basename may be only available in libgen.h with the POSIX behavior,
# not desired here
AC_CHECK_DECLS_ONCE([[strndupa], [basename]], [], [], [[#include <string.h>]])
AC_CHECK_DECLS_ONCE([[basename]], [], [], [[#include <string.h>]])

AC_MSG_CHECKING([whether _Static_assert() is supported])
AC_COMPILE_IFELSE(
Expand Down
12 changes: 0 additions & 12 deletions shared/missing.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ static inline int finit_module(int fd, const char *uargs, int flags)
}
#endif

#if !HAVE_DECL_STRNDUPA
#include <string.h>
#define strndupa(s, n) \
({ \
const char *__old = (s); \
size_t __len = strnlen(__old, (n)); \
char *__new = alloca(__len + 1); \
__new[__len] = '\0'; \
memcpy(__new, __old, __len); \
})
#endif

#if !HAVE_DECL_BASENAME
#include <string.h>
static inline const char *basename(const char *s)
Expand Down
9 changes: 7 additions & 2 deletions shared/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,14 @@ static inline int is_dir(const char *path)

int mkdir_p(const char *path, int len, mode_t mode)
{
char *start, *end;
_cleanup_free_ char *start;
char *end;

start = strndupa(path, len);
start = memdup(path, len + 1);
if (!start)
return -ENOMEM;

start[len] = '\0';
end = start + len;

/*
Expand Down