public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] MTE: Do not pad size in realloc_check
@ 2020-12-22 15:59 Siddhesh Poyarekar
  2020-12-22 17:02 ` H.J. Lu
  0 siblings, 1 reply; 4+ messages in thread
From: Siddhesh Poyarekar @ 2020-12-22 15:59 UTC (permalink / raw)
  To: libc-alpha; +Cc: schwab, rearnsha

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 | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/malloc/hooks.c b/malloc/hooks.c
index 8a1c16dfa4..6474ba8b38 100644
--- a/malloc/hooks.c
+++ b/malloc/hooks.c
@@ -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;
-- 
2.29.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-12-23 13:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-22 15:59 [PATCH] MTE: Do not pad size in realloc_check Siddhesh Poyarekar
2020-12-22 17:02 ` H.J. Lu
2020-12-23  7:46   ` Siddhesh Poyarekar
2020-12-23 13:51     ` H.J. Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).