From 82286d8b7b2cb235422aac427e47547d894470fc Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Tue, 26 May 2015 14:05:26 -0600 Subject: [PATCH] Add strndup() for those without it. As strndup.c uses strnlen(), use our own if it is missing. --- MANIFEST | 1 + config.h.in | 3 ++ configure | 78 ++++++++++++++++++++++++++++--------------- configure.ac | 12 ++++--- include/sudo_compat.h | 9 +++-- lib/util/Makefile.in | 4 +++ lib/util/strndup.c | 61 +++++++++++++++++++++++++++++++++ mkdep.pl | 2 +- 8 files changed, 137 insertions(+), 33 deletions(-) create mode 100644 lib/util/strndup.c diff --git a/MANIFEST b/MANIFEST index 0dab8eaeb..5360972ab 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/strndup.c lib/util/strnlen.c lib/util/strsignal.c lib/util/strtobool.c diff --git a/config.h.in b/config.h.in index 5114d2830..1a49f1e14 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 `strndup' function. */ +#undef HAVE_STRNDUP + /* Define to 1 if you have the `strnlen' function. */ #undef HAVE_STRNLEN diff --git a/configure b/configure index 41174242c..79a548213 100755 --- a/configure +++ b/configure @@ -18930,32 +18930,6 @@ 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 @@ -19087,6 +19061,58 @@ esac done +fi +done + +for ac_func in strndup +do : + ac_fn_c_check_func "$LINENO" "strndup" "ac_cv_func_strndup" +if test "x$ac_cv_func_strndup" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_STRNDUP 1 +_ACEOF + +else + + case " $LIBOBJS " in + *" strndup.$ac_objext "* ) ;; + *) LIBOBJS="$LIBOBJS strndup.$ac_objext" + ;; +esac + + + for _sym in sudo_strndup; do + COMPAT_EXP="${COMPAT_EXP}${_sym} +" + done + + +fi +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 diff --git a/configure.ac b/configure.ac index 09ec91543..99fcb9258 100644 --- a/configure.ac +++ b/configure.ac @@ -2574,10 +2574,6 @@ 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) @@ -2599,6 +2595,14 @@ AC_CHECK_FUNCS([strlcat], [], [ AC_LIBOBJ(strlcat) SUDO_APPEND_COMPAT_EXP(sudo_strlcat) ]) +AC_CHECK_FUNCS([strndup], [], [ + AC_LIBOBJ(strndup) + SUDO_APPEND_COMPAT_EXP(sudo_strndup) +]) +AC_CHECK_FUNCS([strnlen], [], [ + AC_LIBOBJ(strnlen) + SUDO_APPEND_COMPAT_EXP(sudo_strnlen) +]) AC_CHECK_FUNCS([clock_gettime], [], [ # On Solaris, clock_gettime is in librt AC_CHECK_LIB(rt, clock_gettime, [ diff --git a/include/sudo_compat.h b/include/sudo_compat.h index 2efa5a7ca..c5a7d4c42 100644 --- a/include/sudo_compat.h +++ b/include/sudo_compat.h @@ -431,11 +431,16 @@ __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) +#ifndef HAVE_STRNDUP +__dso_public char *sudo_strndup(const char *str, size_t maxlen); +# undef strndup +# define strndup(_a, _b) sudo_strndup((_a), (_b)) +#endif /* HAVE_STRNDUP */ +#ifndef 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 */ +#endif /* 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/Makefile.in b/lib/util/Makefile.in index b9cd86f37..46610c75b 100644 --- a/lib/util/Makefile.in +++ b/lib/util/Makefile.in @@ -485,6 +485,10 @@ strlcat.lo: $(srcdir)/strlcat.c $(incdir)/sudo_compat.h $(top_builddir)/config.h $(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(DEFS) $(srcdir)/strlcat.c strlcpy.lo: $(srcdir)/strlcpy.c $(incdir)/sudo_compat.h $(top_builddir)/config.h $(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(DEFS) $(srcdir)/strlcpy.c +strndup.lo: $(srcdir)/strndup.c $(incdir)/sudo_compat.h $(top_builddir)/config.h + $(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(DEFS) $(srcdir)/strndup.c +strnlen.lo: $(srcdir)/strnlen.c $(incdir)/sudo_compat.h $(top_builddir)/config.h + $(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(DEFS) $(srcdir)/strnlen.c strsignal.lo: $(srcdir)/strsignal.c $(incdir)/sudo_compat.h \ $(incdir)/sudo_gettext.h $(top_builddir)/config.h $(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(DEFS) $(srcdir)/strsignal.c diff --git a/lib/util/strndup.c b/lib/util/strndup.c new file mode 100644 index 000000000..8a13032ec --- /dev/null +++ b/lib/util/strndup.c @@ -0,0 +1,61 @@ +/* $OpenBSD: strndup.c,v 1.1 2010/05/18 22:24:55 tedu 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_STRNDUP + +#include + +#ifdef STDC_HEADERS +# include +# include +#else +# ifdef HAVE_STDLIB_H +# include +# endif +#endif /* STDC_HEADERS */ +#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS) +# include +#endif /* HAVE_MALLOC_H && !STDC_HEADERS */ +#ifdef HAVE_STRING_H +# include +#endif /* HAVE_STRING_H */ +#ifdef HAVE_STRINGS_H +# include +#endif /* HAVE_STRINGS_H */ + +#include "sudo_compat.h" + +char * +sudo_strndup(const char *str, size_t maxlen) +{ + char *copy; + size_t len; + + len = strnlen(str, maxlen); + copy = malloc(len + 1); + if (copy != NULL) { + (void)memcpy(copy, str, len); + copy[len] = '\0'; + } + + return copy; +} + +#endif /* HAVE_STRNDUP */ diff --git a/mkdep.pl b/mkdep.pl index 64a527e51..1dd4ea5ba 100755 --- a/mkdep.pl +++ b/mkdep.pl @@ -70,7 +70,7 @@ sub mkdep { $makefile =~ s:\@SUDOERS_OBJS\@:bsm_audit.lo linux_audit.lo ldap.lo solaris_audit.lo sssd.lo:; # XXX - fill in AUTH_OBJS from contents of the auth dir instead $makefile =~ s:\@AUTH_OBJS\@:afs.lo aix_auth.lo bsdauth.lo dce.lo fwtk.lo getspwuid.lo kerb5.lo pam.lo passwd.lo rfc1938.lo secureware.lo securid5.lo sia.lo:; - $makefile =~ s:\@LTLIBOBJS\@:closefrom.lo fnmatch.lo getaddrinfo.lo getcwd.lo getgrouplist.lo getline.lo getopt_long.lo glob.lo inet_ntop_lo inet_pton.lo isblank.lo memrchr.lo memset_s.lo mksiglist.lo mksigname.lo mktemp.lo pw_dup.lo sha2.lo sig2str.lo siglist.lo signame.lo snprintf.lo strlcat.lo strlcpy.lo strsignal.lo strtonum.lo utimens.lo:; + $makefile =~ s:\@LTLIBOBJS\@:closefrom.lo fnmatch.lo getaddrinfo.lo getcwd.lo getgrouplist.lo getline.lo getopt_long.lo glob.lo inet_ntop_lo inet_pton.lo isblank.lo memrchr.lo memset_s.lo mksiglist.lo mksigname.lo mktemp.lo pw_dup.lo sha2.lo sig2str.lo siglist.lo signame.lo snprintf.lo strlcat.lo strlcpy.lo strndup.lo strnlen.lo strsignal.lo strtonum.lo utimens.lo:; # Parse OBJS lines my %objs; -- 2.40.0