Skip to contentSkip to conversation

vhost-user-scsi: Fix memleaks in vus_proc_req()

PATCH Superseded Alex Chen 2 participants 3 messages Nov 24, 2020 – Nov 25, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Alex Chen

author
#0 lore raw

PATCH 1/1 vhost-user-scsi: Fix memleaks in vus_proc_req()

The 'elem' is allocated memory in vu_queue_pop(), and it's memory should be freed in all error branchs after vu_queue_pop().
In addition, in order to free 'elem' memory outside of while(1) loop, move the definition of 'elem' to the begin of vus_proc_req().

---
 contrib/vhost-user-scsi/vhost-user-scsi.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
contrib/vhost-user-scsi/vhost-user-scsi.c +2 -1
index 0f9ba4b2a2..4639440a70 100644
@@ -232,6 +232,7 @@ static void vus_proc_req(VuDev *vu_dev, int idx)
     VugDev *gdev;
     VusDev *vdev_scsi;
     VuVirtq *vq;
+    VuVirtqElement *elem = NULL;
 
     assert(vu_dev);
 
@@ -248,7 +249,6 @@ static void vus_proc_req(VuDev *vu_dev, int idx)
     g_debug("Got kicked on vq[%d]@%p", idx, vq);
 
     while (1) {
-        VuVirtqElement *elem;
         VirtIOSCSICmdReq *req;
         VirtIOSCSICmdResp *rsp;
 
@@ -288,6 +288,7 @@ static void vus_proc_req(VuDev *vu_dev, int idx)
 
         free(elem);
     }
+    free(elem);
 }
 
 static void vus_queue_set_started(VuDev *vu_dev, int idx, bool started)
Signature · 1 line
2.19.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Alex Chen

The 'elem' is allocated memory in vu_queue_pop(), and it's memory should be freed in all error branchs after vu_queue_pop().

s/branchs/branches

Quoted 2 lines

In addition, in order to free 'elem' memory outside of while(1) loop, move the definition of 'elem' to the begin of vus_proc_req().

s/begin/beginning

Quoted 3 lines

Other than spelling in the commit message, looks good to me.

Quoted 36 lines
---
 contrib/vhost-user-scsi/vhost-user-scsi.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
contrib/vhost-user-scsi/vhost-user-scsi.c +2 -2
index 0f9ba4b2a2..4639440a70 100644
@@ -232,6 +232,7 @@ static void vus_proc_req(VuDev *vu_dev, int idx)
     VugDev *gdev;
     VusDev *vdev_scsi;
     VuVirtq *vq;
+    VuVirtqElement *elem = NULL;
 
     assert(vu_dev);
 
@@ -248,7 +249,6 @@ static void vus_proc_req(VuDev *vu_dev, int idx)
     g_debug("Got kicked on vq[%d]@%p", idx, vq);
 
     while (1) {
-        VuVirtqElement *elem;
         VirtIOSCSICmdReq *req;
         VirtIOSCSICmdResp *rsp;
 
@@ -288,6 +288,7 @@ static void vus_proc_req(VuDev *vu_dev, int idx)
 
         free(elem);
     }
+    free(elem);
 }
 
 static void vus_queue_set_started(VuDev *vu_dev, int idx, bool started)
--

2.19.1

author
#2 lore raw
Quoted 10 lines · Raphael Norwitz
Quoted 3 lines · Alex Chen

The 'elem' is allocated memory in vu_queue_pop(), and it's memory should be freed in all error branchs after vu_queue_pop().

s/branchs/branches

Quoted 2 lines

In addition, in order to free 'elem' memory outside of while(1) loop, move the definition of 'elem' to the begin of vus_proc_req().

Quoted 8 lines

s/begin/beginning

Quoted 3 lines

Other than spelling in the commit message, looks good to me.

Thanks for your review, I will fix it and send patch v2.

Thanks,
Alex

Quoted 40 lines
Quoted 33 lines
---
 contrib/vhost-user-scsi/vhost-user-scsi.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
contrib/vhost-user-scsi/vhost-user-scsi.c +2 -2
index 0f9ba4b2a2..4639440a70 100644
@@ -232,6 +232,7 @@ static void vus_proc_req(VuDev *vu_dev, int idx)
     VugDev *gdev;
     VusDev *vdev_scsi;
     VuVirtq *vq;
+    VuVirtqElement *elem = NULL;
 
     assert(vu_dev);
 
@@ -248,7 +249,6 @@ static void vus_proc_req(VuDev *vu_dev, int idx)
     g_debug("Got kicked on vq[%d]@%p", idx, vq);
 
     while (1) {
-        VuVirtqElement *elem;
         VirtIOSCSICmdReq *req;
         VirtIOSCSICmdResp *rsp;
 
@@ -288,6 +288,7 @@ static void vus_proc_req(VuDev *vu_dev, int idx)
 
         free(elem);
     }
+    free(elem);
 }
 
 static void vus_queue_set_started(VuDev *vu_dev, int idx, bool started)
--
Quoted 3 lines

2.19.1

.