LD_PRELOAD: Compute usable size before realloc
Using a suggestion from
If `ptr` was free'd in the call to realloc, we would call malloc_usable
size on an invalid memory location.
@@ -70,16 +70,19 @@ void *realloc(void *ptr, size_t size)
*(void **) &orig_realloc = dlsym(RTLD_NEXT, "realloc");
}
- p = orig_realloc(ptr, size);
#ifdef HAVE_MALLOC_USABLE_SIZE
- /* If malloc_usable_size is availible, we can get the size of previously
- * allocated buffer, to find out how many new bytes we've allocated */
- size_t ptr_alloc_size;
- size_t p_alloc_size;
+ /* If malloc_usable_size is available, we can get the size of previously
+ * allocated buffer, to find out how many new bytes we've allocated.
+ * Get the usable size for ptr before we call realloc, because realloc may call
+ * free() on the original pointer. */
+ size_t ptr_alloc_size = malloc_usable_size(ptr);
+#endif
- ptr_alloc_size = malloc_usable_size(ptr);
- p_alloc_size = malloc_usable_size(p);
+ p = orig_realloc(ptr, size);
+
+#ifdef HAVE_MALLOC_USABLE_SIZE
+ size_t p_alloc_size = malloc_usable_size(p);
/* If call succeeded and we're enlarging memory, fill the extension with
* non-zero data */
