]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
MTE: Do not pad size in realloc_check
authorSiddhesh Poyarekar <siddhesh@sourceware.org>
Wed, 23 Dec 2020 02:11:17 +0000 (07:41 +0530)
committerSiddhesh Poyarekar <siddhesh@sourceware.org>
Thu, 24 Dec 2020 00:32:05 +0000 (06:02 +0530)
The MTE patch to add malloc support incorrectly padded the size passed
to _int_realloc by SIZE_SZ when it ought to have sent just the
chunksize.  Revert that bit of the change so that realloc works
correctly with MALLOC_CHECK_ set.

This also brings the realloc_check implementation back in sync with
libc_realloc.

malloc/hooks.c

index 8a1c16dfa413c3e0801ecf2273280922aacd003b..6474ba8b38ed540ec562d68f6c12e7cb28521ca3 100644 (file)
@@ -315,7 +315,7 @@ realloc_check (void *oldmem, size_t bytes, const void *caller)
   __libc_lock_unlock (main_arena.mutex);
   if (!oldp)
     malloc_printerr ("realloc(): invalid pointer");
-  const INTERNAL_SIZE_T oldchsize = CHUNK_AVAILABLE_SIZE (oldp);
+  const INTERNAL_SIZE_T oldsize = chunksize (oldp);
 
   if (!checked_request2size (rb, &chnb))
     goto invert;
@@ -331,7 +331,8 @@ realloc_check (void *oldmem, size_t bytes, const void *caller)
       else
 #endif
       {
-        if (oldchsize >= chnb)
+       /* Note the extra SIZE_SZ overhead. */
+        if (oldsize - SIZE_SZ >= chnb)
           newmem = oldmem; /* do nothing */
         else
           {
@@ -340,7 +341,7 @@ realloc_check (void *oldmem, size_t bytes, const void *caller)
            newmem = _int_malloc (&main_arena, rb);
             if (newmem)
               {
-                memcpy (newmem, oldmem, oldchsize - CHUNK_HDR_SZ);
+                memcpy (newmem, oldmem, oldsize - CHUNK_HDR_SZ);
                 munmap_chunk (oldp);
               }
           }
@@ -349,7 +350,7 @@ realloc_check (void *oldmem, size_t bytes, const void *caller)
   else
     {
       top_check ();
-      newmem = _int_realloc (&main_arena, oldp, oldchsize, chnb);
+      newmem = _int_realloc (&main_arena, oldp, oldsize, chnb);
     }
 
   DIAG_PUSH_NEEDS_COMMENT;