[PATCH] Revert "add malloc debugging using MALLOC_CHECK_"

Frodo Baggins frodo.drogo at gmail.com
Tue Sep 4 12:27:01 UTC 2007


This reverts commit 9beec03970ed9b4877b67e93a48eaf1e09b1ac9e.
Will submit a separate patch removing the ifdef'd malloc debugging code.
---
 libparted/libparted.c |   73 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 66 insertions(+), 7 deletions(-)

diff --git a/libparted/libparted.c b/libparted/libparted.c
index 2c2c846..a8c7f0a 100644
--- a/libparted/libparted.c
+++ b/libparted/libparted.c
@@ -51,14 +51,25 @@ 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
-/* 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");
-    }
-}
+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];
 #endif /* DEBUG */

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

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

 #ifdef ENABLE_FS
@@ -238,6 +252,42 @@ 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)
 {
@@ -250,6 +300,11 @@ ped_malloc (size_t size)
                return NULL;
        }

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

@@ -282,5 +337,9 @@ 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