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")
Signed-off-by: Stefano Garzarella <[email protected]>
---
 backends/vhost-user.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/backends/vhost-user.c b/backends/vhost-user.c
index de6b1ba267..a3cd15a57e 100644
--- a/backends/vhost-user.c
+++ b/backends/vhost-user.c
@@ -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;
     }
 
-- 
2.56.0


Reply via email to