From 0c0371fc9e1127e72a36a9f309869857aa1ded95 Mon Sep 17 00:00:00 2001 From: Tianhua Sun Date: Fri, 14 Jun 2019 16:04:47 +0800 Subject: [PATCH] dm: fix some potential memory leaks This patch is to fix some potential memory leak issues 1, free mrp if the mmio_rb_lookup() function return 0; 2, free memory allocated by strdup in some error case handling. Tracked-On: #3277 Signed-off-by: Tianhua Sun Acked-by: Anthony Xu --- devicemodel/core/mem.c | 5 ++--- devicemodel/hw/pci/xhci.c | 2 ++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/devicemodel/core/mem.c b/devicemodel/core/mem.c index 4994eb221..8ff85d908 100644 --- a/devicemodel/core/mem.c +++ b/devicemodel/core/mem.c @@ -265,7 +265,7 @@ register_mem_int(struct mmio_rb_tree *rbt, struct mem_range *memp) struct mmio_rb_range *entry, *mrp; int err; - err = 0; + err = -1; mrp = malloc(sizeof(struct mmio_rb_range)); @@ -280,8 +280,7 @@ register_mem_int(struct mmio_rb_tree *rbt, struct mem_range *memp) pthread_rwlock_unlock(&mmio_rwlock); if (err) free(mrp); - } else - err = -1; + } return err; } diff --git a/devicemodel/hw/pci/xhci.c b/devicemodel/hw/pci/xhci.c index da459b270..d02671901 100644 --- a/devicemodel/hw/pci/xhci.c +++ b/devicemodel/hw/pci/xhci.c @@ -3997,6 +3997,8 @@ errout: free(xdev->portregs); xdev->portregs = NULL; } + if (rc < -2 && s) + free(s); UPRINTF(LFTL, "fail to parse xHCI options, rc=%d\r\n", rc); if (opts)