Linux-f2fs-devel Archive on lore.kernel.org
 help / color / Atom feed
* [f2fs-dev] [PATCH 1/3] sload.f2fs: fix memory leak in build_directory
@ 2019-08-05  1:44 Chao Yu
  2019-08-05  1:44 ` [f2fs-dev] [PATCH 2/3] sload.f2fs: fix memory leak of dentry.link Chao Yu
  2019-08-05  1:44 ` [f2fs-dev] [PATCH 3/3] sload.f2fs: fix error path of build_directory Chao Yu
  0 siblings, 2 replies; 3+ messages in thread
From: Chao Yu @ 2019-08-05  1:44 UTC (permalink / raw)
  To: jaegeuk; +Cc: Xiaojun Wang, linux-f2fs-devel

From: Xiaojun Wang <wangxiaojun11@huawei.com>

This patch fix bug that variable dentries going out of scope leaks
the storage it points to.

Signed-off-by: Xiaojun Wang <wangxiaojun11@huawei.com>
Reviewed-by: Chao Yu <yuchao0@huawei.com>
---
 fsck/sload.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/fsck/sload.c b/fsck/sload.c
index f5a4651..940da5c 100644
--- a/fsck/sload.c
+++ b/fsck/sload.c
@@ -240,12 +240,18 @@ static int build_directory(struct f2fs_sb_info *sbi, const char *full_path,
 		ret = set_selinux_xattr(sbi, dentries[i].path,
 					dentries[i].ino, dentries[i].mode);
 		if (ret)
-			return ret;
+			goto out_free;
 
 		free(dentries[i].path);
 		free(dentries[i].full_path);
 		free((void *)dentries[i].name);
 	}
+out_free:
+	for (; i < entries; i++) {
+		free(dentries[i].path);
+		free(dentries[i].full_path);
+		free((void *)dentries[i].name);
+	}
 
 	free(dentries);
 	return 0;
-- 
2.18.0.rc1



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [f2fs-dev] [PATCH 2/3] sload.f2fs: fix memory leak of dentry.link
  2019-08-05  1:44 [f2fs-dev] [PATCH 1/3] sload.f2fs: fix memory leak in build_directory Chao Yu
@ 2019-08-05  1:44 ` Chao Yu
  2019-08-05  1:44 ` [f2fs-dev] [PATCH 3/3] sload.f2fs: fix error path of build_directory Chao Yu
  1 sibling, 0 replies; 3+ messages in thread
From: Chao Yu @ 2019-08-05  1:44 UTC (permalink / raw)
  To: jaegeuk; +Cc: linux-f2fs-devel

We forgot to release memory allocated in dentry.link, fix to release
it after last use of it.

Signed-off-by: Chao Yu <yuchao0@huawei.com>
---
 fsck/dir.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/fsck/dir.c b/fsck/dir.c
index 92d7e05..0984d26 100644
--- a/fsck/dir.c
+++ b/fsck/dir.c
@@ -523,11 +523,15 @@ static void init_inode_block(struct f2fs_sb_info *sbi,
 	node_blk->footer.flag = 0;
 	node_blk->footer.cp_ver = ckpt->checkpoint_ver;
 
-	if (S_ISDIR(mode))
+	if (S_ISDIR(mode)) {
 		make_empty_dir(sbi, node_blk);
-	else if (S_ISLNK(mode))
+	} else if (S_ISLNK(mode)) {
 		page_symlink(sbi, node_blk, de->link, size);
 
+		free(de->link);
+		de->link = NULL;
+	}
+
 	if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
 		node_blk->i.i_inode_checksum =
 			cpu_to_le32(f2fs_inode_chksum(node_blk));
-- 
2.18.0.rc1



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [f2fs-dev] [PATCH 3/3] sload.f2fs: fix error path of build_directory
  2019-08-05  1:44 [f2fs-dev] [PATCH 1/3] sload.f2fs: fix memory leak in build_directory Chao Yu
  2019-08-05  1:44 ` [f2fs-dev] [PATCH 2/3] sload.f2fs: fix memory leak of dentry.link Chao Yu
@ 2019-08-05  1:44 ` Chao Yu
  1 sibling, 0 replies; 3+ messages in thread
From: Chao Yu @ 2019-08-05  1:44 UTC (permalink / raw)
  To: jaegeuk; +Cc: linux-f2fs-devel

The error handling of build_directory is a mess, fix it.

Signed-off-by: Chao Yu <yuchao0@huawei.com>
---
 fsck/sload.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/fsck/sload.c b/fsck/sload.c
index 940da5c..f601916 100644
--- a/fsck/sload.c
+++ b/fsck/sload.c
@@ -41,19 +41,25 @@ static int filter_dot(const struct dirent *d)
 	return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
 }
 
