disk/core: Fix memory leakage in open all disks
Fix memory leakage in disk/core disk_image__open_all when malloc disk
failed, should free the disks that already malloced.
Signed-off-by: Lei Xiang <leixiang@kylinos.cn>
Suggested-by: Xie Ming <xieming@kylinos.cn>
Suggested-by: Alexandru Elisei <alexandru.elisei@arm.com>
Suggested-by: Will Deacon <will@kernel.org>
Signed-off-by: Will Deacon <will@kernel.org>
diff --git a/disk/core.c b/disk/core.c
index b00b0c0..91db277 100644
--- a/disk/core.c
+++ b/disk/core.c
@@ -171,8 +171,10 @@
if (wwpn) {
disks[i] = malloc(sizeof(struct disk_image));
- if (!disks[i])
- return ERR_PTR(-ENOMEM);
+ if (!disks[i]) {
+ err = ERR_PTR(-ENOMEM);
+ goto error;
+ }
disks[i]->wwpn = wwpn;
continue;
}
@@ -191,10 +193,15 @@
return disks;
error:
- for (i = 0; i < count; i++)
- if (!IS_ERR_OR_NULL(disks[i]))
- disk_image__close(disks[i]);
+ for (i = 0; i < count; i++) {
+ if (IS_ERR_OR_NULL(disks[i]))
+ continue;
+ if (disks[i]->wwpn)
+ free(disks[i]);
+ else
+ disk_image__close(disks[i]);
+ }
free(disks);
return err;
}