From 5ea98697b9f786ac73fd5152d96aebe02906a10d Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Fri, 9 Oct 2015 12:01:46 -0400 Subject: [PATCH 1/1] Port: Add compat for strnlen and strndup MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- configure.ac | 2 +- formats/ctf/types/string.c | 4 +- formats/lttng-live/lttng-live-comm.c | 6 +-- include/babeltrace/compat/string.h | 62 ++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/configure.ac b/configure.ac index 971cee55..0edc2a3c 100644 --- a/configure.ac +++ b/configure.ac @@ -81,7 +81,7 @@ AC_TYPE_SIZE_T # Checks for library functions. AC_FUNC_MALLOC AC_FUNC_MMAP -AC_CHECK_FUNCS([gettimeofday munmap strtoul]) +AC_CHECK_FUNCS([gettimeofday munmap strtoul strndup strnlen]) # Check for MinGW32. MINGW32=no diff --git a/formats/ctf/types/string.c b/formats/ctf/types/string.c index 002f1b4f..c455f9b2 100644 --- a/formats/ctf/types/string.c +++ b/formats/ctf/types/string.c @@ -29,7 +29,7 @@ #include #include #include /* C99 limits */ -#include +#include int ctf_string_read(struct bt_stream_pos *ppos, struct bt_definition *definition) { @@ -53,7 +53,7 @@ int ctf_string_read(struct bt_stream_pos *ppos, struct bt_definition *definition if (max_len_bits < 0) return -EFAULT; /* Add \0, counting in bytes. */ - len = strnlen(srcaddr, (size_t) max_len_bits / CHAR_BIT) + 1; + len = bt_strnlen(srcaddr, (size_t) max_len_bits / CHAR_BIT) + 1; /* Truncated string, unexpected. Trace probably corrupted. */ if (srcaddr[len - 1] != '\0') return -EFAULT; diff --git a/formats/lttng-live/lttng-live-comm.c b/formats/lttng-live/lttng-live-comm.c index 485dabba..d26d83eb 100644 --- a/formats/lttng-live/lttng-live-comm.c +++ b/formats/lttng-live/lttng-live-comm.c @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -53,6 +52,7 @@ #include #include +#include #include "lttng-live.h" #include "lttng-viewer-abi.h" @@ -278,8 +278,8 @@ void update_session_list(GPtrArray *session_list, char *hostname, return; relay_session = g_new0(struct lttng_live_relay_session, 1); - relay_session->hostname = strndup(hostname, MAXNAMLEN); - relay_session->name = strndup(session_name, MAXNAMLEN); + relay_session->hostname = bt_strndup(hostname, MAXNAMLEN); + relay_session->name = bt_strndup(session_name, MAXNAMLEN); relay_session->clients = clients; relay_session->streams = streams; relay_session->timer = timer; diff --git a/include/babeltrace/compat/string.h b/include/babeltrace/compat/string.h index 5d6a0632..7a574e67 100644 --- a/include/babeltrace/compat/string.h +++ b/include/babeltrace/compat/string.h @@ -24,6 +24,7 @@ */ #include +#include #if !defined(__linux__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) @@ -51,4 +52,65 @@ int compat_strerror_r(int errnum, char *buf, size_t buflen) #endif +#ifdef HAVE_STRNLEN +static inline +size_t bt_strnlen(const char *str, size_t max) +{ + return strnlen(str, max); +} +#else +static inline +size_t bt_strnlen(const char *str, size_t max) +{ + size_t ret; + const char *end; + + end = memchr(str, 0, max); + + if (end) { + ret = (size_t) (end - str); + } else { + ret = max; + } + + return ret; +} +#endif /* HAVE_STRNLEN */ + +#ifdef HAVE_STRNDUP +static inline +char *bt_strndup(const char *s, size_t n) +{ + return strndup(s, n); +} +#else +static inline +char *bt_strndup(const char *s, size_t n) +{ + char *ret; + size_t navail; + + if (!s) { + ret = NULL; + goto end; + } + + /* min() */ + navail = strlen(s) + 1; + if ((n + 1) < navail) { + navail = n + 1; + } + + ret = malloc(navail); + if (!ret) { + goto end; + } + + memcpy(ret, s, navail); + ret[navail - 1] = '\0'; +end: + return ret; +} +#endif /* HAVE_STRNDUP */ + #endif /* _BABELTRACE_COMPAT_STRING_H */ -- 2.34.1