Debian Codegraph @

m4/realloc.m4

raw
1 # realloc.m4
2 # serial 39
3 dnl Copyright (C) 2007, 2009-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 # An an experimental option, the user can request a sanitized realloc()
10 # implementation, i.e. one that aborts upon undefined behaviour,
11 # by setting
12 # gl_cv_func_realloc_sanitize=yes
13 # at configure time.
14 AC_DEFUN([gl_FUNC_REALLOC_SANITIZED],
15 [
16 AC_CACHE_CHECK([whether realloc should abort upon undefined behaviour],
17 [gl_cv_func_realloc_sanitize],
18 [test -n "$gl_cv_func_realloc_sanitize" || gl_cv_func_realloc_sanitize=no])
19 ])
20
21 # gl_FUNC_REALLOC_POSIX
22 # ---------------------
23 # Test whether 'realloc' is POSIX compliant (sets errno to ENOMEM when it
24 # fails, and doesn't mess up with ptrdiff_t overflow),
25 # and replace realloc if it is not.
26 AC_DEFUN([gl_FUNC_REALLOC_POSIX],
27 [
28 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
29 AC_REQUIRE([gl_FUNC_MALLOC_POSIX])
30 AC_REQUIRE([gl_FUNC_REALLOC_SANITIZED])
31 if test "$gl_cv_func_realloc_sanitize" != no; then
32 REPLACE_REALLOC_FOR_REALLOC_POSIX=1
33 AC_DEFINE([NEED_SANITIZED_REALLOC], [1],
34 [Define to 1 if realloc should abort upon undefined behaviour.])
35 else
36 REPLACE_REALLOC_FOR_REALLOC_POSIX=$REPLACE_MALLOC_FOR_MALLOC_POSIX
37 fi
38 ])
39
40 # gl_FUNC_REALLOC_0_NONNULL
41 # -------------------------
42 # Replace realloc if realloc (..., 0) returns null.
43 # Modules that use this macro directly or indirectly should depend
44 # on extensions-aix, so that _LINUX_SOURCE_COMPAT gets defined
45 # before this macro gets invoked. This helps if !(__VEC__ || __AIXVEC),
46 # and doesn't hurt otherwise.
47 AC_DEFUN([gl_FUNC_REALLOC_0_NONNULL],
48 [
49 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
50 AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
51 AC_REQUIRE([gl_FUNC_REALLOC_POSIX])
52 AC_CACHE_CHECK([whether realloc (..., 0) returns nonnull],
53 [gl_cv_func_realloc_0_nonnull],
54 [AC_RUN_IFELSE(
55 [AC_LANG_PROGRAM(
56 [[#include <stdlib.h>
57 /* Use prealloc to test; "volatile" prevents the compiler
58 from optimizing the realloc call away. */
59 void *(*volatile prealloc) (void *, size_t) = realloc;]],
60 [[void *p = prealloc (0, 0);
61 int result = !p;
62 p = prealloc (p, 0);
63 result |= !p;
64 free (p);
65 return result;]])],
66 [gl_cv_func_realloc_0_nonnull=yes],
67 [gl_cv_func_realloc_0_nonnull=no],
68 [AS_CASE([$host_os],
69 [# Guess yes on platforms where we know the result.
70 freebsd* | netbsd* | openbsd* | darwin* | bitrig* \
71 | *-musl* | midipix* | midnightbsd* \
72 | hpux* | solaris* | cygwin*],
73 [gl_cv_func_realloc_0_nonnull="guessing yes"],
74 [# Guess as follows if we don't know.
75 gl_cv_func_realloc_0_nonnull=$gl_cross_guess_normal])])])
76 AS_CASE([$gl_cv_func_realloc_0_nonnull],
77 [*yes],
78 [AC_DEFINE([HAVE_REALLOC_0_NONNULL], [1],
79 [Define to 1 if realloc (..., 0) returns nonnull.])],
80 [AS_CASE([$gl_cv_func_realloc_sanitize,$gl_cv_malloc_ptrdiff,$gl_cv_func_malloc_posix,$host],
81 [yes,*,*,* | *,no,*,* | *,*,*no,* | *,*,*,aarch64c-*-freebsd*],
82 [REPLACE_REALLOC_FOR_REALLOC_POSIX=1],
83 [# Optimize for common case of glibc 2.1.1+ and compatibles.
84 REPLACE_REALLOC_FOR_REALLOC_POSIX=2])])
85 ])