diff mbox series

[2/4] Fix memory leak in file Kill

Message ID 20230424080637.2152893-3-miaoguanqin@huawei.com (mailing list archive)
State Mainlined, archived
Delegated to: Jes Sorensen
Headers show
Series Fix memory leak for Manage Assemble Kill mdadm | expand

Commit Message

Guanqin Miao April 24, 2023, 8:06 a.m. UTC
When we test mdadm with asan, we found some memory leaks in Kill.c
We fix these memory leaks based on code logic.

Signed-off-by: Guanqin Miao <miaoguanqin@huawei.com>
Signed-off-by: Li Xiao Keng <lixiaokeng@huawei.com>
---
 Kill.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

Comments

Mariusz Tkaczyk Aug. 23, 2023, 1:49 p.m. UTC | #1
On Mon, 24 Apr 2023 16:06:35 +0800
Guanqin Miao <miaoguanqin@huawei.com> wrote:

> When we test mdadm with asan, we found some memory leaks in Kill.c
> We fix these memory leaks based on code logic.
> 
> Signed-off-by: Guanqin Miao <miaoguanqin@huawei.com>
> Signed-off-by: Li Xiao Keng <lixiaokeng@huawei.com>
> ---
Acked-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
diff mbox series

Patch

diff --git a/Kill.c b/Kill.c
index bfd0efdc..43c9abed 100644
--- a/Kill.c
+++ b/Kill.c
@@ -41,6 +41,7 @@  int Kill(char *dev, struct supertype *st, int force, int verbose, int noexcl)
 	 *  4 - failed to find a superblock.
 	 */
 
+	bool free_super = false;
 	int fd, rv = 0;
 
 	if (force)
@@ -52,8 +53,10 @@  int Kill(char *dev, struct supertype *st, int force, int verbose, int noexcl)
 				dev);
 		return 2;
 	}
-	if (st == NULL)
+	if (st == NULL) {
 		st = guess_super(fd);
+		free_super = true;
+	}
 	if (st == NULL || st->ss->init_super == NULL) {
 		if (verbose >= 0)
 			pr_err("Unrecognised md component device - %s\n", dev);
@@ -77,6 +80,10 @@  int Kill(char *dev, struct supertype *st, int force, int verbose, int noexcl)
 			rv = 0;
 		}
 	}
+	if (free_super && st) {
+		st->ss->free_super(st);
+		free(st);
+	}
 	close(fd);
 	return rv;
 }