summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2013-05-19 19:25:18 +0200
committerChristophe Grenier <grenier@cgsecurity.org>2013-05-19 19:25:18 +0200
commitbf83ac26623fc2743ec041b8f502636074ffe69c (patch)
tree85f199a84587f11355345dc9024db56e2e01c252
parent4699591268012128de457f172963a51ae1c681d6 (diff)
Fix memory leak when freeing device
-rw-r--r--src/common.h30
-rw-r--r--src/ewf.c6
-rw-r--r--src/fnctdsk.c6
-rw-r--r--src/hdaccess.c45
-rw-r--r--src/hdaccess.h2
-rw-r--r--src/hdcache.c9
-rw-r--r--src/msdos.c6
-rw-r--r--src/win32.c16
8 files changed, 58 insertions, 62 deletions
diff --git a/src/common.h b/src/common.h
index 1de1042..93bb565 100644
--- a/src/common.h
+++ b/src/common.h
@@ -330,20 +330,20 @@ struct arch_fnct_struct
const char *part_name;
const char *part_name_option;
const char *msg_part_type;
- list_part_t *(*read_part)(disk_t *disk_car, const int verbose,const int saveheader);
- int (*write_part)(disk_t *disk_car, const list_part_t *list_part, const int ro, const int verbose, const int align);
- list_part_t *(*init_part_order)(const disk_t *disk_car, list_part_t *list_part);
+ list_part_t *(*read_part)(disk_t *disk, const int verbose,const int saveheader);
+ int (*write_part)(disk_t *disk, const list_part_t *list_part, const int ro, const int verbose, const int align);
+ list_part_t *(*init_part_order)(const disk_t *disk, list_part_t *list_part);
/* geometry must be initialized to 0,0,0 in get_geometry_from_mbr()*/
int (*get_geometry_from_mbr)(const unsigned char *buffer, const int verbose, CHSgeometry_t *geometry);
- int (*check_part)(disk_t *disk_car,const int verbose,partition_t *partition, const int saveheader);
- int (*write_MBR_code)(disk_t *disk_car);
- void (*set_prev_status)(const disk_t *disk_car, partition_t *partition);
- void (*set_next_status)(const disk_t *disk_car, partition_t *partition);
+ int (*check_part)(disk_t *disk,const int verbose,partition_t *partition, const int saveheader);
+ int (*write_MBR_code)(disk_t *disk);
+ void (*set_prev_status)(const disk_t *disk, partition_t *partition);
+ void (*set_next_status)(const disk_t *disk, partition_t *partition);
int (*test_structure)(list_part_t *list_part);
unsigned int (*get_part_type)(const partition_t *partition);
int (*set_part_type)(partition_t *partition, unsigned int part_type);
- void (*init_structure)(const disk_t *disk_car,list_part_t *list_part, const int verbose);
- int (*erase_list_part)(disk_t *disk_car);
+ void (*init_structure)(const disk_t *disk,list_part_t *list_part, const int verbose);
+ int (*erase_list_part)(disk_t *disk);
const char *(*get_partition_typename)(const partition_t *partition);
int (*is_part_known)(const partition_t *partition);
};
@@ -360,13 +360,13 @@ struct param_disk_struct
char *model;
char *serial_no;
char *fw_rev;
- const char *(*description)(disk_t *disk_car);
- const char *(*description_short)(disk_t *disk_car);
+ const char *(*description)(disk_t *disk);
+ const char *(*description_short)(disk_t *disk);
void *(*pread_fast)(disk_t *disk, void *buf, const unsigned int count, const uint64_t offset);
- int (*pread)(disk_t *disk_car, void *buf, const unsigned int count, const uint64_t offset);
- int (*pwrite)(disk_t *disk_car, const void *buf, const unsigned int count, const uint64_t offset);
- int (*sync)(disk_t *disk_car);
- int (*clean)(disk_t *disk_car);
+ int (*pread)(disk_t *disk, void *buf, const unsigned int count, const uint64_t offset);
+ int (*pwrite)(disk_t *disk, const void *buf, const unsigned int count, const uint64_t offset);
+ int (*sync)(disk_t *disk);
+ void (*clean)(disk_t *disk);
const arch_fnct_t *arch;
const arch_fnct_t *arch_autodetected;
void *data;
diff --git a/src/ewf.c b/src/ewf.c
index 591a253..e301d79 100644
--- a/src/ewf.c
+++ b/src/ewf.c
@@ -76,7 +76,7 @@ extern const arch_fnct_t arch_none;
static const char *fewf_description(disk_t *disk);
static const char *fewf_description_short(disk_t *disk);
-static int fewf_clean(disk_t *disk);
+static void fewf_clean(disk_t *disk);
static void *fewf_pread_fast(disk_t *disk, void *buffer, const unsigned int count, const uint64_t offset);
static int fewf_pread(disk_t *disk, void *buffer, const unsigned int count, const uint64_t offset);
static int fewf_nopwrite(disk_t *disk, const void *buffer, const unsigned int count, const uint64_t offset);
@@ -366,7 +366,7 @@ static const char *fewf_description_short(disk_t *disk)
return disk->description_short_txt;
}
-static int fewf_clean(disk_t *disk)
+static void fewf_clean(disk_t *disk)
{
if(disk->data!=NULL)
{
@@ -390,7 +390,7 @@ static int fewf_clean(disk_t *disk)
free(disk->data);
disk->data=NULL;
}
- return 0;
+ generic_clean(disk);
}
static int fewf_sync(disk_t *disk)
diff --git a/src/fnctdsk.c b/src/fnctdsk.c
index fc43c21..8aa658c 100644
--- a/src/fnctdsk.c
+++ b/src/fnctdsk.c
@@ -195,11 +195,7 @@ int delete_list_disk(list_disk_t *list_disk)
{
list_disk_t *element_disk_next=element_disk->next;
write_used|=element_disk->disk->write_used;
- if(element_disk->disk->clean!=NULL)
- element_disk->disk->clean(element_disk->disk);
- free(element_disk->disk->device);
- free(element_disk->disk->model);
- free(element_disk->disk);
+ element_disk->disk->clean(element_disk->disk);
free(element_disk);
element_disk=element_disk_next;
}
diff --git a/src/hdaccess.c b/src/hdaccess.c
index 4974196..74afa9e 100644
--- a/src/hdaccess.c
+++ b/src/hdaccess.c
@@ -149,7 +149,7 @@ struct info_file_struct
};
static void autoset_geometry(disk_t * disk_car, const unsigned char *buffer, const int verbose);
-static int file_clean(disk_t *disk_car);
+static void file_clean(disk_t *disk);
static int file_pread(disk_t *disk_car, void *buf, const unsigned int count, const uint64_t offset);
static void *file_pread_fast(disk_t *disk, void *buf, const unsigned int count, const uint64_t offset);
static int file_pwrite(disk_t *disk_car, const void *buf, const unsigned int count, const uint64_t offset);
@@ -159,15 +159,23 @@ static int file_sync(disk_t *disk_car);
static uint64_t compute_device_size(const int hd_h, const char *device, const int verbose, const unsigned int sector_size);
#endif
-int generic_clean(disk_t *disk_car)
+void generic_clean(disk_t *disk)
{
- free(disk_car->data);
- disk_car->data=NULL;
- free(disk_car->rbuffer);
- free(disk_car->wbuffer);
- disk_car->rbuffer=NULL;
- disk_car->wbuffer=NULL;
- return 0;
+ free(disk->device);
+ free(disk->model);
+ free(disk->serial_no);
+ free(disk->fw_rev);
+ free(disk->data);
+ free(disk->rbuffer);
+ free(disk->wbuffer);
+ disk->device=NULL;
+ disk->model=NULL;
+ disk->serial_no=NULL;
+ disk->fw_rev=NULL;
+ disk->data=NULL;
+ disk->rbuffer=NULL;
+ disk->wbuffer=NULL;
+ free(disk);
}
#if defined(__CYGWIN__) || defined(__MINGW32__)
@@ -190,11 +198,7 @@ list_disk_t *insert_new_disk_nodup(list_disk_t *list_disk, disk_t *disk_car, con
{
if(verbose>1)
log_verbose("%s is available but reject it to avoid duplicate disk.\n", device_name);
- if(disk_car->clean!=NULL)
- disk_car->clean(disk_car);
- free(disk_car->device);
- free(disk_car->model);
- free(disk_car);
+ disk_car->clean(disk_car);
return list_disk;
}
return insert_new_disk(list_disk,disk_car);
@@ -1092,17 +1096,17 @@ static const char *file_description_short(disk_t *disk_car)
return disk_car->description_short_txt;
}
-static int file_clean(disk_t *disk_car)
+static void file_clean(disk_t *disk)
{
- if(disk_car->data!=NULL)
+ if(disk->data!=NULL)
{
- struct info_file_struct *data=(struct info_file_struct *)disk_car->data;
+ struct info_file_struct *data=(struct info_file_struct *)disk->data;
/*
#ifdef BLKRRPART
if (ioctl(data->handle, BLKRRPART, NULL)) {
- log_error("%s BLKRRPART failed\n",disk_car->description(disk_car));
+ log_error("%s BLKRRPART failed\n",disk->description(disk));
} else {
- log_debug("%s BLKRRPART ok\n",disk_car->description(disk_car));
+ log_debug("%s BLKRRPART ok\n",disk->description(disk));
}
#endif
*/
@@ -1114,8 +1118,9 @@ static int file_clean(disk_t *disk_car)
}
#endif
close(data->handle);
+ data->handle=NULL;
}
- return generic_clean(disk_car);
+ generic_clean(disk);
}
static int file_pread_aux(disk_t *disk, void *buf, const unsigned int count, const uint64_t offset)
diff --git a/src/hdaccess.h b/src/hdaccess.h
index 7c2ea55..01ffbcc 100644
--- a/src/hdaccess.h
+++ b/src/hdaccess.h
@@ -30,7 +30,7 @@ list_disk_t *hd_parse(list_disk_t *list_disk, const int verbose, const int testd
disk_t *file_test_availability(const char *device, const int verbose, const int testdisk_mode);
void update_disk_car_fields(disk_t *disk_car);
void init_disk(disk_t *disk);
-int generic_clean(disk_t *disk_car);
+void generic_clean(disk_t *disk);
#ifdef __cplusplus
} /* closing brace for extern "C" */
diff --git a/src/hdcache.c b/src/hdcache.c
index f2eadb8..c7b5742 100644
--- a/src/hdcache.c
+++ b/src/hdcache.c
@@ -66,8 +66,8 @@ static int cache_pread_aux(disk_t *disk_car, void *buffer, const unsigned int co
static int cache_pread(disk_t *disk_car, void *buffer, const unsigned int count, const uint64_t offset);
static void *cache_pread_fast(disk_t *disk, void *buffer, const unsigned int count, const uint64_t offset);
static int cache_pwrite(disk_t *disk_car, const void *buffer, const unsigned int count, const uint64_t offset);
-static int cache_sync(disk_t *clean);
-static int cache_clean(disk_t *clean);
+static int cache_sync(disk_t *disk);
+static void cache_clean(disk_t *disk);
static const char *cache_description(disk_t *disk_car);
static const char *cache_description_short(disk_t *disk_car);
@@ -241,7 +241,7 @@ static int cache_pwrite(disk_t *disk_car, const void *buffer, const unsigned int
return data->disk_car->pwrite(data->disk_car, buffer, count, offset);
}
-static int cache_clean(disk_t *disk_car)
+static void cache_clean(disk_t *disk_car)
{
if(disk_car->data)
{
@@ -259,11 +259,10 @@ static int cache_clean(disk_t *disk_car)
struct cache_buffer_struct *cache=&data->cache[i];
free(cache->buffer);
}
- free(data->disk_car);
free(disk_car->data);
disk_car->data=NULL;
}
- return 0;
+ free(disk_car);
}
static int cache_sync(disk_t *disk_car)
diff --git a/src/msdos.c b/src/msdos.c
index acdf076..30565e6 100644
--- a/src/msdos.c
+++ b/src/msdos.c
@@ -60,7 +60,7 @@ static int disk_pread(disk_t *disk_car, void *buf, const unsigned int count, con
static int disk_pwrite(disk_t *disk_car, const void *buf, const unsigned int count, const uint64_t hd_offset);
static int disk_nopwrite(disk_t *disk_car, const void *buf, const unsigned int count, const uint64_t offset);
static int disk_sync(disk_t *disk_car);
-static int disk_clean(disk_t *disk_car);
+static void disk_clean(disk_t *disk_car);
static int cmd_dos_segment = 0;
static int cmd_dos_selector = 0;
@@ -542,7 +542,7 @@ static int disk_sync(disk_t *disk_car)
return -1;
}
-static int disk_clean(disk_t *disk_car)
+static void disk_clean(disk_t *disk_car)
{
/*
if(disk_car->data!=NULL)
@@ -550,7 +550,7 @@ static int disk_clean(disk_t *disk_car)
struct info_disk_struct *data=disk_car->data;
}
*/
- return generic_clean(disk_car);
+ generic_clean(disk_car);
}
static int hd_report_error(disk_t *disk_car, const uint64_t hd_offset, const unsigned int count, const int rc)
diff --git a/src/win32.c b/src/win32.c
index 0a1f4a5..fa93b6f 100644
--- a/src/win32.c
+++ b/src/win32.c
@@ -59,7 +59,7 @@ static unsigned int file_win32_compute_sector_size(HANDLE handle);
static uint64_t filewin32_getfilesize(HANDLE handle, const char *device);
static const char *file_win32_description(disk_t *disk_car);
static const char *file_win32_description_short(disk_t *disk_car);
-static int file_win32_clean(disk_t *disk_car);
+static void file_win32_clean(disk_t *disk_car);
static void *file_win32_pread_fast(disk_t *disk, void *buf, const unsigned int count, const uint64_t offset);
static int file_win32_pread(disk_t *disk_car, void *buf, const unsigned int count, const uint64_t offset);
static int file_win32_pwrite(disk_t *disk_car, const void *buf, const unsigned int count, const uint64_t offset);
@@ -338,11 +338,7 @@ disk_t *file_test_availability_win32(const char *device, const int verbose, int
if(disk_car->disk_real_size!=0)
return disk_car;
log_warning("Warning: can't get size for %s\n",device);
- free(data);
- free(disk_car->device);
- free(disk_car->model);
- free(disk_car);
- CloseHandle(handle);
+ file_win32_clean(disk_car);
}
return NULL;
}
@@ -401,14 +397,14 @@ static const char *file_win32_description_short(disk_t *disk_car)
return disk_car->description_short_txt;
}
-static int file_win32_clean(disk_t *disk_car)
+static void file_win32_clean(disk_t *disk)
{
- if(disk_car->data!=NULL)
+ if(disk->data!=NULL)
{
- struct info_file_win32_struct *data=(struct info_file_win32_struct *)disk_car->data;
+ struct info_file_win32_struct *data=(struct info_file_win32_struct *)disk->data;
CloseHandle(data->handle);
}
- return generic_clean(disk_car);
+ generic_clean(disk);
}
static unsigned int file_win32_compute_sector_size(HANDLE handle)