diff mbox series

[2/2] hw/nvme: fix memory leak in nvme_dsm

Message ID 20230411190448.64863-3-its@irrelevant.dk
State New
Headers show
Series hw/nvme: coverity fixes | expand

Commit Message

Klaus Jensen April 11, 2023, 7:04 p.m. UTC
From: Klaus Jensen <k.jensen@samsung.com>

The iocb (and the allocated memory to hold LBA ranges) leaks if reading
the LBA ranges fails.

Fix this by adding a free and an unref of the iocb.

Reported-by: Coverity (CID 1508281)
Fixes: d7d1474fd85d ("hw/nvme: reimplement dsm to allow cancellation")
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
---
 hw/nvme/ctrl.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Philippe Mathieu-Daudé April 12, 2023, 9:54 a.m. UTC | #1
On 11/4/23 21:04, Klaus Jensen wrote:
> From: Klaus Jensen <k.jensen@samsung.com>
> 
> The iocb (and the allocated memory to hold LBA ranges) leaks if reading
> the LBA ranges fails.
> 
> Fix this by adding a free and an unref of the iocb.
> 
> Reported-by: Coverity (CID 1508281)
> Fixes: d7d1474fd85d ("hw/nvme: reimplement dsm to allow cancellation")
> Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
> ---
>   hw/nvme/ctrl.c | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
> index 8b7be1420912..ac24eeb5ed5a 100644
> --- a/hw/nvme/ctrl.c
> +++ b/hw/nvme/ctrl.c
> @@ -2619,6 +2619,9 @@ static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *req)
>           status = nvme_h2c(n, (uint8_t *)iocb->range, sizeof(NvmeDsmRange) * nr,
>                             req);
>           if (status) {
> +            g_free(iocb->range);
> +            qemu_aio_unref(iocb);

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>

I note the qemu_aio_FOO() functions are not documented.

> +
>               return status;
>           }
>
Klaus Jensen April 12, 2023, 10:02 a.m. UTC | #2
On Apr 12 11:54, Philippe Mathieu-Daudé wrote:
> On 11/4/23 21:04, Klaus Jensen wrote:
> > From: Klaus Jensen <k.jensen@samsung.com>
> > 
> > The iocb (and the allocated memory to hold LBA ranges) leaks if reading
> > the LBA ranges fails.
> > 
> > Fix this by adding a free and an unref of the iocb.
> > 
> > Reported-by: Coverity (CID 1508281)
> > Fixes: d7d1474fd85d ("hw/nvme: reimplement dsm to allow cancellation")
> > Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
> > ---
> >   hw/nvme/ctrl.c | 3 +++
> >   1 file changed, 3 insertions(+)
> > 
> > diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
> > index 8b7be1420912..ac24eeb5ed5a 100644
> > --- a/hw/nvme/ctrl.c
> > +++ b/hw/nvme/ctrl.c
> > @@ -2619,6 +2619,9 @@ static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *req)
> >           status = nvme_h2c(n, (uint8_t *)iocb->range, sizeof(NvmeDsmRange) * nr,
> >                             req);
> >           if (status) {
> > +            g_free(iocb->range);
> > +            qemu_aio_unref(iocb);
> 
> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> 
> I note the qemu_aio_FOO() functions are not documented.
> 

As-in, "you are not supposed to use them" or "this should be documented
at some point"?

Thanks for your reviews Philippe, you're a life-saver :)
diff mbox series

Patch

diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 8b7be1420912..ac24eeb5ed5a 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -2619,6 +2619,9 @@  static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *req)
         status = nvme_h2c(n, (uint8_t *)iocb->range, sizeof(NvmeDsmRange) * nr,
                           req);
         if (status) {
+            g_free(iocb->range);
+            qemu_aio_unref(iocb);
+
             return status;
         }