noreturn tests: Avoid test failure on Solaris 10/x86 with cc.
[gnulib.git] / m4 / calloc.m4
blobeeeb033d8b97f75411eb3862813d155b5d35a649
1 # calloc.m4 serial 23
3 # Copyright (C) 2004-2021 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;
42               typedef struct { char c[8]; } S8;
43               size_t n = (size_t) -1 / sizeof (S8) + 2;
44               S8 * volatile s = calloc (n, sizeof (S8));
45               if (s)
46                 {
47                   s[0].c[0] = 1;
48                   if (s[n - 1].c[0])
49                     result = 0;
50                   else
51                     result = 2;
52                 }
53               else
54                 result = 3;
55               free (s);
56               return result;
57             ]])],
58          dnl The exit code of this program is 0 if calloc() succeeded with a
59          dnl wrap-around bug (which it shouldn't), 2 if calloc() succeeded in
60          dnl a non-flat address space, 3 if calloc() failed, or 1 if some leak
61          dnl sanitizer terminated the program as a result of the calloc() call.
62          [ac_cv_func_calloc_0_nonnull=no],
63          [])
64      else
65        case "$host_os" in
66                         # Guess yes on glibc systems.
67          *-gnu* | gnu*) ac_cv_func_calloc_0_nonnull="guessing yes" ;;
68                         # Guess yes on musl systems.
69          *-musl*)       ac_cv_func_calloc_0_nonnull="guessing yes" ;;
70                         # Guess yes on native Windows.
71          mingw*)        ac_cv_func_calloc_0_nonnull="guessing yes" ;;
72                         # If we don't know, obey --enable-cross-guesses.
73          *)             ac_cv_func_calloc_0_nonnull="$gl_cross_guess_normal" ;;
74        esac
75      fi
76     ])
77   case "$ac_cv_func_calloc_0_nonnull" in
78     *yes)
79       $1
80       ;;
81     *)
82       $2
83       ;;
84   esac
85 ])# AC_FUNC_CALLOC
88 # gl_FUNC_CALLOC_GNU
89 # ------------------
90 # Report whether 'calloc (0, 0)' is properly handled, and replace calloc if
91 # needed.
92 AC_DEFUN([gl_FUNC_CALLOC_GNU],
94   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
95   _AC_FUNC_CALLOC_IF(
96     [AC_DEFINE([HAVE_CALLOC_GNU], [1],
97                [Define to 1 if your system has a GNU libc compatible 'calloc'
98                 function, and to 0 otherwise.])],
99     [AC_DEFINE([HAVE_CALLOC_GNU], [0])
100      REPLACE_CALLOC=1
101     ])
102 ])# gl_FUNC_CALLOC_GNU
105 # gl_FUNC_CALLOC_POSIX
106 # --------------------
107 # Test whether 'calloc' is POSIX compliant (sets errno to ENOMEM when it
108 # fails), and replace calloc if it is not.
109 AC_DEFUN([gl_FUNC_CALLOC_POSIX],
111   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
112   AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
113   if test $gl_cv_func_malloc_posix = yes; then
114     AC_DEFINE([HAVE_CALLOC_POSIX], [1],
115       [Define if the 'calloc' function is POSIX compliant.])
116   else
117     REPLACE_CALLOC=1
118   fi