[Ocfs2-tools-devel] [PATCH 6/8 V2] Fix a memory leak problem in mkfs.c
piaojun
piaojun at huawei.com
Sun Mar 29 18:52:28 PDT 2015
In main(), 'root_dir', 'system_dir', 'orphan_dir', 's' and 'record' should
be freed at the end of the function.
Signed-off-by: Jun Piao <piaojun at huawei.com>
Reviewed-by: Alex Chen <alex.chen at huawei.com>
---
mkfs.ocfs2/mkfs.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 102 insertions(+), 3 deletions(-)
diff --git a/mkfs.ocfs2/mkfs.c b/mkfs.ocfs2/mkfs.c
index 7abea73..b6f366d 100644
--- a/mkfs.ocfs2/mkfs.c
+++ b/mkfs.ocfs2/mkfs.c
@@ -21,6 +21,7 @@
#include "mkfs.h"
static State *get_state(int argc, char **argv);
+static void free_state(State *s);
static int get_number(char *arg, uint64_t *res);
static void parse_journal_opts(char *progname, const char *opts,
uint64_t *journal_size_in_bytes,
@@ -36,7 +37,7 @@ static void do_pwrite(State *s, const void *buf, size_t count,
static AllocBitmap *initialize_bitmap(State *s, uint32_t bits,
uint32_t unit_bits, const char *name,
SystemFileDiskRecord *bm_record);
-
+static void free_global_bitmap(State *s, AllocBitmap *global_bm);
static int
find_clear_bits(void *buf, unsigned int size, uint32_t num_bits, uint32_t offset);
static int alloc_bytes_from_bitmap(State *s, uint64_t bytes,
@@ -46,6 +47,7 @@ static int alloc_from_bitmap(State *s, uint64_t num_bits, AllocBitmap *bitmap,
uint64_t *start, uint64_t *num);
static uint64_t alloc_inode(State *s, uint16_t *suballoc_bit);
static DirData *alloc_directory(State *s);
+static void free_directory(DirData *dir);
static void add_entry_to_directory(State *s, DirData *dir, char *name,
uint64_t byte_off, uint8_t type);
static uint32_t blocks_needed(State *s);
@@ -67,6 +69,7 @@ static void close_device(State *s);
static int initial_slots_for_volume(uint64_t size);
static void create_generation(State *s);
static void init_record(State *s, SystemFileDiskRecord *rec, int type, int mode);
+static void free_records(SystemFileDiskRecord *record[]);
static void print_state(State *s);
static void clear_both_ends(State *s);
static int ocfs2_clusters_per_group(int block_size,
@@ -76,6 +79,7 @@ static AllocGroup * initialize_alloc_group(State *s, const char *name,
uint64_t blkno,
uint16_t chain, uint16_t cpg,
uint16_t bpc);
+static void free_alloc_group(AllocGroup *group);
static void index_system_dirs(State *s, ocfs2_filesys *fs);
static void create_lost_found_dir(State *s, ocfs2_filesys *fs);
static void format_journals(State *s, ocfs2_filesys *fs);
@@ -818,11 +822,16 @@ main(int argc, char **argv)
if (!s->hb_dev)
finish_normal_format(s);
+ free_directory(root_dir);
+ free_directory(system_dir);
+ for (i = 0; i < s->initial_slots; ++i)
+ free_directory(orphan_dir[i]);
close_device(s);
if (!s->quiet)
printf("%s successful\n\n", s->progname);
-
+ free_state(s);
+ free_records(record);
return 0;
}
@@ -1103,6 +1112,10 @@ get_state(int argc, char **argv)
optind++;
s = malloc(sizeof(State));
+ if (!s) {
+ com_err(progname, 0, "Unable to allocate State structure.");
+ exit(1);
+ }
memset(s, 0, sizeof(State));
if (optind < argc) {
@@ -1227,6 +1240,33 @@ get_state(int argc, char **argv)
return s;
}
+static void
+free_state(State *s)
+{
+ if (!s)
+ return;
+
+ if (s->vol_label)
+ ocfs2_free(&s->vol_label);
+
+ if (s->device_name)
+ ocfs2_free(&s->device_name);
+
+ if (s->cluster_stack)
+ ocfs2_free(&s->cluster_stack);
+
+ if (s->cluster_name)
+ ocfs2_free(&s->cluster_name);
+
+ if (s->global_bm)
+ free_global_bitmap(s, s->global_bm);
+
+ if (s->system_group)
+ free_alloc_group(s->system_group);
+
+ ocfs2_free(&s);
+}
+
static int
get_number(char *arg, uint64_t *res)
{
@@ -1841,6 +1881,21 @@ initialize_alloc_group(State *s, const char *name,
return group;
}
+static void
+free_alloc_group(AllocGroup *group)
+{
+ if (!group)
+ return;
+
+ if (group->gd)
+ ocfs2_free(&group->gd);
+
+ if (group->name)
+ ocfs2_free(&group->name);
+
+ ocfs2_free(&group);
+}
+
static AllocBitmap *
initialize_bitmap(State *s, uint32_t bits, uint32_t unit_bits,
const char *name, SystemFileDiskRecord *bm_record)
@@ -1944,6 +1999,27 @@ initialize_bitmap(State *s, uint32_t bits, uint32_t unit_bits,
return bitmap;
}
+static void
+free_global_bitmap(State *s, AllocBitmap *global_bm)
+{
+ int i;
+
+ if (!global_bm)
+ return;
+
+ if (global_bm->name)
+ ocfs2_free(&global_bm->name);
+
+ if (global_bm->groups) {
+ for (i = 0; i < s->nr_cluster_groups; i++)
+ free_alloc_group(global_bm->groups[i]);
+
+ ocfs2_free(&global_bm->groups);
+ }
+
+ ocfs2_free(&global_bm);
+}
+
#if 0
static void
destroy_bitmap(AllocBitmap *bitmap)
@@ -2058,7 +2134,6 @@ alloc_from_bitmap(State *s, uint64_t num_bits, AllocBitmap *bitmap,
"start = %"PRIu64". used_bits = %u\n", num_bits, *num, *start,
bitmap->bm_record->bi.used_bits);
#endif
-
while (num_bits--) {
ocfs2_set_bit(start_bit, buf);
start_bit++;
@@ -2129,6 +2204,16 @@ alloc_directory(State *s)
}
static void
+free_directory(DirData *dir)
+{
+ if (dir) {
+ if (dir->buf)
+ ocfs2_free(&dir->buf);
+ ocfs2_free(&dir);
+ }
+}
+
+static void
add_entry_to_directory(State *s, DirData *dir, char *name, uint64_t byte_off,
uint8_t type)
{
@@ -2832,6 +2917,20 @@ init_record(State *s, SystemFileDiskRecord *rec, int type, int mode)
}
static void
+free_records(SystemFileDiskRecord *record[])
+{
+ int i;
+
+ if (!record)
+ return;
+
+ for (i = 0; i < NUM_SYSTEM_INODES; i++) {
+ if (record[i])
+ ocfs2_free(&record[i]);
+ }
+}
+
+static void
print_state(State *s)
{
int i;
-- 1.8.4.3
More information about the Ocfs2-tools-devel
mailing list