[PATCH] add malloc debugging using MALLOC_CHECK_

Frodo Baggins frodo.drogo at gmail.com
Fri Aug 31 02:15:50 UTC 2007


Set environment variable MALLOC_CHECK_ to 2 so that abort() is
called when libc detects a heap problem. This way we get a memory dump
to analyse in such conditions.

Signed-off-by: Frodo Baggins <frodo.drogo at gmail.com>
---
 libparted/libparted.c |   73 +++++--------------------------------------------
 1 files changed, 7 insertions(+), 66 deletions(-)

diff --git a/libparted/libparted.c b/libparted/libparted.c
index a8c7f0a..2c2c846 100644
--- a/libparted/libparted.c
+++ b/libparted/libparted.c
@@ -51,25 +51,14 @@ typedef struct
     size_t     size;
 } pointer_size_type;

-/* IMHO, none of the DEBUG-related code below is useful, and the
-   ped_malloc memset code is actually quite harmful: it masked at
-   least two nasty bugs that were fixed in June of 2007.  */
-#undef DEBUG
 #ifdef DEBUG
-static pointer_size_type dodgy_malloc_list[] = {
- {0,           0},
- {0,           0},
- {0,           0},
- {0,           0},
- {0,           0},
- {0,           0},
- {0,           0},
- {0,           0},
- {0,           0},
- {0,           0}
-};
-
-static int     dodgy_memory_active[100];
+/* Set MALLOC_CHECK_ to 2 so that heap corruption causes a call to abort() */
+if(NULL==getenv("MALLOC_CHECK_")){
+    if(-1==setenv("MALLOC_CHECK_","2",1)){
+       /* signal error setting this env variable */
+       fprintf(stderr,"Could not set MALLOC_CHECK_ value to 2\n");
+    }
+}
 #endif /* DEBUG */

 int
@@ -199,9 +188,6 @@ _init()
        ped_set_architecture (&ped_gnu_arch);
 #endif

-#ifdef DEBUG
-       memset (dodgy_memory_active, 0, sizeof (dodgy_memory_active));
-#endif
 }

 #ifdef ENABLE_FS
@@ -252,42 +238,6 @@ ped_get_version ()
        return VERSION;
 }

-#ifdef DEBUG
-static void
-_check_dodgy_pointer (const void* ptr, size_t size, int is_malloc)
-{
-       int             i;
-
-       for (i=0; dodgy_malloc_list[i].pointer; i++) {
-               if (dodgy_malloc_list[i].pointer != ptr)
-                       continue;
-               if (is_malloc && dodgy_malloc_list[i].size != size)
-                       continue;
-               if (!is_malloc && !dodgy_memory_active[i])
-                       continue;
-
-
-               if (is_malloc) {
-                       ped_exception_throw (
-                               PED_EXCEPTION_INFORMATION,
-                               PED_EXCEPTION_OK,
-                               "Dodgy malloc(%x) == %p occurred (active==%d)",
-                               size, ptr, dodgy_memory_active[i]);
-                       dodgy_memory_active[i]++;
-               } else {
-                       ped_exception_throw (
-                               PED_EXCEPTION_INFORMATION,
-                               PED_EXCEPTION_OK,
-                               "Dodgy free(%p) occurred (active==%d)",
-                               ptr, dodgy_memory_active[i]);
-                       dodgy_memory_active[i]--;
-               }
-
-               return;
-       }
-}
-#endif /* DEBUG */
-
 void*
 ped_malloc (size_t size)
 {
@@ -300,11 +250,6 @@ ped_malloc (size_t size)
                return NULL;
        }

-#ifdef DEBUG
-       memset (mem, 0xff, size);
-       _check_dodgy_pointer (mem, size, 1);
-#endif
-
        return mem;
 }

@@ -337,9 +282,5 @@ void* ped_calloc (size_t size)
 void
 ped_free (void* ptr)
 {
-#ifdef DEBUG
-       _check_dodgy_pointer (ptr, 0, 0);
-#endif
-
        free (ptr);
 }
--
1.4.3.4



More information about the parted-devel mailing list