Subject: Re: bug#75551: Potentially erroneous long loop in lrealloc

From: Paul Eggert <[email-masked]>
Date: Thu, 16 Jan 2025 23:37:29 -0800

On 2025-01-16 13:46, Hong Xu wrote:
> After applying the patch to the master branch, I'm getting a memory
> exhaustion error when starting Emacs:

Oh, I was assuming sane malloc/realloc behavior and you tested on a 
platform which doesn't do that. Gnulib fixes that and I thought Emacs 
was already using Gnulib for this, but I now see that Emacs uses Gnulib 
to fix only malloc, not realloc.

Please try the attached two patches instead. The first one arranges for 
Emacs to also fix realloc. The second one is the same patch that I gave 
you earlier, the patch that simplifies the lrealloc code.

The MS-Windows port already fixes malloc and realloc in a different way, 
so the first patch should affect only non-MS-Windows platforms.

From a9ee0a5fae1f2551c6acfadae803c6d3ff769d03 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Thu, 16 Jan 2025 23:28:34 -0800
Subject: [PATCH 1/2] Let Gnulib deal with malloc/realloc 0

Instead of worrying about whether malloc and realloc yield a
non-null pointer when given size 0 (the traditional behavior
recommended by Doug McIlroy, and the only behavior that makes
sense for Emacs), let Gnulib arrange for the traditional behavior.
* admin/merge-gnulib (GNULIB_MODULES): Add malloc-gnu (which we
were already depending on indirectly) and realloc-posix.
* lib/gnulib.mk.in, m4/gnulib-comp.m4:
Regenerate by running admin/merge-gnulib.
---
 admin/merge-gnulib |  4 ++--
 lib/gnulib.mk.in   |  8 ++------
 m4/gnulib-comp.m4  | 43 ++++++++++---------------------------------
 3 files changed, 14 insertions(+), 41 deletions(-)

diff --git a/admin/merge-gnulib b/admin/merge-gnulib
index 2eab5ab8b9c..ae433961df3 100755
--- a/admin/merge-gnulib
+++ b/admin/merge-gnulib
@@ -41,11 +41,11 @@ GNULIB_MODULES=
   free-posix fstatat fsusage fsync futimens
   getline getloadavg getopt-gnu getrandom gettime gettimeofday gitlog-to-changelog
   ieee754-h ignore-value intprops largefile libgmp lstat
-  manywarnings memmem-simple mempcpy memrchr memset_explicit
+  malloc-gnu manywarnings memmem-simple mempcpy memrchr memset_explicit
   minmax mkostemp mktime
   nanosleep nproc nstrftime
   pathmax pipe2 pselect pthread_sigmask
-  qcopy-acl readlink readlinkat regex
+  qcopy-acl readlink readlinkat realloc-posix regex
   sig2str sigdescr_np socklen stat-time std-gnu11
   stdc_bit_width stdc_count_ones stdc_trailing_zeros
   stdckdint-h stddef-h stdio-h
diff --git a/lib/gnulib.mk.in b/lib/gnulib.mk.in
index ce874048f4a..c044757f2a6 100644
--- a/lib/gnulib.mk.in
+++ b/lib/gnulib.mk.in
@@ -132,6 +132,7 @@
 #  largefile \
 #  libgmp \
 #  lstat \
+#  malloc-gnu \
 #  manywarnings \
 #  memmem-simple \
 #  mempcpy \
@@ -150,6 +151,7 @@
 #  qcopy-acl \
 #  readlink \
 #  readlinkat \
+#  realloc-posix \
 #  regex \
 #  sig2str \
 #  sigdescr_np \
@@ -1476,14 +1478,12 @@ gl_GNULIB_ENABLED_03e0aaad4cb89ca757653bd367a6ccb7_CONDITION = @gl_GNULIB_ENABLE
 gl_GNULIB_ENABLED_260941c0e5dc67ec9e87d1fb321c300b_CONDITION = @gl_GNULIB_ENABLED_260941c0e5dc67ec9e87d1fb321c300b_CONDITION@
 gl_GNULIB_ENABLED_5264294aa0a5557541b53c8c741f7f31_CONDITION = @gl_GNULIB_ENABLED_5264294aa0a5557541b53c8c741f7f31_CONDITION@
 gl_GNULIB_ENABLED_6099e9737f757db36c47fa9d9f02e88c_CONDITION = @gl_GNULIB_ENABLED_6099e9737f757db36c47fa9d9f02e88c_CONDITION@