-static void f2fs_make_directory(struct f2fs_sb_info *sbi,
+static int f2fs_make_directory(struct f2fs_sb_info *sbi,
 				int entries, struct dentry *de)
 {
+	int ret = 0;
 	int i = 0;
 
 	for (i = 0; i < entries; i++) {
 		if (de[i].file_type == F2FS_FT_DIR)
-			f2fs_mkdir(sbi, de + i);
+			ret = f2fs_mkdir(sbi, de + i);
 		else if (de[i].file_type == F2FS_FT_REG_FILE)
-			f2fs_create(sbi, de + i);
+			ret = f2fs_create(sbi, de + i);
 		else if (de[i].file_type == F2FS_FT_SYMLINK)
-			f2fs_symlink(sbi, de + i);
+			ret = f2fs_symlink(sbi, de + i);
+
+		if (ret)
+			break;
 	}
+
+	return ret;
 }
 
 #ifdef HAVE_LIBSELINUX
@@ -173,7 +179,7 @@ static int build_directory(struct f2fs_sb_info *sbi, const char *full_path,
 	int entries = 0;
 	struct dentry *dentries;
 	struct dirent **namelist = NULL;
-	int i, ret = 0;
+	int i = 0, ret = 0;
 
 	entries = scandir(full_path, &namelist, filter_dot, (void *)alphasort);
 	if (entries < 0) {
@@ -182,8 +188,7 @@ static int build_directory(struct f2fs_sb_info *sbi, const char *full_path,
 	}
 
 	dentries = calloc(entries, sizeof(struct dentry));
-	if (dentries == NULL)
-		return -ENOMEM;
+	ASSERT(dentries);
 
 	for (i = 0; i < entries; i++) {
 		dentries[i].name = (unsigned char *)strdup(namelist[i]->d_name);
@@ -208,7 +213,9 @@ static int build_directory(struct f2fs_sb_info *sbi, const char *full_path,
 
 	free(namelist);
 
-	f2fs_make_directory(sbi, entries, dentries);
+	ret = f2fs_make_directory(sbi, entries, dentries);
+	if (ret)
+		goto out_free;
 
 	for (i = 0; i < entries; i++) {
 		if (dentries[i].file_type == F2FS_FT_REG_FILE) {
@@ -224,10 +231,15 @@ static int build_directory(struct f2fs_sb_info *sbi, const char *full_path,
 							dentries[i].path);
 			ASSERT(ret > 0);
 
-			build_directory(sbi, subdir_full_path, subdir_dir_path,
-					target_out_dir, dentries[i].ino);
+			ret = build_directory(sbi, subdir_full_path,
+						subdir_dir_path,
+						target_out_dir,
+						dentries[i].ino);
 			free(subdir_full_path);
 			free(subdir_dir_path);
+
+			if (ret)
+				goto out_free;
 		} else if (dentries[i].file_type == F2FS_FT_SYMLINK) {
 			/*
 			 * It is already done in f2fs_make_directory
-- 
2.18.0.rc1



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-05  1:44 [f2fs-dev] [PATCH 1/3] sload.f2fs: fix memory leak in build_directory Chao Yu
2019-08-05  1:44 ` [f2fs-dev] [PATCH 2/3] sload.f2fs: fix memory leak of dentry.link Chao Yu
2019-08-05  1:44 ` [f2fs-dev] [PATCH 3/3] sload.f2fs: fix error path of build_directory Chao Yu

Linux-f2fs-devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-f2fs-devel/0 linux-f2fs-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-f2fs-devel linux-f2fs-devel/ https://lore.kernel.org/linux-f2fs-devel \
		linux-f2fs-devel@lists.sourceforge.net linux-f2fs-devel@archiver.kernel.org
	public-inbox-index linux-f2fs-devel


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/net.sourceforge.lists.linux-f2fs-devel


AGPL code for this site: git clone https://public-inbox.org/ public-inbox