2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 01:49:11 +00:00

Add strndup() for those without it. As strndup.c uses strnlen(),

use our own if it is missing.
This commit is contained in:
Todd C. Miller 2015-05-26 14:05:26 -06:00
parent 8437e3606b
commit 82286d8b7b
8 changed files with 137 additions and 33 deletions

View File

@ -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

View File

@ -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

78
configure vendored
View File

@ -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

View File

@ -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, [

View File

@ -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

View File

@ -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

61
lib/util/strndup.c Normal file
View File

@ -0,0 +1,61 @@
/* $OpenBSD: strndup.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */
/*
* Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
*
* 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 <config.h>
#ifndef HAVE_STRNDUP
#include <sys/types.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif /* STDC_HEADERS */
#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
# include <malloc.h>
#endif /* HAVE_MALLOC_H && !STDC_HEADERS */
#ifdef HAVE_STRING_H
# include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
# include <strings.h>
#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 */

View File

@ -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;