Re: [PATCH] kvm tools:Fix memory leakage in open all disks

雷翔
2024-08-07T06:18:19Z

[-- Attachment #1: Type: text/html, Size: 3802 bytes --]

[-- Attachment #2: v4-0001-kvmtool-disk-core-Fix-memory-leakage-in-open-all-.patch --]
[-- Type: application/octet-stream, Size: 1479 bytes --]

From f9ebbe412cffafbf06c738336ee45942d7c1977f Mon Sep 17 00:00:00 2001
From: leixiang <leixiang@kylinos.cn>
Date: Wed, 10 Jul 2024 17:45:51 +0800
Subject: [PATCH v4] kvmtool: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>
---
disk/core.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)

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 @@ static struct disk_image **disk_image__open_all(struct kvm *kvm)

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 @@ static struct disk_image **disk_image__open_all(struct kvm *kvm)

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;
}
--
2.34.1