alloc-util: don't use malloc_usable_size() to determine allocated size
authorAaron Barany <aaron.barany@here.com>
Mon, 29 Apr 2019 22:00:30 +0000 (15:00 -0700)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 30 Apr 2019 06:20:59 +0000 (08:20 +0200)
This reverts commit d4b604baeadbb2498e4f2c3e260260eed210f5d6.

When realloc() is called, the extra memory between the originally
requested size and the end of malloc_usable_size() isn't copied. (at
least with the version of glibc that currently ships on Arch Linux)
As a result, some elements get lost and use uninitialized memory, most
commonly 0, and can lead to crashes.

fixes #12384

src/basic/alloc-util.c

index 1e4ee72..f4bd33f 100644 (file)
@@ -1,6 +1,5 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
-#include <malloc.h>
 #include <stdint.h>
 #include <string.h>
 
@@ -65,7 +64,7 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) {
                 return NULL;
 
         *p = q;
-        *allocated = _unlikely_(size == 0) ? newalloc : malloc_usable_size(q) / size;
+        *allocated = newalloc;
         return q;
 }