[parted-devel] [PATCH] Enable malloc error checking
Frodo Baggins
frodo.drogo at gmail.com
Sun Aug 26 22:13:23 UTC 2007
Hi all,
Below patch sets MALLOC_CHECK_ when DEBUG is defined to enable malloc
error checking. There are some other minor cleanups too.
I think we need to unset MALLOC_CHECK_ when exiting. I will try doing that next.
Regards,
Frodo B
--
diff --git a/libparted/libparted.c b/libparted/libparted.c
index a8c7f0a..d37ee07 100644
--- a/libparted/libparted.c
+++ b/libparted/libparted.c
@@ -51,26 +51,15 @@ 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];
-#endif /* 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 */
+ }
+}
+#endif
int
ped_set_architecture (const PedArchitecture* arch)
@@ -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,59 +238,18 @@ 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)
{
void* mem;
- mem = (void*) malloc (size);
- if (!mem) {
+ mem = malloc (size);
+ if (mem==NULL) {
ped_exception_throw (PED_EXCEPTION_FATAL, PED_EXCEPTION_CANCEL,
_("Out of memory."));
return NULL;
}
-#ifdef DEBUG
- memset (mem, 0xff, size);
- _check_dodgy_pointer (mem, size, 1);
-#endif
-
return mem;
}
@@ -313,8 +258,8 @@ ped_realloc (void** old, size_t size)
{
void* mem;
- mem = (void*) realloc (*old, size);
- if (!mem) {
+ mem = realloc (*old, size);
+ if (mem==NULL) {
ped_exception_throw (PED_EXCEPTION_FATAL, PED_EXCEPTION_CANCEL,
_("Out of memory."));
return 0;
@@ -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);
}
diff --git a/parted/command.c b/parted/command.c
index a3ae7c0..71078e6 100644
--- a/parted/command.c
+++ b/parted/command.c
@@ -77,7 +77,7 @@ command_get (Command** list, char* name)
int partial_match = -1;
int ambiguous = 0;
- if (!name)
+ if (name==NULL)
return NULL;
for (i=0; list [i]; i++) {
diff --git a/parted/command.h b/parted/command.h
diff --git a/parted/parted.c b/parted/parted.c
index 6a606ae..10bdeba 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -61,7 +61,7 @@
/* minimum amount of free space to leave, or maximum amount to gobble up */
#define MIN_FREESPACE (1000 * 2) /* 1000k */
-static int MEGABYTE_SECTORS (PedDevice* dev)
+static int sectors_per_megabyte (PedDevice* dev)
{
return PED_MEGABYTE_SIZE / dev->sector_size;
}
@@ -1731,8 +1731,8 @@ do_rescue (PedDevice** dev)
if (!command_line_get_sector (_("End?"), *dev, &end, NULL))
goto error_destroy_disk;
- fuzz = PED_MAX (PED_MIN ((end - start) / 10, MEGABYTE_SECTORS(*dev)),
- MEGABYTE_SECTORS(*dev) * 16);
+ fuzz = PED_MAX (PED_MIN ((end - start) / 10,
sectors_per_megabyte(*dev)),
+ sectors_per_megabyte(*dev) * 16);
ped_geometry_init (&probe_start_region, *dev,
PED_MAX(start - fuzz, 0),
diff --git a/parted/strlist.c b/parted/strlist.c
index 6b4568a..216fa66 100644
--- a/parted/strlist.c
+++ b/parted/strlist.c
@@ -49,7 +49,10 @@
#include "strlist.h"
-#define MIN(a,b) ( (a<b)? a : b )
+static int min (int a, int b)
+{
+ return a > b ? a : b;
+}
int
wchar_strlen (const wchar_t* str)
@@ -461,7 +464,7 @@ str_list_print_wrap (const StrList* list
while (line_left < str_len || wchar_strchr (str, '\n')) {
line_break = 0;
- cut_left = MIN (line_left - 1, str_len - 1);
+ cut_left = min(line_left - 1, str_len - 1);
/* we can have a space "over", but not a comma */
if (cut_left < str_len
diff --git a/parted/strlist.h b/parted/strlist.h
diff --git a/parted/table.c b/parted/table.c
diff --git a/parted/table.h b/parted/table.h
diff --git a/parted/ui.c b/parted/ui.c
diff --git a/parted/ui.h b/parted/ui.h
diff --git a/partprobe/partprobe.c b/partprobe/partprobe.c
index d324167..14be42e 100644
--- a/partprobe/partprobe.c
+++ b/partprobe/partprobe.c
@@ -111,17 +111,17 @@ process_dev (PedDevice* dev)
PedDisk* disk;
disk_type = ped_disk_probe (dev);
- if (!disk_type || !strcmp (disk_type->name, "loop"))
+ if (disk_type==NULL || !strcmp (disk_type->name, "loop"))
return 1;
disk = ped_disk_new (dev);
- if (!disk)
+ if (disk==NULL)
goto error;
- if (!opt_no_inform) {
+ if (opt_no_inform==0) {
if (!ped_disk_commit_to_os (disk))
goto error_destroy_disk;
}
- if (opt_summary)
+ if (opt_summary==1)
summary (disk);
ped_disk_destroy (disk);
return 1;
More information about the parted-devel
mailing list