[libvirt] [PATCH] Fix memory leak issues in virshAllocpagesPagesizeCompleter.

Roland Schulz posted 1 patch 5 years, 11 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20180523124041.15108-2-schullzroll@gmail.com
Test syntax-check passed
tools/virsh-completer.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[libvirt] [PATCH] Fix memory leak issues in virshAllocpagesPagesizeCompleter.
Posted by Roland Schulz 5 years, 11 months ago
Signed-off-by: Roland Schulz <schullzroll@gmail.com>
---
 tools/virsh-completer.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
index 1435d1d4c..c72f3bcad 100644
--- a/tools/virsh-completer.c
+++ b/tools/virsh-completer.c
@@ -576,6 +576,7 @@ virshAllocpagesPagesizeCompleter(vshControl *ctl,
     virshControlPtr priv = ctl->privData;
     unsigned int npages = 0;
     xmlNodePtr *pages = NULL;
+    xmlDocPtr doc = NULL;
     double size = 0;
     size_t i = 0;
     const char *suffix = NULL;
@@ -595,7 +596,7 @@ virshAllocpagesPagesizeCompleter(vshControl *ctl,
     if (!(cap_xml = virConnectGetCapabilities(priv->conn)))
         goto error;
 
-    if (!(virXMLParseStringCtxt(cap_xml, _("capabilities"), &ctxt)))
+    if ((doc = virXMLParseStringCtxt(cap_xml, _("capabilities"), &ctxt)) == NULL)
         goto error;
 
     if (cellno && vshCommandOptStringQuiet(ctl, cmd, "cellno", &cellnum) > 0) {
@@ -631,12 +632,11 @@ virshAllocpagesPagesizeCompleter(vshControl *ctl,
 
  cleanup:
     xmlXPathFreeContext(ctxt);
-    for (i = 0; i < npages; i++)
-        VIR_FREE(pages[i]);
     VIR_FREE(pages);
-    VIR_FREE(cap_xml);
+    xmlFreeDoc(doc);
     VIR_FREE(path);
     VIR_FREE(pagesize);
+    VIR_FREE(cap_xml);
     VIR_FREE(unit);
 
     return ret;
-- 
2.17.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] Fix memory leak issues in virshAllocpagesPagesizeCompleter.
Posted by Michal Privoznik 5 years, 11 months ago
On 05/23/2018 02:40 PM, Roland Schulz wrote:
> Signed-off-by: Roland Schulz <schullzroll@gmail.com>
> ---
>  tools/virsh-completer.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
> index 1435d1d4c..c72f3bcad 100644
> --- a/tools/virsh-completer.c
> +++ b/tools/virsh-completer.c
> @@ -576,6 +576,7 @@ virshAllocpagesPagesizeCompleter(vshControl *ctl,
>      virshControlPtr priv = ctl->privData;
>      unsigned int npages = 0;
>      xmlNodePtr *pages = NULL;
> +    xmlDocPtr doc = NULL;
>      double size = 0;
>      size_t i = 0;
>      const char *suffix = NULL;
> @@ -595,7 +596,7 @@ virshAllocpagesPagesizeCompleter(vshControl *ctl,
>      if (!(cap_xml = virConnectGetCapabilities(priv->conn)))
>          goto error;
>  
> -    if (!(virXMLParseStringCtxt(cap_xml, _("capabilities"), &ctxt)))
> +    if ((doc = virXMLParseStringCtxt(cap_xml, _("capabilities"), &ctxt)) == NULL)
>          goto error;

You can keep the !ptr comparison just like @cap_xml is tested a few
lines above.

>  
>      if (cellno && vshCommandOptStringQuiet(ctl, cmd, "cellno", &cellnum) > 0) {
> @@ -631,12 +632,11 @@ virshAllocpagesPagesizeCompleter(vshControl *ctl,
>  
>   cleanup:
>      xmlXPathFreeContext(ctxt);
> -    for (i = 0; i < npages; i++)
> -        VIR_FREE(pages[i]);
>      VIR_FREE(pages);
> -    VIR_FREE(cap_xml);
> +    xmlFreeDoc(doc);
>      VIR_FREE(path);
>      VIR_FREE(pagesize);
> +    VIR_FREE(cap_xml);
>      VIR_FREE(unit);
>  
>      return ret;
> 

ACKed and pushed. Thank you for addressing all the points I raised.

Michal

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list