From 058b03c3a7f3a075d25d7c0f71f90ca624bf18a1 Mon Sep 17 00:00:00 2001 From: Junhao Gao Date: Fri, 8 Nov 2019 00:00:32 +0000 Subject: [PATCH] dm: fix memory free issue for xhci remove uninitialized variable "dir", then make sure "xfer->data","xfer->data[i].hcb","xfer->reqs" free correctly. Tracked-On: #4154 Signed-off-by: Junhao Gao Reviewed-by: Yonghua Huang Acked-by: Yu Wang --- devicemodel/hw/pci/xhci.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/devicemodel/hw/pci/xhci.c b/devicemodel/hw/pci/xhci.c index 08a9529bc..6b72da09c 100644 --- a/devicemodel/hw/pci/xhci.c +++ b/devicemodel/hw/pci/xhci.c @@ -1555,7 +1555,7 @@ pci_xhci_alloc_usb_xfer(struct pci_xhci_dev_emu *dev, int epid) struct usb_xfer *xfer; struct xhci_dev_ctx *dev_ctx; struct xhci_endp_ctx *ep_ctx; - int dir, max_blk_cnt, i = 0; + int max_blk_cnt, i = 0; uint8_t type; if (!dev) @@ -1585,8 +1585,8 @@ pci_xhci_alloc_usb_xfer(struct pci_xhci_dev_emu *dev, int epid) max_blk_cnt = 2048; break; default: - UPRINTF(LFTL, "err: unexpected epid %d type %d dir %d\r\n", - epid, type, dir); + UPRINTF(LFTL, "err: unexpected epid %d type %d\r\n", + epid, type); return NULL; } @@ -1608,8 +1608,8 @@ pci_xhci_alloc_usb_xfer(struct pci_xhci_dev_emu *dev, int epid) goto fail; } - UPRINTF(LINF, "allocate %d blocks for epid %d type %d dir %d\r\n", - max_blk_cnt, epid, type, dir); + UPRINTF(LINF, "allocate %d blocks for epid %d type %d\r\n", + max_blk_cnt, epid, type); xfer->max_blk_cnt = max_blk_cnt; xfer->dev = (void *)dev; @@ -1617,11 +1617,14 @@ pci_xhci_alloc_usb_xfer(struct pci_xhci_dev_emu *dev, int epid) return xfer; fail: - for (; i >= 0; i--) - free(xfer->data[i].hcb); - - free(xfer->data); - free(xfer->reqs); + if (xfer->data) { + for (; i >= 0; i--) + if (xfer->data[i].hcb) + free(xfer->data[i].hcb); + free(xfer->data); + } + if (xfer->reqs) + free(xfer->reqs); free(xfer); return NULL; }