From 4d0419ed71ceae109e6cb64bbd9697342c7fe69b Mon Sep 17 00:00:00 2001 From: Binbin Wu Date: Fri, 8 Mar 2019 10:30:07 +0800 Subject: [PATCH] dm: passthru: fix potentail mem leaks Fix potential memory leakage in some error case handling. Tracked-On: #2704 Signed-off-by: Binbin Wu Reviewed-by: Yonghua Huang --- devicemodel/hw/pci/passthrough.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/devicemodel/hw/pci/passthrough.c b/devicemodel/hw/pci/passthrough.c index f6fc89113..be26b84d2 100644 --- a/devicemodel/hw/pci/passthrough.c +++ b/devicemodel/hw/pci/passthrough.c @@ -793,7 +793,8 @@ passthru_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) ptdev = calloc(1, sizeof(struct passthru_dev)); if (ptdev == NULL) { warnx("%s: calloc FAIL!", __func__); - return -ENOMEM; + error = -ENOMEM; + goto done; } ptdev->phys_bdf = PCI_BDF(bus, slot, func); @@ -802,7 +803,7 @@ passthru_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) error = pciaccess_init(); if (error < 0) - return error; + goto done; error = -ENODEV; iter = pci_slot_match_iterator_create(NULL); @@ -817,7 +818,7 @@ passthru_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) if (error < 0) { warnx("No physical PCI device %x:%x.%x!", bus, slot, func); - return -ENODEV; + goto done; } pci_device_probe(ptdev->phys_dev); @@ -867,7 +868,9 @@ passthru_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) error = 0; /* success */ done: if (error) { - free(ptdev); + if (ptdev != NULL) { + free(ptdev); + } vm_unassign_ptdev(ctx, bus, slot, func); } return error;