-gl_GNULIB_ENABLED_61bcaca76b3e6f9ae55d57a1c3193bc4_CONDITION = @gl_GNULIB_ENABLED_61bcaca76b3e6f9ae55d57a1c3193bc4_CONDITION@
 gl_GNULIB_ENABLED_8444034ea779b88768865bb60b4fb8c9_CONDITION = @gl_GNULIB_ENABLED_8444034ea779b88768865bb60b4fb8c9_CONDITION@
 gl_GNULIB_ENABLED_925677f0343de64b89a9f0c790b4104c_CONDITION = @gl_GNULIB_ENABLED_925677f0343de64b89a9f0c790b4104c_CONDITION@
 gl_GNULIB_ENABLED_a9786850e999ae65a836a6041e8e5ed1_CONDITION = @gl_GNULIB_ENABLED_a9786850e999ae65a836a6041e8e5ed1_CONDITION@
 gl_GNULIB_ENABLED_be453cec5eecf5731a274f2de7f2db36_CONDITION = @gl_GNULIB_ENABLED_be453cec5eecf5731a274f2de7f2db36_CONDITION@
 gl_GNULIB_ENABLED_cloexec_CONDITION = @gl_GNULIB_ENABLED_cloexec_CONDITION@
 gl_GNULIB_ENABLED_dirfd_CONDITION = @gl_GNULIB_ENABLED_dirfd_CONDITION@
-gl_GNULIB_ENABLED_e80bf6f757095d2e5fc94dafb8f8fc8b_CONDITION = @gl_GNULIB_ENABLED_e80bf6f757095d2e5fc94dafb8f8fc8b_CONDITION@
 gl_GNULIB_ENABLED_endian_CONDITION = @gl_GNULIB_ENABLED_endian_CONDITION@
 gl_GNULIB_ENABLED_euidaccess_CONDITION = @gl_GNULIB_ENABLED_euidaccess_CONDITION@
 gl_GNULIB_ENABLED_fd38c7e463b54744b77b98aeafb4fa7c_CONDITION = @gl_GNULIB_ENABLED_fd38c7e463b54744b77b98aeafb4fa7c_CONDITION@
@@ -2686,9 +2686,7 @@ endif
 ## begin gnulib module malloc-gnu
 ifeq (,$(OMIT_GNULIB_MODULE_malloc-gnu))
 
-ifneq (,$(gl_GNULIB_ENABLED_e80bf6f757095d2e5fc94dafb8f8fc8b_CONDITION))
 
-endif
 EXTRA_DIST += malloc.c
 
 EXTRA_libgnu_a_SOURCES += malloc.c
@@ -2931,12 +2929,10 @@ endif
 ## begin gnulib module realloc-posix
 ifeq (,$(OMIT_GNULIB_MODULE_realloc-posix))
 
-ifneq (,$(gl_GNULIB_ENABLED_61bcaca76b3e6f9ae55d57a1c3193bc4_CONDITION))
 ifneq (,$(GL_COND_OBJ_REALLOC_POSIX_CONDITION))
 libgnu_a_SOURCES += realloc.c
 endif
 
-endif
 endif
 ## end   gnulib module realloc-posix
 
diff --git a/m4/gnulib-comp.m4 b/m4/gnulib-comp.m4
index dd083cae05d..980baf83998 100644
--- a/m4/gnulib-comp.m4
+++ b/m4/gnulib-comp.m4
@@ -424,6 +424,11 @@ AC_DEFUN
   ])
   gl_SYS_STAT_MODULE_INDICATOR([lstat])
   gl_MODULE_INDICATOR([lstat])
+  gl_FUNC_MALLOC_GNU
+  if test $REPLACE_MALLOC_FOR_MALLOC_GNU = 1; then
+    AC_LIBOBJ([malloc])
+  fi
+  gl_STDLIB_MODULE_INDICATOR([malloc-gnu])
   AC_REQUIRE([gl_FUNC_MALLOC_POSIX])
   if test $REPLACE_MALLOC_FOR_MALLOC_POSIX = 1; then
     AC_LIBOBJ([malloc])
