[libvirt] [PATCH] virsh.c: avoid leak on OOM error path

Jim Meyering jim at meyering.net
Fri Feb 19 17:18:40 UTC 2010


Jim Meyering wrote:

> No one really cares if we leak memory while dying, but who knows...
> freeing that buffer may let us go down more gracefully.
>
> FYI, the leak is triggered when virFileReadAll succeeds
> (it allocates "buffer"), yet xmlNewDoc fails:
>
>     if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
>         return FALSE;
>
>     doc = xmlNewDoc(NULL);
>     if (doc == NULL)
>         goto no_memory;
>
>
>>From 03c7a44e3d5b2e7c992bebc98fc8c6a7bf63881e Mon Sep 17 00:00:00 2001
> From: Jim Meyering <meyering at redhat.com>
> Date: Fri, 19 Feb 2010 18:03:41 +0100
> Subject: [PATCH] virsh.c: avoid leak on OOM error path
>
> * tools/virsh.c (cmdCPUBaseline): Also free "buffer" upon OOM.
> ---
>  tools/virsh.c |    1 +
>  1 files changed, 1 insertions(+), 0 deletions(-)
>
> diff --git a/tools/virsh.c b/tools/virsh.c
> index dd916f3..8756a7a 100644
> --- a/tools/virsh.c
> +++ b/tools/virsh.c
> @@ -7139,6 +7139,7 @@ cleanup:
>      return ret;
>
>  no_memory:
> +    VIR_FREE(buffer);
>      vshError(ctl, "%s", _("Out of memory"));
>      ret = FALSE;
>      return ret;
> --

The above is correct, but there's another leak in the same function,
so I've amended the patch to also free the "list" buffer.
"list" is allocated in the for-loop.
If on the 2nd or subsequent iteration of that loop we take
the "goto no_memory", we'd leak that buffer.

>From 3d97412799c1b5bfedc647059c7d3b18e763f0bc Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Fri, 19 Feb 2010 18:03:41 +0100
Subject: [PATCH] virsh.c: avoid leak on OOM error path

* tools/virsh.c (cmdCPUBaseline): Also free "buffer" and "list" upon OOM.
---
 tools/virsh.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index dd916f3..c8ae9f2 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -7139,6 +7139,8 @@ cleanup:
     return ret;

 no_memory:
+    VIR_FREE(list);
+    VIR_FREE(buffer);
     vshError(ctl, "%s", _("Out of memory"));
     ret = FALSE;
     return ret;
--
1.7.0.233.g05e1a




More information about the libvir-list mailing list