diff --git a/lib/calloc.c b/lib/calloc.c index f4545c2b2..31e1ef50c 100644 --- a/lib/calloc.c +++ b/lib/calloc.c @@ -31,6 +31,7 @@ #include <stdlib.h> #include <errno.h> +#include <string.h> /* Call the system's calloc below. */ #undef calloc @@ -44,6 +45,7 @@ rpl_calloc (size_t n, size_t s) void *result; #if NEED_CALLOC_GNU + size_t bytes = n * s; if (n == 0 || s == 0) { n = 1; @@ -53,7 +55,6 @@ rpl_calloc (size_t n, size_t s) { /* Defend against buggy calloc implementations that mishandle size_t overflow. */ - size_t bytes = n * s; if (bytes / s != n) { errno = ENOMEM; @@ -64,6 +65,14 @@ rpl_calloc (size_t n, size_t s) result = calloc (n, s); +#if NEED_CALLOC_GNU && defined __hpux + /* Work around HP-UX bug where resulting memory is not cleared. See: + https://support.hpe.com/hpsc/doc/public/display?docId=pdb_na-PHCO_38658 + */ + if (result) + memset (result, 0, bytes); +#endif + #if !HAVE_CALLOC_POSIX if (result == NULL) errno = ENOMEM; diff --git a/m4/calloc.m4 b/m4/calloc.m4 index be10e211f..c6c070d0d 100644 --- a/m4/calloc.m4 +++ b/m4/calloc.m4 @@ -1,4 +1,4 @@ -# calloc.m4 serial 18 +# calloc.m4 serial 19 # Copyright (C) 2004-2018 Free Software Foundation, Inc. # This file is free software; the Free Software Foundation @@ -20,33 +20,39 @@ AC_DEFUN([_AC_FUNC_CALLOC_IF], AC_REQUIRE([AC_TYPE_SIZE_T])dnl AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles AC_CACHE_CHECK([for GNU libc compatible calloc], - [ac_cv_func_calloc_0_nonnull], - [AC_RUN_IFELSE( - [AC_LANG_PROGRAM( - [AC_INCLUDES_DEFAULT], - [[int result = 0; - char *p = calloc (0, 0); - if (!p) - result |= 1; - free (p); - p = calloc ((size_t) -1 / 8 + 1, 8); - if (p) - result |= 2; - free (p); - return result; - ]])], - [ac_cv_func_calloc_0_nonnull=yes], - [ac_cv_func_calloc_0_nonnull=no], - [case "$host_os" in - # Guess yes on glibc systems. - *-gnu* | gnu*) ac_cv_func_calloc_0_nonnull="guessing yes" ;; - # Guess yes on native Windows. - mingw*) ac_cv_func_calloc_0_nonnull="guessing yes" ;; - # If we don't know, assume the worst. - *) ac_cv_func_calloc_0_nonnull="guessing no" ;; - esac - ])]) - case "$ac_cv_func_calloc_0_nonnull" in + [ac_cv_func_gnu_compatible_calloc], + [case $host_os in + hpux*) + # Not easy to check for PHCO_38658, so guess no. + ac_cv_func_gnu_compatible_calloc="guessing no" ;; + *) + AC_RUN_IFELSE( + [AC_LANG_PROGRAM( + [AC_INCLUDES_DEFAULT], + [[int result = 0; + char *p = calloc (0, 0); + if (!p) + result |= 1; + free (p); + p = calloc ((size_t) -1 / 8 + 1, 8); + if (p) + result |= 2; + free (p); + return result; + ]])], + [ac_cv_func_gnu_compatible_calloc=yes], + [ac_cv_func_gnu_compatible_calloc=no], + [case $host_os in + # Guess yes on glibc systems. + *-gnu* | gnu*) ac_cv_func_gnu_compatible_calloc="guessing yes" ;; + # Guess yes on native Windows. + mingw*) ac_cv_func_gnu_compatible_calloc="guessing yes" ;; + # If we don't know, assume the worst. + *) ac_cv_func_gnu_compatible_calloc="guessing no" ;; + esac + ]);; + esac]) + case $ac_cv_func_gnu_compatible_calloc in *yes) $1 ;; diff --git a/modules/calloc-gnu b/modules/calloc-gnu index ffc8b50ef..5cba38ff4 100644 --- a/modules/calloc-gnu +++ b/modules/calloc-gnu @@ -21,7 +21,7 @@ Include: <stdlib.h> License: -GPL +LGPLv2+ Maintainer: Jim Meyering diff --git a/modules/regex b/modules/regex index 278510c4c..ce513cb91 100644 --- a/modules/regex +++ b/modules/regex @@ -19,6 +19,7 @@ ssize_t alloca-opt [test $ac_use_included_regex = yes] btowc [test $ac_use_included_regex = yes] builtin-expect [test $ac_use_included_regex = yes] +calloc-gnu [test $ac_use_included_regex = yes] intprops [test $ac_use_included_regex = yes] langinfo [test $ac_use_included_regex = yes] libc-config [test $ac_use_included_regex = yes]
Some HP-UX versions have a broken 'calloc' that could explain the bug. The
broken 'calloc' returns memory that isn't properly zeroed. Can you investigate
whether zeroing the calloc memory fixes the bug? Perhaps something like the
attached Gnulib patch? Thanks.
- regex and grep severely broken on HP-UX Bruno Haible
- Re: regex and grep severely broken on HP-UX Bruno Haible
- Re: regex and grep severely broken on HP-UX Bruno Haible
- Re: [Grep-devel] regex and grep severely broken ... Jim Meyering
- Re: [Grep-devel] regex and grep severely bro... Bruno Haible
- Re: [Grep-devel] regex and grep severel... Jim Meyering
- Re: [Grep-devel] regex and grep severel... Paul Eggert
- Re: [Grep-devel] regex and grep sev... Bruno Haible