@@ -502,6 +507,11 @@ AC_DEFUN
   gl_CONDITIONAL([GL_COND_OBJ_READLINKAT],
                  [test $HAVE_READLINKAT = 0 || test $REPLACE_READLINKAT = 1])
   gl_UNISTD_MODULE_INDICATOR([readlinkat])
+  gl_FUNC_REALLOC_POSIX
+  gl_FUNC_REALLOC_0_NONNULL
+  gl_CONDITIONAL([GL_COND_OBJ_REALLOC_POSIX],
+                 [test $REPLACE_REALLOC_FOR_REALLOC_POSIX != 0])
+  gl_STDLIB_MODULE_INDICATOR([realloc-posix])
   gl_REGEX
   gl_CONDITIONAL([GL_COND_OBJ_REGEX], [test $ac_use_included_regex = yes])
   AM_COND_IF([GL_COND_OBJ_REGEX], [
@@ -684,12 +694,10 @@ AC_DEFUN
   gl_gnulib_enabled_8444034ea779b88768865bb60b4fb8c9=false
   gl_gnulib_enabled_a9786850e999ae65a836a6041e8e5ed1=false
   gl_gnulib_enabled_lchmod=false
-  gl_gnulib_enabled_e80bf6f757095d2e5fc94dafb8f8fc8b=false
   gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31=false
   gl_gnulib_enabled_open=false
   gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7=false
   gl_gnulib_enabled_rawmemchr=false
-  gl_gnulib_enabled_61bcaca76b3e6f9ae55d57a1c3193bc4=false
   gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c=false
   gl_gnulib_enabled_strtoll=false
   gl_gnulib_enabled_utimens=false
@@ -811,7 +819,6 @@ AC_DEFUN
     if $gl_gnulib_enabled_8444034ea779b88768865bb60b4fb8c9; then :; else
       AC_PROG_MKDIR_P
       gl_gnulib_enabled_8444034ea779b88768865bb60b4fb8c9=true
-      func_gl_gnulib_m4code_61bcaca76b3e6f9ae55d57a1c3193bc4
     fi
   }
   func_gl_gnulib_m4code_a9786850e999ae65a836a6041e8e5ed1 ()
@@ -827,9 +834,6 @@ AC_DEFUN
       if test $HAVE_GROUP_MEMBER = 0; then
         func_gl_gnulib_m4code_getgroups
       fi
-      if test $HAVE_GROUP_MEMBER = 0; then
-        func_gl_gnulib_m4code_e80bf6f757095d2e5fc94dafb8f8fc8b
-      fi
     fi
   }
   func_gl_gnulib_m4code_lchmod ()
@@ -844,17 +848,6 @@ AC_DEFUN
       gl_gnulib_enabled_lchmod=true
     fi
   }
-  func_gl_gnulib_m4code_e80bf6f757095d2e5fc94dafb8f8fc8b ()
-  {
-    if $gl_gnulib_enabled_e80bf6f757095d2e5fc94dafb8f8fc8b; then :; else
-      gl_FUNC_MALLOC_GNU
-      if test $REPLACE_MALLOC_FOR_MALLOC_GNU = 1; then
-        AC_LIBOBJ([malloc])
-      fi
-      gl_STDLIB_MODULE_INDICATOR([malloc-gnu])
-      gl_gnulib_enabled_e80bf6f757095d2e5fc94dafb8f8fc8b=true
-    fi
-  }
   func_gl_gnulib_m4code_5264294aa0a5557541b53c8c741f7f31 ()
   {
     if $gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31; then :; else
@@ -899,17 +892,6 @@ AC_DEFUN
       gl_gnulib_enabled_rawmemchr=true
     fi
   }
