[PATCH 3/5] util: log an error if virXMLNodeContentString will return NULL

Laine Stump laine at redhat.com
Mon Jul 20 20:48:41 UTC 2020


Many of our calls to xmlNodeGetContent() (which are now all via
virXMLNodeContentString() are failing to check for a NULL return. We
need to remedy that, but in order to make the remedy simpler, let's
log an error in virXMLNodeContentString(), so that the callers don't
all individually need to (since it would be the same error message for
all of them anyway).

Signed-off-by: Laine Stump <laine at redhat.com>
---
 src/util/virxml.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/util/virxml.c b/src/util/virxml.c
index 27d22598ee..5315d4ff6f 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -538,7 +538,23 @@ virXMLPropStringLimit(xmlNodePtr node,
 char *
 virXMLNodeContentString(xmlNodePtr node)
 {
-    return (char *)xmlNodeGetContent(node);
+    char *ret = (char *)xmlNodeGetContent(node);
+
+    if (node->type !=  XML_ELEMENT_NODE) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("node '%s' has unexpected type %d"),
+                       node->name, node->type);
+        return NULL;
+    }
+
+    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"),
+                       node->name);
+        return NULL;
+    }
+
+    return ret;
 }
 
 
-- 
2.25.4




More information about the libvir-list mailing list