From e0c59faa68127b9016c45b3ff6ca6e93e19d6970 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Wed, 15 Jun 2016 11:22:04 +0200 Subject: [PATCH] leak-detective: Make sure to actually call malloc() from calloc() hook Newer versions of GCC are too "smart" and replace a call to malloc(X) followed by a call to memset(0,X) with a call co calloc(), which obviously results in an infinite loop when it does that in our own calloc() implementation. Using `volatile` for the variable storing the total size prevents the optimization and we actually call malloc(). --- src/libstrongswan/utils/leak_detective.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libstrongswan/utils/leak_detective.c b/src/libstrongswan/utils/leak_detective.c index 1543c7f99..a0bdae715 100644 --- a/src/libstrongswan/utils/leak_detective.c +++ b/src/libstrongswan/utils/leak_detective.c @@ -810,10 +810,11 @@ HOOK(void*, malloc, size_t bytes) HOOK(void*, calloc, size_t nmemb, size_t size) { void *ptr; + volatile size_t total; - size *= nmemb; - ptr = malloc(size); - memset(ptr, 0, size); + total = nmemb * size; + ptr = malloc(total); + memset(ptr, 0, total); return ptr; }