]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
Free map to avoid resource leak issues
authorGuoqing Jiang <gqjiang@suse.com>
Mon, 11 Jun 2018 09:03:44 +0000 (17:03 +0800)
committerJes Sorensen <jsorensen@fb.com>
Mon, 11 Jun 2018 10:35:41 +0000 (06:35 -0400)
1. There are some places which didn't free map as
discovered by coverity.

CID 289661 (#1 of 1): Resource leak (RESOURCE_LEAK)12. leaked_storage: Variable mapl going out of scope leaks the storage it points to.
CID 289619 (#3 of 3): Resource leak (RESOURCE_LEAK)63. leaked_storage: Variable map going out of scope leaks the storage it points to.
CID 289618 (#1 of 1): Resource leak (RESOURCE_LEAK)26. leaked_storage: Variable map going out of scope leaks the storage it points to.
CID 289607 (#1 of 1): Resource leak (RESOURCE_LEAK)41. leaked_storage: Variable map going out of scope leaks the storage it points to.

2. If we call map_by_* inside a loop, then map_free
should be called in the same loop, and it is better
to set map to NULL after free.

3. And map_unlock is always called with map_lock,
if we don't call map_remove before map_unlock,
then the memory (allocated by  map_lock -> map_read
-> map_add -> xmalloc) could be leaked. So we
need to free it in map_unlock as well.

Signed-off-by: Guoqing Jiang <gqjiang@suse.com>
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
Assemble.c
Detail.c
Incremental.c
config.c
mapfile.c
mdadm.c

index 32e6f6ffd1c82c36f05d1148be0902203ac4d0e7..5a907c14aa4e333613891e69825d45ee3b842b94 100644 (file)
@@ -1851,8 +1851,8 @@ try_again:
        if (rv == 1 && !pre_exist)
                ioctl(mdfd, STOP_ARRAY, NULL);
        free(devices);
-       map_unlock(&map);
 out:
+       map_unlock(&map);
        if (rv == 0) {
                wait_for(chosen_name, mdfd);
                close(mdfd);
index 860241ce201717e70d4fa1f68f9d6a8fe66bb5b3..b3e857a7e2c934a06ff43451cde56f186f539ec5 100644 (file)
--- a/Detail.c
+++ b/Detail.c
@@ -263,6 +263,7 @@ int Detail(char *dev, struct context *c)
 
                        if (st->ss->export_detail_super)
                                st->ss->export_detail_super(st);
+                       map_free(map);
                } else {
                        struct map_ent *mp, *map = NULL;
                        char nbuf[64];
@@ -277,6 +278,7 @@ int Detail(char *dev, struct context *c)
                                print_escape(mp->path+8);
                                putchar('\n');
                        }
+                       map_free(map);
                }
                if (sra) {
                        struct mdinfo *mdi;
index 0beab163e642b6df38c3b656b266f85bfb8cd513..0c5698ee0ad5babd3525a1cd110ebe71729547f8 100644 (file)
@@ -1413,6 +1413,7 @@ restart:
                        sysfs_free(sra);
                }
        }
+       map_free(mapl);
        return rv;
 }
 
@@ -1587,6 +1588,8 @@ static int Incremental_container(struct supertype *st, char *devname,
 
                assemble_container_content(st, mdfd, ra, c,
                                           chosen_name, &result);
+               map_free(map);
+               map = NULL;
                close(mdfd);
        }
        if (c->export && result) {
@@ -1663,6 +1666,7 @@ static int Incremental_container(struct supertype *st, char *devname,
                        close(sfd);
        }
        domain_free(domains);
+       map_free(map);
        return 0;
 }
 
index 48e0278880cf937a55f684092165af738642a784..e14eae0cdab31b62b034063fd32ead125bdccc41 100644 (file)
--- a/config.c
+++ b/config.c
@@ -181,9 +181,10 @@ struct mddev_dev *load_containers(void)
                        }
                        d->next = rv;
                        rv = d;
+                       map_free(map);
+                       map = NULL;
                }
        free_mdstat(mdstat);
-       map_free(map);
 
        return rv;
 }
index f3c8191e80b7485760152db11df90bb2548c70f8..a50255632d285ccae210d91dfabbc4da2e3501fb 100644 (file)
--- a/mapfile.c
+++ b/mapfile.c
@@ -143,6 +143,8 @@ void map_unlock(struct map_ent **melp)
                unlink(mapname[2]);
                fclose(lf);
        }
+       if (*melp)
+               map_free(*melp);
        lf = NULL;
 }
 
diff --git a/mdadm.c b/mdadm.c
index 5afe4155d7011f2f3b653ff162dedeb940cdd911..1cf5c189a0061f8d326e7ffd5b9f01ffb1b07081 100644 (file)
--- a/mdadm.c
+++ b/mdadm.c
@@ -1885,6 +1885,8 @@ static int misc_scan(char devmode, struct context *c)
                        else
                                rv |= WaitClean(name, c->verbose);
                        put_md_name(name);
+                       map_free(map);
+                       map = NULL;
                }
        }
        free_mdstat(ms);