-  func_gl_gnulib_m4code_61bcaca76b3e6f9ae55d57a1c3193bc4 ()
-  {
-    if $gl_gnulib_enabled_61bcaca76b3e6f9ae55d57a1c3193bc4; then :; else
-      gl_FUNC_REALLOC_POSIX
-      gl_FUNC_REALLOC_0_NONNULL
-      gl_CONDITIONAL([GL_COND_OBJ_REALLOC_POSIX],
-                     [test $REPLACE_REALLOC_FOR_REALLOC_POSIX != 0])
-      gl_STDLIB_MODULE_INDICATOR([realloc-posix])
-      gl_gnulib_enabled_61bcaca76b3e6f9ae55d57a1c3193bc4=true
-    fi
-  }
   func_gl_gnulib_m4code_6099e9737f757db36c47fa9d9f02e88c ()
   {
     if $gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c; then :; else
@@ -1011,9 +993,6 @@ AC_DEFUN
   if test $ac_use_included_regex = yes; then
     func_gl_gnulib_m4code_fd38c7e463b54744b77b98aeafb4fa7c
   fi
-  if test $ac_use_included_regex = yes; then
-    func_gl_gnulib_m4code_e80bf6f757095d2e5fc94dafb8f8fc8b
-  fi
   if test $ac_use_included_regex = yes; then
     func_gl_gnulib_m4code_verify
   fi
@@ -1047,12 +1026,10 @@ AC_DEFUN
   AM_CONDITIONAL([gl_GNULIB_ENABLED_8444034ea779b88768865bb60b4fb8c9], [$gl_gnulib_enabled_8444034ea779b88768865bb60b4fb8c9])
   AM_CONDITIONAL([gl_GNULIB_ENABLED_a9786850e999ae65a836a6041e8e5ed1], [$gl_gnulib_enabled_a9786850e999ae65a836a6041e8e5ed1])
   AM_CONDITIONAL([gl_GNULIB_ENABLED_lchmod], [$gl_gnulib_enabled_lchmod])
-  AM_CONDITIONAL([gl_GNULIB_ENABLED_e80bf6f757095d2e5fc94dafb8f8fc8b], [$gl_gnulib_enabled_e80bf6f757095d2e5fc94dafb8f8fc8b])
   AM_CONDITIONAL([gl_GNULIB_ENABLED_5264294aa0a5557541b53c8c741f7f31], [$gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31])
   AM_CONDITIONAL([gl_GNULIB_ENABLED_open], [$gl_gnulib_enabled_open])
   AM_CONDITIONAL([gl_GNULIB_ENABLED_03e0aaad4cb89ca757653bd367a6ccb7], [$gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7])
   AM_CONDITIONAL([gl_GNULIB_ENABLED_rawmemchr], [$gl_gnulib_enabled_rawmemchr])
-  AM_CONDITIONAL([gl_GNULIB_ENABLED_61bcaca76b3e6f9ae55d57a1c3193bc4], [$gl_gnulib_enabled_61bcaca76b3e6f9ae55d57a1c3193bc4])
   AM_CONDITIONAL([gl_GNULIB_ENABLED_6099e9737f757db36c47fa9d9f02e88c], [$gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c])
   AM_CONDITIONAL([gl_GNULIB_ENABLED_strtoll], [$gl_gnulib_enabled_strtoll])
   AM_CONDITIONAL([gl_GNULIB_ENABLED_utimens], [$gl_gnulib_enabled_utimens])
-- 
2.45.2

From 7d94d00ddc2e82e5ccbbc5aeb76f47c8f299d717 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Wed, 15 Jan 2025 23:32:43 -0800
Subject: [PATCH 2/2] Simplify alloc by assuming MALLOC_IS_LISP_ALIGNED

Problem reported by Hong Xu <https://bugs.gnu.org/75551#14>.
* src/alloc.c (MALLOC_IS_LISP_ALIGNED): static_assert it,
since it is true on all current Emacs platforms.
All uses simplified to assume it is true.
(xmalloc, xzalloc, xrealloc, lisp_malloc): Just use
malloc/calloc/realloc.  Since we are using the malloc-gnu
and realloc-posix modules, we need not worry about whether
these functions return a null pointer for zero-size requests.
(xrealloc): Stop worrying about no-longer-existing platforms
where realloc (nullptr, ...) did not work.
(laligned, lmalloc, lrealloc): Remove.  All uses removed.
---
 src/alloc.c | 128 +++++++++++-----------------------------------------
 1 file changed, 26 insertions(+), 102 deletions(-)

diff --git a/src/alloc.c b/src/alloc.c
index 8307c74c106..e7290c55f88 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -704,21 +704,24 @@ #define COMMON_MULTIPLE(a, b) \
   ((a) % (b) == 0 ? (a) : (b) % (a) == 0 ? (b) : (a) * (b))
 
 /* Alignment needed for memory blocks that are allocated via malloc
-   and that contain Lisp objects.  On typical hosts malloc already
-   aligns sufficiently, but extra work is needed on oddball hosts
-   where Emacs would crash if malloc returned a non-GCALIGNED pointer.  */
+   and that contain Lisp objects.  */
 enum { LISP_ALIGNMENT = alignof (union { union emacs_align_type x;
 					 GCALIGNED_UNION_MEMBER }) };
 static_assert (LISP_ALIGNMENT % GCALIGNMENT == 0);
 
-/* True if malloc (N) is known to return storage suitably aligned for
-   Lisp objects whenever N is a multiple of LISP_ALIGNMENT.  In
-   practice this is true whenever alignof (max_align_t) is also a
+/* Verify Emacs's assumption that malloc (N) returns storage suitably
+   aligned for Lisp objects whenever N is a multiple of LISP_ALIGNMENT.
+   This assumption holds for current Emacs porting targets;
+   if the assumption fails on a new platform, this check should
+   cause compilation to fail and some porting work will need to be done.
+
+   In practice the assumption holds when alignof (max_align_t) is also a
    multiple of LISP_ALIGNMENT.  This works even for buggy platforms
    like MinGW circa 2020, where alignof (max_align_t) is 16 even though
    the malloc alignment is only 8, and where Emacs still works because
    it never does anything that requires an alignment of 16.  */
 enum { MALLOC_IS_LISP_ALIGNED = alignof (max_align_t) % LISP_ALIGNMENT == 0 };
+static_assert (MALLOC_IS_LISP_ALIGNED);
 
 /* If compiled with XMALLOC_BLOCK_INPUT_CHECK, define a symbol
    BLOCK_INPUT_IN_MEMORY_ALLOCATORS that is visible to the debugger.
@@ -759,9 +762,6 @@ #define MALLOC_PROBE(size)			\
       malloc_probe (size);			\
   } while (0)
 
-static void *lmalloc (size_t, bool) ATTRIBUTE_MALLOC_SIZE ((1));
-static void *lrealloc (void *, size_t);
-
 /* Like malloc but check for no memory and block interrupt input.  */
 
 void *
@@ -770,7 +770,7 @@ xmalloc (size_t size)
   void *val;
 
   MALLOC_BLOCK_INPUT;
-  val = lmalloc (size, false);
+  val = malloc (size);
   MALLOC_UNBLOCK_INPUT;
 
   if (!val)
@@ -787,7 +787,7 @@ xzalloc (size_t size)
   void *val;
 
   MALLOC_BLOCK_INPUT;
-  val = lmalloc (size, true);
+  val = calloc (1, size);
   MALLOC_UNBLOCK_INPUT;
 
   if (!val)
@@ -804,12 +804,7 @@ xrealloc (void *block, size_t size)
   void *val;
 
   MALLOC_BLOCK_INPUT;
-  /* Call lmalloc when BLOCK is null, for the benefit of long-obsolete
-     platforms lacking support for realloc (NULL, size).  */
-  if (! block)
-    val = lmalloc (size, false);
-  else
-    val = lrealloc (block, size);
+  val = realloc (block, size);
   MALLOC_UNBLOCK_INPUT;
 
   if (!val)
@@ -994,15 +989,23 @@ record_xmalloc (size_t size)
 }

