leak-detective: Try to properly free allocations after deinitialization

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.
This commit is contained in:
Tobias Brunner 2016-06-27 18:04:39 +02:00
parent c1410cb045
commit 505c318701
1 changed files with 13 additions and 0 deletions

View 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;
}