backends/vhost-user: fix memory leak when vhost_dev_init() fails
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.
--- backends/vhost-user.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
backends/vhost-user.c +5 -1
@@ -24,6 +24,7 @@ intvhost_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
Quoted 7 lines · Stefano Garzarella
From: Stefano Garzarella <[email protected]>
vhost_dev_init()clears the structvhost_devin the error path callingvhost_dev_cleanup(). So, to prevent memory leaks, we need to keep a local reference to thevhost_virtqueuearray and deallocate it in case of failure, just as most other callers ofvhost_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")Signed-off-by Stefano Garzarella <[email protected]>---
Thanks,
Luigi
Quoted 7 lines · Stefano Garzarella
From: Stefano Garzarella <[email protected]>
vhost_dev_init()clears the structvhost_devin the error path callingvhost_dev_cleanup(). So, to prevent memory leaks, we need to keep a local reference to thevhost_virtqueuearray and deallocate it in case of failure, just as most other callers ofvhost_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")Signed-off-by Stefano Garzarella <[email protected]>---
Best regards,
Alexandr