efl/eina: detect when freeing pointer inside an allocated memory.
authorCedric BAIL <cedric.bail@free.fr>
Thu, 24 Jan 2013 02:44:14 +0000 (02:44 +0000)
committerCedric BAIL <cedric.bail@free.fr>
Thu, 24 Jan 2013 02:44:14 +0000 (02:44 +0000)
SVN revision: 83195

src/modules/eina/mp/chained_pool/eina_chained_mempool.c

index 869633a..2da74ad 100644 (file)
@@ -211,14 +211,22 @@ _eina_chained_mempool_free_in(Chained_Mempool *pool, Chained_Pool *p, void *ptr)
    // pool mem base
    pmem = (void *)(((unsigned char *)p) + sizeof(Chained_Pool));
 
+#ifdef DEBUG
    // is it in pool mem?
    if (ptr < pmem)
      {
-#ifdef DEBUG
-        INF("%p is inside the private part of %p pool from %p Chained_Mempool (could be the sign of a buffer underrun).", ptr, p, pool);
-#endif
+        INF("%p is inside the private part of %p pool from %p '%s' Chained_Mempool (could be the sign of a buffer underrun).", ptr, p, pool, pool->name);
+        return EINA_FALSE;
+     }
+
+   // is it really a pointer returned by malloc
+   if ((((unsigned char *)ptr) - (unsigned char *)(p + 1)) % pool->item_alloc)
+     {
+        INF("%p is %i bytes inside a pointer served by %p '%s' Chained_Mempool (You are freeing the wrong pointer man !). %i",
+            ptr, ((((unsigned char *)ptr) - (unsigned char *)(p + 1)) % pool->item_alloc), pool, pool->name);
         return EINA_FALSE;
      }
+#endif
 
    // freed node points to prev free node
    eina_trash_push(&p->base, ptr);