]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
Fix memory leak in file Assemble
authorGuanqin Miao <miaoguanqin@huawei.com>
Mon, 24 Apr 2023 08:06:34 +0000 (16:06 +0800)
committerJes Sorensen <jes@trained-monkey.org>
Fri, 1 Sep 2023 16:06:53 +0000 (12:06 -0400)
When we test mdadm with asan, we found some memory leaks in Assemble.c
We fix these memory leaks based on code logic.

v2: Set st = NULL before jumping to loop

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>
Assemble.c

index 498049415955a48620b198eef238a31bf5373d10..61e8cd17e33265ade0de725b2f28b5c3015682cf 100644 (file)
@@ -341,8 +341,10 @@ static int select_devices(struct mddev_dev *devlist,
                                st->ss->free_super(st);
                        dev_policy_free(pol);
                        domain_free(domains);
-                       if (tst)
+                       if (tst) {
                                tst->ss->free_super(tst);
+                               free(tst);
+                       }
                        return -1;
                }
 
@@ -417,6 +419,7 @@ static int select_devices(struct mddev_dev *devlist,
                                st->ss->free_super(st);
                                dev_policy_free(pol);
                                domain_free(domains);
+                               free(st);
                                return -1;
                        }
                        if (c->verbose > 0)
@@ -425,6 +428,8 @@ static int select_devices(struct mddev_dev *devlist,
 
                        /* make sure we finished the loop */
                        tmpdev = NULL;
+                       free(st);
+                       st = NULL;
                        goto loop;
                } else {
                        content = *contentp;
@@ -533,6 +538,7 @@ static int select_devices(struct mddev_dev *devlist,
                                st->ss->free_super(st);
                                dev_policy_free(pol);
                                domain_free(domains);
+                               free(tst);
                                return -1;
                        }
                        tmpdev->used = 1;
@@ -546,8 +552,10 @@ static int select_devices(struct mddev_dev *devlist,
                }
                dev_policy_free(pol);
                pol = NULL;
-               if (tst)
+               if (tst) {
                        tst->ss->free_super(tst);
+                       free(tst);
+               }
        }
 
        /* Check if we found some imsm spares but no members */
@@ -839,6 +847,7 @@ static int load_devices(struct devs *devices, char *devmap,
                                close(mdfd);
                                free(devices);
                                free(devmap);
+                               free(best);
                                *stp = st;
                                return -1;
                        }
@@ -1950,6 +1959,7 @@ out:
        } else if (mdfd >= 0)
                close(mdfd);
 
+       free(best);
        /* '2' means 'OK, but not started yet' */
        if (rv == -1) {
                free(devices);