From 9bed827b18cc4f27fb7cd7c02ad301519eca6d29 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 17 Aug 2018 10:37:59 -0400 Subject: [PATCH] Fix configure's snprintf test so it exposes HP-UX bug. Since commit e1d19c902, buildfarm member gharial has been failing with symptoms indicating that snprintf sometimes returns -1 for buffer overrun, even though it passes the added configure check. Some google research suggests that this happens only in limited cases, such as when the overrun happens partway through a %d item. Adjust the configure check to exercise it that way. Since I'm now feeling more paranoid than I was before, also make the test explicitly verify that the buffer doesn't get physically overrun. --- config/c-library.m4 | 11 ++++++++--- configure | 9 ++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/config/c-library.m4 b/config/c-library.m4 index 6bcfceee6e..da7fa77303 100644 --- a/config/c-library.m4 +++ b/config/c-library.m4 @@ -202,7 +202,7 @@ AC_MSG_RESULT([$pgac_cv_snprintf_arg_control]) ])# PGAC_FUNC_SNPRINTF_ARG_CONTROL # PGAC_FUNC_SNPRINTF_SIZE_T_SUPPORT -# --------------------------------------- +# --------------------------------- # Determine if snprintf supports the z length modifier for printing # size_t-sized variables. That's supported by C99 and POSIX but not # all platforms play ball, so we must test whether it's working. @@ -243,9 +243,11 @@ AC_MSG_RESULT([$pgac_cv_snprintf_size_t_support]) # Determine whether snprintf returns the desired buffer length when # it overruns the actual buffer length. That's required by C99 and POSIX # but ancient platforms don't behave that way, so we must test. +# While we're at it, let's just verify that it doesn't physically overrun +# the buffer. # AC_DEFUN([PGAC_FUNC_SNPRINTF_C99_RESULT], -[AC_MSG_CHECKING([whether snprintf reports buffer overrun per C99]) +[AC_MSG_CHECKING([whether snprintf handles buffer overrun per C99]) AC_CACHE_VAL(pgac_cv_snprintf_c99_result, [AC_RUN_IFELSE([AC_LANG_SOURCE([[#include #include @@ -254,7 +256,10 @@ int main() { char buf[10]; - if (snprintf(buf, sizeof(buf), "12345678901234567890") != 20) + strcpy(buf, "abcdefghi"); + if (snprintf(buf, 4, "%d", 123456) != 6) + return 1; + if (strcmp(buf, "123") != 0 || buf[4] != 'e') return 1; return 0; }]])], diff --git a/configure b/configure index cecd96a14f..836d68dad3 100755 --- a/configure +++ b/configure @@ -16378,8 +16378,8 @@ fi # Force use of our snprintf if the system's doesn't handle buffer overrun # as specified by C99. if test "$pgac_need_repl_snprintf" = no; then - { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether snprintf reports buffer overrun per C99" >&5 -$as_echo_n "checking whether snprintf reports buffer overrun per C99... " >&6; } + { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether snprintf handles buffer overrun per C99" >&5 +$as_echo_n "checking whether snprintf handles buffer overrun per C99... " >&6; } if ${pgac_cv_snprintf_c99_result+:} false; then : $as_echo_n "(cached) " >&6 else @@ -16395,7 +16395,10 @@ int main() { char buf[10]; - if (snprintf(buf, sizeof(buf), "12345678901234567890") != 20) + strcpy(buf, "abcdefghi"); + if (snprintf(buf, 4, "%d", 123456) != 6) + return 1; + if (strcmp(buf, "123") != 0 || buf[4] != 'e') return 1; return 0; }