From: Martin Matuska Date: Tue, 8 Dec 2020 01:26:52 +0000 (+0100) Subject: Use built-in strnlen on platforms where not available X-Git-Tag: v3.5.1~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c581b317cc71f964b2446b5534ebcc07be2f7cab;p=thirdparty%2Flibarchive.git Use built-in strnlen on platforms where not available Fixes #1464 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index ab3bcf1f7..9d8fc3c46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1378,6 +1378,7 @@ CHECK_FUNCTION_EXISTS_GLIBC(strchr HAVE_STRCHR) CHECK_FUNCTION_EXISTS_GLIBC(strdup HAVE_STRDUP) CHECK_FUNCTION_EXISTS_GLIBC(strerror HAVE_STRERROR) CHECK_FUNCTION_EXISTS_GLIBC(strncpy_s HAVE_STRNCPY_S) +CHECK_FUNCTION_EXISTS_GLIBC(strnlen HAVE_STRNLEN) CHECK_FUNCTION_EXISTS_GLIBC(strrchr HAVE_STRRCHR) CHECK_FUNCTION_EXISTS_GLIBC(symlink HAVE_SYMLINK) CHECK_FUNCTION_EXISTS_GLIBC(timegm HAVE_TIMEGM) diff --git a/build/cmake/config.h.in b/build/cmake/config.h.in index 6d2fa7469..ff629f9ce 100644 --- a/build/cmake/config.h.in +++ b/build/cmake/config.h.in @@ -959,6 +959,9 @@ typedef uint64_t uintmax_t; /* Define to 1 if you have the `strchr' function. */ #cmakedefine HAVE_STRCHR 1 +/* Define to 1 if you have the `strnlen' function. */ +#cmakedefine HAVE_STRNLEN 1 + /* Define to 1 if you have the `strdup' function. */ #cmakedefine HAVE_STRDUP 1 diff --git a/configure.ac b/configure.ac index 94df60815..cb823bd47 100644 --- a/configure.ac +++ b/configure.ac @@ -654,8 +654,8 @@ AC_CHECK_FUNCS([mkdir mkfifo mknod mkstemp]) AC_CHECK_FUNCS([nl_langinfo openat pipe poll posix_spawnp readlink readlinkat]) AC_CHECK_FUNCS([readpassphrase]) AC_CHECK_FUNCS([select setenv setlocale sigaction statfs statvfs]) -AC_CHECK_FUNCS([strchr strdup strerror strncpy_s strrchr symlink timegm]) -AC_CHECK_FUNCS([tzset unlinkat unsetenv utime utimensat utimes vfork]) +AC_CHECK_FUNCS([strchr strdup strerror strncpy_s strnlen strrchr symlink]) +AC_CHECK_FUNCS([timegm tzset unlinkat unsetenv utime utimensat utimes vfork]) AC_CHECK_FUNCS([wcrtomb wcscmp wcscpy wcslen wctomb wmemcmp wmemcpy wmemmove]) AC_CHECK_FUNCS([_ctime64_s _fseeki64]) AC_CHECK_FUNCS([_get_timezone _gmtime64_s _localtime64_s _mkgmtime64]) diff --git a/libarchive/archive_read_support_format_mtree.c b/libarchive/archive_read_support_format_mtree.c index 127706d53..93ba2959a 100644 --- a/libarchive/archive_read_support_format_mtree.c +++ b/libarchive/archive_read_support_format_mtree.c @@ -136,6 +136,9 @@ static int skip(struct archive_read *a); static int read_header(struct archive_read *, struct archive_entry *); static int64_t mtree_atol(char **, int base); +#ifndef HAVE_STRNLEN +static size_t mtree_strnlen(const char *, size_t); +#endif /* * There's no standard for TIME_T_MAX/TIME_T_MIN. So we compute them @@ -187,6 +190,24 @@ get_time_t_min(void) #endif } +#ifdef HAVE_STRNLEN +#define mtree_strnlen(a,b) strnlen(a,b) +#else +static size_t +mtree_strnlen(const char *p, size_t maxlen) +{ + size_t i; + + for (i = 0; i <= maxlen; i++) { + if (p[i] == 0) + break; + } + if (i > maxlen) + return (-1);/* invalid */ + return (i); +} +#endif + static int archive_read_format_mtree_options(struct archive_read *a, const char *key, const char *val) @@ -1540,7 +1561,7 @@ parse_digest(struct archive_read *a, struct archive_entry *entry, len *= 2; - if (strnlen(digest, len+1) != len) { + if (mtree_strnlen(digest, len+1) != len) { archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "incorrect digest length, ignoring"); return ARCHIVE_WARN;