Skip to contentSkip to conversation

backends/vhost-user: fix memory leak when vhost_dev_init() fails

PATCH In review Stefano Garzarella 3 participants 3 messages Nov 5 – Nov 9

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 backends/vhost-user: fix memory leak when vhost_dev_init() fails

From: Stefano Garzarella <[email protected]>

vhost_dev_init() clears the struct vhost_dev in the error path calling vhost_dev_cleanup(). So, to prevent memory leaks, we need to keep a local reference to the vhost_virtqueue array and deallocate it in case of failure, just as most other callers of vhost_dev_init() do.

Discovered while reviewing a patch by Alexandr Moshkov fixing the same issue in vhost-user-fs.

Fixes f24bb7287ddb ("Add vhost-user-backend")
---
 backends/vhost-user.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
backends/vhost-user.c +5 -1
index de6b1ba267..a3cd15a57e 100644
@@ -24,6 +24,7 @@ int
 vhost_user_backend_dev_init(VhostUserBackend *b, VirtIODevice *vdev,
                             unsigned nvqs, Error **errp)
 {
+    struct vhost_virtqueue *vqs;
     int ret;
 
     assert(!b->vdev && vdev);
@@ -32,13 +33,16 @@ vhost_user_backend_dev_init(VhostUserBackend *b, VirtIODevice *vdev,
         return -1;
     }
 
+    vqs = g_new0(struct vhost_virtqueue, nvqs);
+
     b->vdev = vdev;
     b->dev.nvqs = nvqs;
-    b->dev.vqs = g_new0(struct vhost_virtqueue, nvqs);
+    b->dev.vqs = vqs;
 
     ret = vhost_dev_init(&b->dev, &b->vhost_user, VHOST_BACKEND_TYPE_USER, 0,
                          errp);
     if (ret < 0) {
+        g_free(vqs);
         return -1;
     }
 
Signature · 1 line
2.56.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

vhost_dev_init() clears the struct vhost_dev in the error path calling vhost_dev_cleanup(). So, to prevent memory leaks, we need to keep a local reference to the vhost_virtqueue array and deallocate it in case of failure, just as most other callers of vhost_dev_init() do.

Quoted 7 lines

Discovered while reviewing a patch by Alexandr Moshkov fixing the same issue in vhost-user-fs.

Fixes f24bb7287ddb ("Add vhost-user-backend")

---

Thanks,
Luigi

Reviewed-by on v1 1/1
#2 lore raw
In reply to Stefano Garzarella
Quoted 7 lines · Stefano Garzarella

From: Stefano Garzarella <[email protected]>

vhost_dev_init() clears the struct vhost_dev in the error path calling vhost_dev_cleanup(). So, to prevent memory leaks, we need to keep a local reference to the vhost_virtqueue array and deallocate it in case of failure, just as most other callers of vhost_dev_init() do.

Quoted 6 lines

Discovered while reviewing a patch by Alexandr Moshkov fixing the same issue in vhost-user-fs.

Fixes f24bb7287ddb ("Add vhost-user-backend")

---

Best regards,

Alexandr