leak-detective: Try to properly free allocations after deinitialization
authorTobias Brunner <tobias@strongswan.org>
Mon, 27 Jun 2016 16:04:39 +0000 (18:04 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 29 Jun 2016 09:09:38 +0000 (11:09 +0200)
If a function we whitelist allocates memory while leak detective is enabled
but only frees it after LD has already been disabled, free() will get called
with invalid pointers (not pointing to the actually allocated memory by LD),
which will cause checks in the C library to fail and the program to crash.
This tries to detect such cases and calling free with the correct pointer.

src/libstrongswan/utils/leak_detective.c

index aeadc0c..d0f646c 100644 (file)
@@ -844,6 +844,18 @@ HOOK(void, free, void *ptr)
 
        if (!enabled || thread_disabled->get(thread_disabled))
        {
+               /* after deinitialization we might have to free stuff we allocated
+                * while we were enabled */
+               if (!first_header.magic && ptr)
+               {
+                       hdr = ptr - sizeof(memory_header_t);
+                       tail = ptr + hdr->bytes;
+                       if (hdr->magic == MEMORY_HEADER_MAGIC &&
+                               tail->magic == MEMORY_TAIL_MAGIC)
+                       {
+                               ptr = hdr;
+                       }
+               }
                real_free(ptr);
                return;
        }
@@ -960,6 +972,7 @@ METHOD(leak_detective_t, destroy, void,
        lock->destroy(lock);
        thread_disabled->destroy(thread_disabled);
        free(this);
+       first_header.magic = 0;
        first_header.next = NULL;
 }