From a2a5a77a44df6f06569d113c53a97201cd72875f Mon Sep 17 00:00:00 2001 From: Soonho Kong Date: Thu, 26 Sep 2013 00:31:30 -0700 Subject: [PATCH] fix(memory): increase memory counters by the actual size of reallocated memory On OSX, we had a test failure on memory module. The problem was in the realloc function (line 38): void * realloc(void * ptr, size_t sz) { size_t old_sz = malloc_size(ptr); g_global_memory.dec(old_sz); g_global_memory.inc(sz); g_thread_memory.dec(old_sz); g_thread_memory.inc(sz); void * r = realloc_core(ptr, sz); if (r || sz == 0) return r; else ... The size of r could be bigger than sz. For instance, |ptr| = 40 but |r| = 48 In the current code, here we only increase counters by 40. But later when we free it, we decrease them by 48, and this caused the problem, underflow of an unsigned counter in g_global_memory. --- src/util/memory.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/util/memory.cpp b/src/util/memory.cpp index 4dc5713cb..ef101f400 100644 --- a/src/util/memory.cpp +++ b/src/util/memory.cpp @@ -135,10 +135,11 @@ void * malloc(size_t sz) { void * realloc(void * ptr, size_t sz) { size_t old_sz = malloc_size(ptr); g_global_memory.dec(old_sz); - g_global_memory.inc(sz); g_thread_memory.dec(old_sz); - g_thread_memory.inc(sz); void * r = realloc_core(ptr, sz); + size_t new_sz = malloc_size(r); + g_global_memory.inc(new_sz); + g_thread_memory.inc(new_sz); if (r || sz == 0) return r; else