From ec9cee7fc2f4407597597b216791d2980d656e5f Mon Sep 17 00:00:00 2001 From: Wouter Wijngaards Date: Tue, 27 Oct 2015 13:47:50 +0000 Subject: [PATCH] - portability, replace snprintf if return value broken. git-svn-id: file:///svn/unbound/trunk@3518 be551aaa-1e26-0410-a405-d3ace91eadb9 --- config.h.in | 9 +++++---- configure | 42 ++++++++++++++++++++++++++++++++++++++++++ configure.ac | 22 +++++++++++++++++++++- doc/Changelog | 1 + 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/config.h.in b/config.h.in index 5eb81c79d..32a9d88c6 100644 --- a/config.h.in +++ b/config.h.in @@ -542,6 +542,9 @@ /* The size of `time_t', as computed by sizeof. */ #undef SIZEOF_TIME_T +/* define if (v)snprintf does not return length needed, (but length used) */ +#undef SNPRINTF_RET_BROKEN + /* Define to 1 if you have the ANSI C header files. */ #undef STDC_HEADERS @@ -855,15 +858,13 @@ #define MAXHOSTNAMELEN 256 #endif - -#ifndef HAVE_SNPRINTF +#if !defined(HAVE_SNPRINTF) || defined(SNPRINTF_RET_BROKEN) #define snprintf snprintf_unbound #define vsnprintf vsnprintf_unbound #include int snprintf (char *str, size_t count, const char *fmt, ...); int vsnprintf (char *str, size_t count, const char *fmt, va_list arg); -#endif /* HAVE_SNPRINTF */ - +#endif /* HAVE_SNPRINTF or SNPRINTF_RET_BROKEN */ #ifndef HAVE_INET_PTON #define inet_pton inet_pton_unbound diff --git a/configure b/configure index 1e729af42..bc763f95b 100755 --- a/configure +++ b/configure @@ -18237,6 +18237,48 @@ esac fi +# test if snprintf return the proper length +if test "x$ac_cv_func_snprintf" = xyes; then + if test c${cross_compiling} = cno; then + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for correct snprintf return value" >&5 +$as_echo_n "checking for correct snprintf return value... " >&6; } + if test "$cross_compiling" = yes; then : + { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 +$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} +as_fn_error $? "cannot run test program while cross compiling +See \`config.log' for more details" "$LINENO" 5; } +else + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +$ac_includes_default + +int main(void) { return (snprintf(NULL, 0, "test") == 5); } + +_ACEOF +if ac_fn_c_try_run "$LINENO"; then : + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } +else + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + +$as_echo "#define SNPRINTF_RET_BROKEN /**/" >>confdefs.h + + case " $LIBOBJS " in + *" snprintf.$ac_objext "* ) ;; + *) LIBOBJS="$LIBOBJS snprintf.$ac_objext" + ;; +esac + + +fi +rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \ + conftest.$ac_objext conftest.beam conftest.$ac_ext +fi + + fi +fi ac_fn_c_check_func "$LINENO" "strlcat" "ac_cv_func_strlcat" if test "x$ac_cv_func_strlcat" = xyes; then : $as_echo "#define HAVE_STRLCAT 1" >>confdefs.h diff --git a/configure.ac b/configure.ac index 17036184e..79367280c 100644 --- a/configure.ac +++ b/configure.ac @@ -1002,6 +1002,20 @@ AC_REPLACE_FUNCS(inet_aton) AC_REPLACE_FUNCS(inet_pton) AC_REPLACE_FUNCS(inet_ntop) AC_REPLACE_FUNCS(snprintf) +# test if snprintf return the proper length +if test "x$ac_cv_func_snprintf" = xyes; then + if test c${cross_compiling} = cno; then + AC_MSG_CHECKING([for correct snprintf return value]) + AC_RUN_IFELSE([AC_LANG_SOURCE(AC_INCLUDES_DEFAULT +[[ +int main(void) { return (snprintf(NULL, 0, "test") == 5); } +]])], [AC_MSG_RESULT(yes)], [ + AC_MSG_RESULT(no) + AC_DEFINE([SNPRINTF_RET_BROKEN], [], [define if (v)snprintf does not return length needed, (but length used)]) + AC_LIBOBJ(snprintf) + ]) + fi +fi AC_REPLACE_FUNCS(strlcat) AC_REPLACE_FUNCS(strlcpy) AC_REPLACE_FUNCS(memmove) @@ -1241,7 +1255,13 @@ AHX_CONFIG_FORMAT_ATTRIBUTE AHX_CONFIG_UNUSED_ATTRIBUTE AHX_CONFIG_FSEEKO AHX_CONFIG_MAXHOSTNAMELEN -AHX_CONFIG_SNPRINTF(unbound) +#if !defined(HAVE_SNPRINTF) || defined(SNPRINTF_RET_BROKEN) +#define snprintf snprintf_unbound +#define vsnprintf vsnprintf_unbound +#include +int snprintf (char *str, size_t count, const char *fmt, ...); +int vsnprintf (char *str, size_t count, const char *fmt, va_list arg); +#endif /* HAVE_SNPRINTF or SNPRINTF_RET_BROKEN */ AHX_CONFIG_INET_PTON(unbound) AHX_CONFIG_INET_NTOP(unbound) AHX_CONFIG_INET_ATON(unbound) diff --git a/doc/Changelog b/doc/Changelog index 620e69a62..2b0b5a66f 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -2,6 +2,7 @@ - isblank() compat implementation. - detect libexpat without xml_StopParser function. - portability fixes. + - portability, replace snprintf if return value broken. 23 October 2015: Wouter - Fix #714: Document config to block private-address for IPv4 -- 2.47.2