From 49d56f323e7636e0aa26d45485fb9effe67cee2c Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Tue, 26 May 2015 13:55:18 -0600 Subject: [PATCH] Add strnlen() replacement needed for glob.c. Only used if no glob() and no strnlen(). --- MANIFEST | 1 + config.h.in | 3 +++ configure | 26 ++++++++++++++++++++++++++ configure.ac | 4 ++++ include/sudo_compat.h | 5 +++++ lib/util/strnlen.c | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+) create mode 100644 lib/util/strnlen.c diff --git a/MANIFEST b/MANIFEST index 99ff97eca..0dab8eaeb 100644 --- a/MANIFEST +++ b/MANIFEST @@ -159,6 +159,7 @@ lib/util/siglist.in lib/util/snprintf.c lib/util/strlcat.c lib/util/strlcpy.c +lib/util/strnlen.c lib/util/strsignal.c lib/util/strtobool.c lib/util/strtoid.c diff --git a/config.h.in b/config.h.in index dd5590ee0..5114d2830 100644 --- a/config.h.in +++ b/config.h.in @@ -662,6 +662,9 @@ /* Define to 1 if you have the `strlcpy' function. */ #undef HAVE_STRLCPY +/* Define to 1 if you have the `strnlen' function. */ +#undef HAVE_STRNLEN + /* Define to 1 if you have the `strsignal' function. */ #undef HAVE_STRSIGNAL diff --git a/configure b/configure index c14828b59..41174242c 100755 --- a/configure +++ b/configure @@ -18930,6 +18930,32 @@ esac " done + for ac_func in strnlen +do : + ac_fn_c_check_func "$LINENO" "strnlen" "ac_cv_func_strnlen" +if test "x$ac_cv_func_strnlen" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_STRNLEN 1 +_ACEOF + +else + + case " $LIBOBJS " in + *" strnlen.$ac_objext "* ) ;; + *) LIBOBJS="$LIBOBJS strnlen.$ac_objext" + ;; +esac + + + for _sym in sudo_strnlen; do + COMPAT_EXP="${COMPAT_EXP}${_sym} +" + done + + +fi +done + fi done diff --git a/configure.ac b/configure.ac index ab879c116..09ec91543 100644 --- a/configure.ac +++ b/configure.ac @@ -2574,6 +2574,10 @@ SUDO_FUNC_ISBLANK AC_CHECK_FUNCS([glob], [], [ AC_LIBOBJ(glob) SUDO_APPEND_COMPAT_EXP(sudo_glob sudo_globfree) + AC_CHECK_FUNCS([strnlen], [], [ + AC_LIBOBJ(strnlen) + SUDO_APPEND_COMPAT_EXP(sudo_strnlen) + ]) ]) AC_CHECK_FUNCS([memrchr], [], [ AC_LIBOBJ(memrchr) diff --git a/include/sudo_compat.h b/include/sudo_compat.h index 7f9aae68a..2efa5a7ca 100644 --- a/include/sudo_compat.h +++ b/include/sudo_compat.h @@ -431,6 +431,11 @@ __dso_public size_t sudo_strlcpy(char *dst, const char *src, size_t siz); # undef strlcpy # define strlcpy(_a, _b, _c) sudo_strlcpy((_a), (_b), (_c)) #endif /* HAVE_STRLCPY */ +#if !defined(HAVE_GLOB) && !defined(HAVE_STRNLEN) +__dso_public size_t sudo_strnlen(char *str, size_t maxlen); +# undef strnlen +# define strnlen(_a, _b) sudo_strnlen((_a), (_b)) +#endif /* !HAVE_GLOB && !HAVE_STRNLEN */ #ifndef HAVE_MEMRCHR __dso_public void *sudo_memrchr(const void *s, int c, size_t n); # undef memrchr diff --git a/lib/util/strnlen.c b/lib/util/strnlen.c new file mode 100644 index 000000000..1502ad0f5 --- /dev/null +++ b/lib/util/strnlen.c @@ -0,0 +1,36 @@ +/* $OpenBSD: strnlen.c,v 1.5 2014/06/10 04:17:37 deraadt Exp $ */ + +/* + * Copyright (c) 2010 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#ifndef HAVE_STRNLEN + +#include + +size_t +strnlen(const char *str, size_t maxlen) +{ + const char *cp; + + for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--) + ; + + return (size_t)(cp - str); +} + +#endif /* HAVE_STRNLEN */ -- 2.40.0