Debian Codegraph @

m4/malloc.m4

raw
1 # malloc.m4 serial 31
2 dnl Copyright (C) 2007, 2009-2024 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 # This is adapted with modifications from upstream Autoconf here:
8 # https://git.savannah.gnu.org/cgit/autoconf.git/tree/lib/autoconf/functions.m4?id=v2.70#n949
9 AC_DEFUN([_AC_FUNC_MALLOC_IF],
10 [
11 AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
12 AC_CACHE_CHECK([whether malloc (0) returns nonnull],
13 [ac_cv_func_malloc_0_nonnull],
14 [AC_RUN_IFELSE(
15 [AC_LANG_PROGRAM(
16 [[#include <stdlib.h>
17 ]],
18 [[void *p = malloc (0);
19 void * volatile vp = p;
20 int result = !vp;
21 free (p);
22 return result;]])
23 ],
24 [ac_cv_func_malloc_0_nonnull=yes],
25 [ac_cv_func_malloc_0_nonnull=no],
26 [case "$host_os" in
27 # Guess yes on platforms where we know the result.
28 *-gnu* | freebsd* | netbsd* | openbsd* | bitrig* \
29 | gnu* | *-musl* | midipix* | midnightbsd* \
30 | hpux* | solaris* | cygwin* | mingw* | windows* | msys* )
31 ac_cv_func_malloc_0_nonnull="guessing yes" ;;
32 # If we don't know, obey --enable-cross-guesses.
33 *) ac_cv_func_malloc_0_nonnull="$gl_cross_guess_normal" ;;
34 esac
35 ])
36 ])
37 AS_CASE([$ac_cv_func_malloc_0_nonnull], [*yes], [$1], [$2])
38 ])# _AC_FUNC_MALLOC_IF
39
40 # gl_FUNC_MALLOC_GNU
41 # ------------------
42 # Replace malloc if it is not compatible with GNU libc.
43 AC_DEFUN([gl_FUNC_MALLOC_GNU],
44 [
45 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
46 AC_REQUIRE([gl_FUNC_MALLOC_POSIX])
47 REPLACE_MALLOC_FOR_MALLOC_GNU="$REPLACE_MALLOC_FOR_MALLOC_POSIX"
48 if test $REPLACE_MALLOC_FOR_MALLOC_GNU = 0; then
49 _AC_FUNC_MALLOC_IF([], [REPLACE_MALLOC_FOR_MALLOC_GNU=1])
50 fi
51 ])
52
53 # gl_FUNC_MALLOC_PTRDIFF
54 # ----------------------
55 # Test whether malloc (N) reliably fails when N exceeds PTRDIFF_MAX,
56 # and replace malloc otherwise.
57 AC_DEFUN([gl_FUNC_MALLOC_PTRDIFF],
58 [
59 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
60 AC_REQUIRE([gl_CHECK_MALLOC_PTRDIFF])
61 test "$gl_cv_malloc_ptrdiff" = yes || REPLACE_MALLOC_FOR_MALLOC_POSIX=1
62 ])
63
64 # Test whether malloc, realloc, calloc refuse to create objects
65 # larger than what can be expressed in ptrdiff_t.
66 # Set gl_cv_func_malloc_gnu to yes or no accordingly.
67 AC_DEFUN([gl_CHECK_MALLOC_PTRDIFF],
68 [
69 AC_CACHE_CHECK([whether malloc is ptrdiff_t safe],
70 [gl_cv_malloc_ptrdiff],
71 [AC_COMPILE_IFELSE(
72 [AC_LANG_PROGRAM(
73 [[#include <stdint.h>
74 ]],
75 [[/* 64-bit ptrdiff_t is so wide that no practical platform
76 can exceed it. */
77 #define WIDE_PTRDIFF (PTRDIFF_MAX >> 31 >> 31 != 0)
78
79 /* On rare machines where size_t fits in ptrdiff_t there
80 is no problem. */
81 #define NARROW_SIZE (SIZE_MAX <= PTRDIFF_MAX)
82
83 /* glibc 2.30 and later malloc refuses to exceed ptrdiff_t
84 bounds even on 32-bit platforms. We don't know which
85 non-glibc systems are safe. */
86 #define KNOWN_SAFE (2 < __GLIBC__ + (30 <= __GLIBC_MINOR__))
87
88 #if WIDE_PTRDIFF || NARROW_SIZE || KNOWN_SAFE
89 return 0;
90 #else
91 #error "malloc might not be ptrdiff_t safe"
92 syntax error
93 #endif
94 ]])],
95 [gl_cv_malloc_ptrdiff=yes],
96 [gl_cv_malloc_ptrdiff=no])
97 ])
98 ])
99
100 # gl_FUNC_MALLOC_POSIX
101 # --------------------
102 # Test whether 'malloc' is POSIX compliant (sets errno to ENOMEM when it
103 # fails, and doesn't mess up with ptrdiff_t overflow), and replace
104 # malloc if it is not.
105 AC_DEFUN([gl_FUNC_MALLOC_POSIX],
106 [
107 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
108 AC_REQUIRE([gl_FUNC_MALLOC_PTRDIFF])
109 AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
110 if test "$gl_cv_func_malloc_posix" = yes; then
111 AC_DEFINE([HAVE_MALLOC_POSIX], [1],
112 [Define if malloc, realloc, and calloc set errno on allocation failure.])
113 else
114 REPLACE_MALLOC_FOR_MALLOC_POSIX=1
115 fi
116 ])
117
118 # Test whether malloc, realloc, calloc set errno to ENOMEM on failure.
119 # Set gl_cv_func_malloc_posix to yes or no accordingly.
120 AC_DEFUN([gl_CHECK_MALLOC_POSIX],
121 [
122 AC_REQUIRE([AC_CANONICAL_HOST])
123 AC_CACHE_CHECK([whether malloc, realloc, calloc set errno on failure],
124 [gl_cv_func_malloc_posix],
125 [
126 dnl It is too dangerous to try to allocate a large amount of memory:
127 dnl some systems go to their knees when you do that. So assume that
128 dnl all Unix implementations of the function set errno on failure,
129 dnl except on those platforms where we have seen 'test-malloc-gnu',
130 dnl 'test-realloc-gnu', 'test-calloc-gnu' fail.
131 case "$host_os" in
132 mingw* | windows*)
133 gl_cv_func_malloc_posix=no ;;
134 irix* | solaris*)
135 dnl On IRIX 6.5, the three functions return NULL with errno unset
136 dnl when the argument is larger than PTRDIFF_MAX.
137 dnl On Solaris 11.3, the three functions return NULL with errno set
138 dnl to EAGAIN, not ENOMEM, when the argument is larger than
139 dnl PTRDIFF_MAX.
140 dnl Here is a test program:
141 m4_divert_push([KILL])
142 #include <errno.h>
143 #include <stdio.h>
144 #include <stdlib.h>
145 #define ptrdiff_t long
146 #ifndef PTRDIFF_MAX
147 # define PTRDIFF_MAX ((ptrdiff_t) ((1UL << (8 * sizeof (ptrdiff_t) - 1)) - 1))
148 #endif
149
150 int main ()
151 {
152 void *p;
153
154 fprintf (stderr, "PTRDIFF_MAX = %lu\n", (unsigned long) PTRDIFF_MAX);
155
156 errno = 0;
157 p = malloc ((unsigned long) PTRDIFF_MAX + 1);
158 fprintf (stderr, "p=%p errno=%d\n", p, errno);
159
160 errno = 0;
161 p = calloc (PTRDIFF_MAX / 2 + 1, 2);
162 fprintf (stderr, "p=%p errno=%d\n", p, errno);
163
164 errno = 0;
165 p = realloc (NULL, (unsigned long) PTRDIFF_MAX + 1);
166 fprintf (stderr, "p=%p errno=%d\n", p, errno);
167
168 return 0;
169 }
170 m4_divert_pop([KILL])
171 gl_cv_func_malloc_posix=no ;;
172 *)
173 gl_cv_func_malloc_posix=yes ;;
174 esac
175 ])
176 ])