[Ocfs2-tools-devel] [PATCH v3] fsck.ocfs2: the members of 'o2fsck_state' need to be freed at the end of main()

piaojun piaojun at huawei.com
Sat Oct 8 22:55:21 PDT 2016


The allocation for the members of 'o2fsck_state' by o2fsck_state_init()
need to be freed at last in case of memory leak.

Signed-off-by: Jun Piao <piaojun at huawei.com>
Reviewed-by: Joseph Qi <joseph.qi at huawei.com>
---
 fsck.ocfs2/fsck.c  | 20 +++++++++++++++++++-
 fsck.ocfs2/pass1.c |  6 ++----
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/fsck.ocfs2/fsck.c b/fsck.ocfs2/fsck.c
index dc5209d..481a7e7 100644
--- a/fsck.ocfs2/fsck.c
+++ b/fsck.ocfs2/fsck.c
@@ -232,6 +232,22 @@ errcode_t o2fsck_state_reinit(ocfs2_filesys *fs, o2fsck_state *ost)
 	return 0;
 }
 
+static void o2fsck_state_release(o2fsck_state *ost)
+{
+	if (ost->ost_dir_inodes)
+		ocfs2_bitmap_free(&ost->ost_dir_inodes);
+	if (ost->ost_reg_inodes)
+		ocfs2_bitmap_free(&ost->ost_reg_inodes);
+	if (ost->ost_allocated_clusters)
+		ocfs2_bitmap_free(&ost->ost_allocated_clusters);
+	if (ost->ost_duplicate_clusters)
+		ocfs2_bitmap_free(&ost->ost_duplicate_clusters);
+	if (ost->ost_icount_in_inodes)
+		o2fsck_icount_free(ost->ost_icount_in_inodes);
+	if (ost->ost_icount_refs)
+		o2fsck_icount_free(ost->ost_icount_refs);
+}
+
 static errcode_t check_superblock(o2fsck_state *ost)
 {
 	struct ocfs2_dinode *di = ost->ost_fs->fs_super;
@@ -1102,7 +1118,9 @@ close:
 		com_err(whoami, ret, "while closing file \"%s\"", filename);
 		/* XXX I wonder about this error.. */
 		fsck_mask |= FSCK_ERROR;
-	} 
+	}
+
+	o2fsck_state_release(ost);
 
 out:
 	return fsck_mask;
diff --git a/fsck.ocfs2/pass1.c b/fsck.ocfs2/pass1.c
index c076e28..1ff258a 100644
--- a/fsck.ocfs2/pass1.c
+++ b/fsck.ocfs2/pass1.c
@@ -83,6 +83,7 @@ void o2fsck_free_inode_allocs(o2fsck_state *ost)
 
 	for (i = 0; i < OCFS2_RAW_SB(ost->ost_fs->fs_super)->s_max_slots; i++)
 		ocfs2_free_cached_inode(ost->ost_fs, ost->ost_inode_allocs[i]);
+	ocfs2_free(&ost->ost_inode_allocs);
 }
 
 /* update our in memory images of the inode chain alloc bitmaps.  these
@@ -146,8 +147,6 @@ static void update_inode_alloc(o2fsck_state *ost,
 
 			ost->ost_write_inode_alloc_asked = 1;
 			ost->ost_write_inode_alloc = !!yn;
-			if (!ost->ost_write_inode_alloc)
-				o2fsck_free_inode_allocs(ost);
 		}
 		break;
 	}
@@ -1447,8 +1446,6 @@ static void write_inode_alloc(o2fsck_state *ost)
 			com_err(whoami, ret, "while trying to write back slot "
 				"%d's inode allocator", i);
 	}
-
-	o2fsck_free_inode_allocs(ost);
 }
 
 errcode_t o2fsck_pass1(o2fsck_state *ost)
@@ -1566,6 +1563,7 @@ out_free:
 	o2fsck_add_resource_track(&ost->ost_rt, &rt);
 
 out:
+	o2fsck_free_inode_allocs(ost);
 	if (ost->ost_prog) {
 		tools_progress_stop(ost->ost_prog);
 		setlinebuf(stdout);
-- 
1.9.5.msysgit.1




More information about the Ocfs2-tools-devel mailing list