-/* Like malloc but used for allocating Lisp data.  NBYTES is the
-   number of bytes to allocate, TYPE describes the intended use of the
-   allocated memory block (for strings, for conses, ...).  */
-
 #if ! USE_LSB_TAG
 extern void *lisp_malloc_loser;
 void *lisp_malloc_loser EXTERNALLY_VISIBLE;
 #endif
 
+/* Allocate memory for Lisp data.
+   NBYTES is the number of bytes to allocate; it must be Lisp-aligned.
+   If CLEARIT, arrange for the allocated memory to be cleared
+   by using calloc, which can be faster than malloc+memset.
+   TYPE describes the intended use of the allocated memory block
+   (for strings, for conses, ...).
+   Return a null pointer if and only if allocation failed.
+
+   Code allocating heap memory for Lisp should use this function to get
+   a pointer P; that way, if T is an enum Lisp_Type value and
+   L == make_lisp_ptr (P, T), then XPNTR (L) == P and XTYPE (L) == T.  */
+
 static void *
 lisp_malloc (size_t nbytes, bool clearit, enum mem_type type)
 {
@@ -1014,7 +1017,7 @@ lisp_malloc (size_t nbytes, bool clearit, enum mem_type type)
   allocated_mem_type = type;
 #endif
 
-  val = lmalloc (nbytes, clearit);
+  val = clearit ? calloc (1, nbytes) : malloc (nbytes);
 
 #if ! USE_LSB_TAG
   /* If the memory just allocated cannot be addressed thru a Lisp
@@ -1098,11 +1101,7 @@ aligned_alloc (size_t alignment, size_t size)
      Verify this for all arguments this function is given.  */
   static_assert (BLOCK_ALIGN % sizeof (void *) == 0
 		 && POWER_OF_2 (BLOCK_ALIGN / sizeof (void *)));
-  static_assert (MALLOC_IS_LISP_ALIGNED
-		 || (LISP_ALIGNMENT % sizeof (void *) == 0
-		     && POWER_OF_2 (LISP_ALIGNMENT / sizeof (void *))));
-  eassert (alignment == BLOCK_ALIGN
-	   || (!MALLOC_IS_LISP_ALIGNED && alignment == LISP_ALIGNMENT));
+  eassert (alignment == BLOCK_ALIGN);
 
   void *p;
   return posix_memalign (&p, alignment, size) == 0 ? p : 0;
@@ -1350,81 +1349,6 @@ lisp_align_free (void *block)
   MALLOC_UNBLOCK_INPUT;
 }
 
