calloc-gnu: Avoid wrong configure results with GCC's AddressSanitizer.
[gnulib.git] / m4 / calloc.m4
bloba93439e8c8f4391084b54cd43c39fe96b2ab9c07
1 # calloc.m4 serial 22
3 # Copyright (C) 2004-2020 Free Software Foundation, Inc.
4 # This file is free software; the Free Software Foundation
5 # gives unlimited permission to copy and/or distribute it,
6 # with or without modifications, as long as this notice is preserved.
8 # Written by Jim Meyering.
10 # Determine whether calloc (N, S) returns non-NULL when N*S is zero,
11 # and returns NULL when N*S overflows.
12 # If so, define HAVE_CALLOC.  Otherwise, define calloc to rpl_calloc
13 # and arrange to use a calloc wrapper function that does work in that case.
15 # _AC_FUNC_CALLOC_IF([IF-WORKS], [IF-NOT])
16 # -------------------------------------
17 # If 'calloc (0, 0)' is properly handled, run IF-WORKS, otherwise, IF-NOT.
18 AC_DEFUN([_AC_FUNC_CALLOC_IF],
20   AC_REQUIRE([AC_TYPE_SIZE_T])dnl
21   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
22   AC_CACHE_CHECK([for GNU libc compatible calloc],
23     [ac_cv_func_calloc_0_nonnull],
24     [if test $cross_compiling != yes; then
25        ac_cv_func_calloc_0_nonnull=yes
26        AC_RUN_IFELSE(
27          [AC_LANG_PROGRAM(
28             [AC_INCLUDES_DEFAULT],
29             [[int result = 0;
30               char * volatile p = calloc (0, 0);
31               if (!p)
32                 result |= 1;
33               free (p);
34               return result;
35             ]])],
36          [],
37          [ac_cv_func_calloc_0_nonnull=no])
38        AC_RUN_IFELSE(
39          [AC_LANG_PROGRAM(
40             [AC_INCLUDES_DEFAULT],
41             [[int result = 0;
42               char * volatile p = calloc ((size_t) -1 / 8 + 1, 8);
43               if (!p)
44                 result |= 2;
45               free (p);
46               return result;
47             ]])],
48          dnl The exit code of this program is 0 if calloc() succeeded (which
49          dnl it shouldn't), 2 if calloc() failed, or 1 if some leak sanitizer
50          dnl terminated the program as a result of the calloc() call.
51          [ac_cv_func_calloc_0_nonnull=no],
52          [])
53      else
54        case "$host_os" in
55                         # Guess yes on glibc systems.
56          *-gnu* | gnu*) ac_cv_func_calloc_0_nonnull="guessing yes" ;;
57                         # Guess yes on musl systems.
58          *-musl*)       ac_cv_func_calloc_0_nonnull="guessing yes" ;;
59                         # Guess yes on native Windows.
60          mingw*)        ac_cv_func_calloc_0_nonnull="guessing yes" ;;
61                         # If we don't know, obey --enable-cross-guesses.
62          *)             ac_cv_func_calloc_0_nonnull="$gl_cross_guess_normal" ;;
63        esac
64      fi
65     ])
66   case "$ac_cv_func_calloc_0_nonnull" in
67     *yes)
68       $1
69       ;;
70     *)
71       $2
72       ;;
73   esac
74 ])# AC_FUNC_CALLOC
77 # gl_FUNC_CALLOC_GNU
78 # ------------------
79 # Report whether 'calloc (0, 0)' is properly handled, and replace calloc if
80 # needed.
81 AC_DEFUN([gl_FUNC_CALLOC_GNU],
83   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
84   _AC_FUNC_CALLOC_IF(
85     [AC_DEFINE([HAVE_CALLOC_GNU], [1],
86                [Define to 1 if your system has a GNU libc compatible 'calloc'
87                 function, and to 0 otherwise.])],
88     [AC_DEFINE([HAVE_CALLOC_GNU], [0])
89      REPLACE_CALLOC=1
90     ])
91 ])# gl_FUNC_CALLOC_GNU
94 # gl_FUNC_CALLOC_POSIX
95 # --------------------
96 # Test whether 'calloc' is POSIX compliant (sets errno to ENOMEM when it
97 # fails), and replace calloc if it is not.
98 AC_DEFUN([gl_FUNC_CALLOC_POSIX],
100   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
101   AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
102   if test $gl_cv_func_malloc_posix = yes; then
103     AC_DEFINE([HAVE_CALLOC_POSIX], [1],
104       [Define if the 'calloc' function is POSIX compliant.])
105   else
106     REPLACE_CALLOC=1
107   fi