]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
Fix memory leak in file Kill
authorGuanqin Miao <miaoguanqin@huawei.com>
Mon, 24 Apr 2023 08:06:35 +0000 (16:06 +0800)
committerJes Sorensen <jes@trained-monkey.org>
Fri, 1 Sep 2023 16:08:06 +0000 (12:08 -0400)
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>
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
Kill.c

diff --git a/Kill.c b/Kill.c
index bfd0efdc0b1d463bda64756cb51a2f487ba58de3..43c9abed3b42b512448ca49cba943ba4ebe5603c 100644 (file)
--- 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;
 }