-/* True if a malloc-returned pointer P is suitably aligned for SIZE,
-   where Lisp object alignment may be needed if SIZE is a multiple of
-   LISP_ALIGNMENT.  */
-
-static bool
-laligned (void *p, size_t size)
-{
-  return (MALLOC_IS_LISP_ALIGNED || (intptr_t) p % LISP_ALIGNMENT == 0
-	  || size % LISP_ALIGNMENT != 0);
-}
-
-/* Like malloc and realloc except return null only on failure,
-   the result is Lisp-aligned if SIZE is, and lrealloc's pointer
-   argument must be nonnull.  Code allocating C heap memory
-   for a Lisp object should use one of these functions to obtain a
-   pointer P; that way, if T is an enum Lisp_Type value and L ==
-   make_lisp_ptr (P, T), then XPNTR (L) == P and XTYPE (L) == T.
-
-   If CLEARIT, arrange for the allocated memory to be cleared.
-   This might use calloc, as calloc can be faster than malloc+memset.
-
-   On typical modern platforms these functions' loops do not iterate.
-   On now-rare (and perhaps nonexistent) platforms, the code can loop,
-   reallocating (typically with larger and larger sizes) until the
-   allocator returns a Lisp-aligned pointer.  This loop in
-   theory could repeat forever.  If an infinite loop is possible on a
-   platform, a build would surely loop and the builder can then send
-   us a bug report.  Adding a counter to try to detect any such loop
-   would complicate the code (and possibly introduce bugs, in code
-   that's never really exercised) for little benefit.  */
-
-static void *
-lmalloc (size_t size, bool clearit)
-{
-#ifdef USE_ALIGNED_ALLOC
-  if (! MALLOC_IS_LISP_ALIGNED && size % LISP_ALIGNMENT == 0)
-    {
-      void *p = aligned_alloc (LISP_ALIGNMENT, size);
-      if (p)
-	{
-	  if (clearit)
-	    memclear (p, size);
-	}
-      else if (! (MALLOC_0_IS_NONNULL || size))
-	return aligned_alloc (LISP_ALIGNMENT, LISP_ALIGNMENT);
-      return p;
-    }
-#endif
-
-  while (true)
-    {
-      void *p = clearit ? calloc (1, size) : malloc (size);
-      if (laligned (p, size) && (MALLOC_0_IS_NONNULL || size || p))
-	return p;
-      free (p);
-      size_t bigger;
-      if (!ckd_add (&bigger, size, LISP_ALIGNMENT))
-	size = bigger;
-    }
-}
-
-static void *
-lrealloc (void *p, size_t size)
-{
-  while (true)
-    {
-      p = realloc (p, size);
-      if (laligned (p, size) && (size || p))
-	return p;
-      size_t bigger;
-      if (!ckd_add (&bigger, size, LISP_ALIGNMENT))
-	size = bigger;
-    }
-}
-
 
 /***********************************************************************
 			 Interval Allocation
-- 
2.45.2