[PATCH] virxml: Fix possible memory leak in virXMLNodeContentString()

Kristina Hanicova posted 1 patch 3 years ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/10f9b1b29c7d67fe103c335ee876c41d9d9f09e0.1615999363.git.khanicov@redhat.com
src/util/virxml.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] virxml: Fix possible memory leak in virXMLNodeContentString()
Posted by Kristina Hanicova 3 years ago
Previously, if xml node passed to the virXMLNodeContentString()
was not of type XML_ELEMENT_NODE, @ret could have caused a memory
leak because xmlNodeGetContent() works for other types of nodes
as well.

Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
---
 src/util/virxml.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/util/virxml.c b/src/util/virxml.c
index 060b7530fc..4a6fe09468 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -536,7 +536,7 @@ virXMLPropStringLimit(xmlNodePtr node,
 char *
 virXMLNodeContentString(xmlNodePtr node)
 {
-    char *ret = (char *)xmlNodeGetContent(node);
+    char *ret = NULL;
 
     if (node->type !=  XML_ELEMENT_NODE) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -545,6 +545,8 @@ virXMLNodeContentString(xmlNodePtr node)
         return NULL;
     }
 
+    ret = (char *)xmlNodeGetContent(node);
+
     if (!ret) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("node '%s' has unexpected NULL content. This could be caused by malformed input, or a memory allocation failure"),
-- 
2.29.2

Re: [PATCH] virxml: Fix possible memory leak in virXMLNodeContentString()
Posted by Michal Privoznik 3 years ago
On 3/17/21 6:01 PM, Kristina Hanicova wrote:
> Previously, if xml node passed to the virXMLNodeContentString()
> was not of type XML_ELEMENT_NODE, @ret could have caused a memory
> leak because xmlNodeGetContent() works for other types of nodes
> as well.
> 
> Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
> ---
>   src/util/virxml.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

Nice catch! Pushed.

Michal