malloc: Minor cleanup

Message ID PAXPR08MB6782BEC608C8560A8CB7874183882@PAXPR08MB6782.eurprd08.prod.outlook.com (mailing list archive)
State New
Headers
Series malloc: Minor cleanup |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed

Commit Message

Wilco Dijkstra Oct. 3, 2026, 9:54 a.m. UTC
  Fix an incorrect use of CHUNK_HDR_SZ where chunksize2usable should be used.
Also use chunksize2usable instead of musable in __libc_realloc.

Passes regress, OK for commit?

---
  

Patch

diff --git a/malloc/malloc.c b/malloc/malloc.c
index a001875f2543175b18d0fa74e8ae1f281ec0cd40..97c91ea4d723702be6dec783b7d41c605893ecbe 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3081,14 +3081,15 @@  __libc_realloc (void *oldmem, size_t bytes)
 #endif
 
   /* chunk corresponding to oldmem */
-  const mchunkptr oldp = mem2chunk (oldmem);
+  mchunkptr oldp = mem2chunk (oldmem);
+  INTERNAL_SIZE_T oldsize = chunksize (oldp);
+  size_t usable = chunksize2usable (oldsize);
 
   /* Return the chunk as is if the request grows within usable bytes, typically
      into the alignment padding.  We want to avoid reusing the block for
      shrinkages because it ends up unnecessarily fragmenting the address space.
      This is also why the heuristic misses alignment padding for THP for
      now.  */
-  size_t usable = musable (oldmem);
   if (bytes <= usable)
     {
       size_t difference = usable - bytes;
@@ -3096,9 +3097,6 @@  __libc_realloc (void *oldmem, size_t bytes)
 	return oldmem;
     }
 
-  /* its size */
-  const INTERNAL_SIZE_T oldsize = chunksize (oldp);
-
   /* Little security check which won't hurt performance: the allocator
      never wraps around at the end of the address space.  Therefore
      we can exclude some size values which might appear here by
@@ -4008,7 +4006,7 @@  _int_free_merge_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T size)
                         || nextsize >= av->system_mem))
     malloc_printerr ("free(): invalid next size (normal)");
 
-  free_perturb (chunk2mem(p), size - CHUNK_HDR_SZ);
+  free_perturb (chunk2mem(p), chunksize2usable (size));
 
   /* Consolidate backward.  */
   if (!prev_inuse(p))