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