From 7ab6e7ea03e3607590eff6404fd478fe3f5d81a7 Mon Sep 17 00:00:00 2001 From: Conghui Chen Date: Tue, 12 Mar 2019 07:17:32 +0800 Subject: [PATCH] dm: usb: fix possible memory leak fix possible memory leak for usb. Tracked-On: #2704 Signed-off-by: Conghui Chen Reviewed-by: Yuan Liu Reviewed-by: Yonghua Huang --- devicemodel/hw/pci/xhci.c | 18 ++++++++++++++++-- devicemodel/hw/platform/usb_pmapper.c | 5 +++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/devicemodel/hw/pci/xhci.c b/devicemodel/hw/pci/xhci.c index e20a8c9cb..e4749152d 100644 --- a/devicemodel/hw/pci/xhci.c +++ b/devicemodel/hw/pci/xhci.c @@ -4012,8 +4012,8 @@ errout: for (i = 1; i <= XHCI_MAX_DEVS && xdev->devices[i]; i++) free(xdev->devices[i]); xdev->ndevices = 0; - xdev->devices = NULL; free(xdev->devices); + xdev->devices = NULL; } if (xdev->slots) { free(xdev->slots); @@ -4170,7 +4170,21 @@ pci_xhci_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) done: if (error) { UPRINTF(LFTL, "%s fail, error=%d\n", __func__, error); - free(xdev); + if (xdev) { + if (xdev->devices) { + free(xdev->devices); + xdev->devices = NULL; + } + if (xdev->slots) { + free(xdev->slots); + xdev->slots = NULL; + } + if (xdev->portregs) { + free(xdev->portregs); + xdev->portregs = NULL; + } + free(xdev); + } } return error; diff --git a/devicemodel/hw/platform/usb_pmapper.c b/devicemodel/hw/platform/usb_pmapper.c index cf15d83d9..f08e159b8 100755 --- a/devicemodel/hw/platform/usb_pmapper.c +++ b/devicemodel/hw/platform/usb_pmapper.c @@ -857,6 +857,11 @@ usb_dev_data(void *pdata, struct usb_data_xfer *xfer, int dir, int epctx) } else { UPRINTF(LFTL, "%s: wrong endpoint type %d\r\n", __func__, type); + if (req->buffer) + free(req->buffer); + if (req->libusb_xfer) + libusb_free_transfer(req->libusb_xfer); + free(req); xfer->status = USB_ERR_INVAL; }