[PATCH] malloc: Fix mremap_chunk statistics [BZ #34706]

Wilco Dijkstra Wilco.Dijkstra@arm.com
Fri Nov 6 18:44:43 GMT 2026


Adjust counters based on the total size change of mapped memory in mremap_chunk.
This fixes BZ #34706.

Passes regress, OK for commit?

---

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 4bb267a648fb7dae9bd89426d60ab2781e6b7153..815f8f986be371f2f413eb90417a096d925b0037 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -2580,7 +2580,6 @@ mremap_chunk (mchunkptr p, size_t new_size)
   bool is_hp = mmap_is_hp (p);
   size_t pagesize = is_hp ? mp_.hp_pagesize : GLRO (dl_pagesize);
   INTERNAL_SIZE_T offset = mmap_base_offset (p);
-  INTERNAL_SIZE_T size = chunksize (p);
   char *cp;
 
   assert (chunk_is_mmapped (p));
@@ -2597,8 +2596,7 @@ mremap_chunk (mchunkptr p, size_t new_size)
   if (total_size == new_size)
     return p;
 
-  cp = (char *) __mremap ((char *) block, total_size, new_size,
-                          MREMAP_MAYMOVE);
+  cp = (char *) __mremap ((char *) block, total_size, new_size, MREMAP_MAYMOVE);
 
   if (cp == MAP_FAILED)
     return NULL;
@@ -2611,8 +2609,8 @@ mremap_chunk (mchunkptr p, size_t new_size)
   p = mmap_set_chunk ((uintptr_t) cp, new_size, offset, is_hp);
 
   INTERNAL_SIZE_T new;
-  new = atomic_fetch_add_relaxed (&mp_.mmapped_mem, new_size - size - offset)
-        + new_size - size - offset;
+  new = atomic_fetch_add_relaxed (&mp_.mmapped_mem, new_size - total_size)
+	+ new_size - total_size;
   atomic_max (&mp_.max_mmapped_mem, new);
   return p;
 }



More information about the Libc-alpha mailing list