077c9d
commit b50dd3bc8cbb1efe85399b03d7e6c0310c2ead84
077c9d
Author: Florian Weimer <fw@deneb.enyo.de>
077c9d
Date:   Mon Dec 31 22:04:36 2018 +0100
077c9d
077c9d
    malloc: Always call memcpy in _int_realloc [BZ #24027]
077c9d
    
077c9d
    This commit removes the custom memcpy implementation from _int_realloc
077c9d
    for small chunk sizes.  The ncopies variable has the wrong type, and
077c9d
    an integer wraparound could cause the existing code to copy too few
077c9d
    elements (leaving the new memory region mostly uninitialized).
077c9d
    Therefore, removing this code fixes bug 24027.
077c9d
077c9d
diff --git a/malloc/malloc.c b/malloc/malloc.c
077c9d
index 7bfa66a56786d110..0234d968c0ce65a0 100644
077c9d
--- a/malloc/malloc.c
077c9d
+++ b/malloc/malloc.c
077c9d
@@ -4532,11 +4532,6 @@ _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
077c9d
   mchunkptr        remainder;       /* extra space at end of newp */
077c9d
   unsigned long    remainder_size;  /* its size */
077c9d
 
077c9d
-  unsigned long    copysize;        /* bytes to copy */
077c9d
-  unsigned int     ncopies;         /* INTERNAL_SIZE_T words to copy */
077c9d
-  INTERNAL_SIZE_T* s;               /* copy source */
077c9d
-  INTERNAL_SIZE_T* d;               /* copy destination */
077c9d
-
077c9d
   /* oldmem size */
077c9d
   if (__builtin_expect (chunksize_nomask (oldp) <= 2 * SIZE_SZ, 0)
077c9d
       || __builtin_expect (oldsize >= av->system_mem, 0))
077c9d
@@ -4604,43 +4599,7 @@ _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
077c9d
             }
077c9d
           else
077c9d
             {
077c9d
-              /*
077c9d
-                 Unroll copy of <= 36 bytes (72 if 8byte sizes)
077c9d
-                 We know that contents have an odd number of
077c9d
-                 INTERNAL_SIZE_T-sized words; minimally 3.
077c9d
-               */
077c9d
-
077c9d
-              copysize = oldsize - SIZE_SZ;
077c9d
-              s = (INTERNAL_SIZE_T *) (chunk2mem (oldp));
077c9d
-              d = (INTERNAL_SIZE_T *) (newmem);
077c9d
-              ncopies = copysize / sizeof (INTERNAL_SIZE_T);
077c9d
-              assert (ncopies >= 3);
077c9d
-
077c9d
-              if (ncopies > 9)
077c9d
-                memcpy (d, s, copysize);
077c9d
-
077c9d
-              else
077c9d
-                {
077c9d
-                  *(d + 0) = *(s + 0);
077c9d
-                  *(d + 1) = *(s + 1);
077c9d
-                  *(d + 2) = *(s + 2);
077c9d
-                  if (ncopies > 4)
077c9d
-                    {
077c9d
-                      *(d + 3) = *(s + 3);
077c9d
-                      *(d + 4) = *(s + 4);
077c9d
-                      if (ncopies > 6)
077c9d
-                        {
077c9d
-                          *(d + 5) = *(s + 5);
077c9d
-                          *(d + 6) = *(s + 6);
077c9d
-                          if (ncopies > 8)
077c9d
-                            {
077c9d
-                              *(d + 7) = *(s + 7);
077c9d
-                              *(d + 8) = *(s + 8);
077c9d
-                            }
077c9d
-                        }
077c9d
-                    }
077c9d
-                }
077c9d
-
077c9d
+	      memcpy (newmem, chunk2mem (oldp), oldsize - SIZE_SZ);
077c9d
               _int_free (av, oldp, 1);
077c9d
               check_inuse_chunk (av, newp);
077c9d
               return chunk2mem (newp);