[PATCH 19/33] util: xml: Add wrapper for 'xmlNewNode'

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add a wrapper that will handle the out of memory condition by abort()
and also prevents callers from having to typecast the argument.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/conf/domain_conf.c        |  7 ++-----
 src/libvirt_private.syms      |  1 +
 src/util/virxml.c             | 13 +++++++++++++
 src/util/virxml.h             |  4 ++++
 src/vbox/vbox_snapshot_conf.c | 34 +++++++++-------------------------
 tools/virsh-domain.c          |  5 +----
 6 files changed, 30 insertions(+), 34 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 7a3374b5be..c0881608af 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -30456,11 +30456,8 @@ virDomainDefSetMetadata(virDomainDefPtr def,
                 return -1;

             /* create the root node if needed */
-            if (!def->metadata &&
-                !(def->metadata = xmlNewNode(NULL, (unsigned char *)"metadata"))) {
-                virReportOOMError();
-                return -1;
-            }
+            if (!def->metadata)
+                def->metadata = virXMLNewNode(NULL, "metadata");

             if (!(new = xmlCopyNode(doc->children, 1))) {
                 virReportOOMError();
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index dd54550b60..48f66daab8 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3533,6 +3533,7 @@ virXMLBufferCreate;
 virXMLCheckIllegalChars;
 virXMLExtractNamespaceXML;
 virXMLFormatElement;
+virXMLNewNode;
 virXMLNodeContentString;
 virXMLNodeNameEqual;
 virXMLNodeSanitizeNamespaces;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 3fed2b2a6e..ebe479f5d3 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -1474,3 +1474,16 @@ virXMLBufferCreate(void)

     return ret;
 }
+
+
+xmlNodePtr
+virXMLNewNode(xmlNsPtr ns,
+              const char *name)
+{
+    xmlNodePtr ret;
+
+    if (!(ret = xmlNewNode(ns, BAD_CAST name)))
+        abort();
+
+    return ret;
+}
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 24a2234506..d32f77b867 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -289,3 +289,7 @@ int virParseScaledValue(const char *xpath,

 xmlBufferPtr
 virXMLBufferCreate(void);
+
+xmlNodePtr
+virXMLNewNode(xmlNsPtr ns,
+              const char *name);
diff --git a/src/vbox/vbox_snapshot_conf.c b/src/vbox/vbox_snapshot_conf.c
index f1cae3039a..5792d3175e 100644
--- a/src/vbox/vbox_snapshot_conf.c
+++ b/src/vbox/vbox_snapshot_conf.c
@@ -328,7 +328,7 @@ virVBoxSnapshotConfCreateHardDiskNode(virVBoxSnapshotConfHardDiskPtr hardDisk)
     int result = -1;
     size_t i = 0;
     char *uuid = NULL;
-    xmlNodePtr ret = xmlNewNode(NULL, BAD_CAST "HardDisk");
+    xmlNodePtr ret = virXMLNewNode(NULL, "HardDisk");
     uuid = g_strdup_printf("{%s}", hardDisk->uuid);

     if (xmlNewProp(ret, BAD_CAST "uuid", BAD_CAST uuid) == NULL)
@@ -404,7 +404,7 @@ virVBoxSnapshotConfSerializeSnapshot(xmlNodePtr node,

     /* node description */
     if (snapshot->description != NULL) {
-        descriptionNode = xmlNewNode(NULL, BAD_CAST "Description");
+        descriptionNode = virXMLNewNode(NULL, "Description");
         xmlNodeSetContent(descriptionNode, BAD_CAST snapshot->description);
         xmlAddChild(node, descriptionNode);
     }
@@ -433,10 +433,10 @@ virVBoxSnapshotConfSerializeSnapshot(xmlNodePtr node,
     xmlAddChild(node, storageControllerNode);

     if (snapshot->nchildren > 0) {
-        snapshotsNode = xmlNewNode(NULL, BAD_CAST "Snapshots");
+        snapshotsNode = virXMLNewNode(NULL, "Snapshots");
         xmlAddChild(node, snapshotsNode);
         for (i = 0; i < snapshot->nchildren; i++) {
-            xmlNodePtr child = xmlNewNode(NULL, BAD_CAST "Snapshot");
+            xmlNodePtr child = virXMLNewNode(NULL, "Snapshot");
             xmlAddChild(snapshotsNode, child);
             if (virVBoxSnapshotConfSerializeSnapshot(child, snapshot->children[i]) < 0)
                 goto cleanup;
@@ -1001,11 +1001,7 @@ virVBoxSnapshotConfSaveVboxFile(virVBoxSnapshotConfMachinePtr machine,
         goto cleanup;
     }

-    cur = xmlNewNode(NULL, BAD_CAST "VirtualBox");
-    if (!cur) {
-        virReportOOMError();
-        goto cleanup;
-    }
+    cur = virXMLNewNode(NULL, "VirtualBox");

     if (!xmlNewProp(cur, BAD_CAST "version", BAD_CAST "1.12-linux")) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
@@ -1038,11 +1034,7 @@ virVBoxSnapshotConfSaveVboxFile(virVBoxSnapshotConfMachinePtr machine,
         goto cleanup;
     }

-    machineNode = xmlNewNode(NULL, BAD_CAST "Machine");
-    if (!machineNode) {
-        virReportOOMError();
-        goto cleanup;
-    }
+    machineNode = virXMLNewNode(NULL, "Machine");

     if (!xmlNewProp(machineNode, BAD_CAST "uuid", BAD_CAST machine->uuid)) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
@@ -1101,11 +1093,7 @@ virVBoxSnapshotConfSaveVboxFile(virVBoxSnapshotConfMachinePtr machine,
     }
     xmlAddChild(xmlDocGetRootElement(xml), machineNode);

-    mediaRegistryNode = xmlNewNode(NULL, BAD_CAST "MediaRegistry");
-    if (!mediaRegistryNode) {
-        virReportOOMError();
-        goto cleanup;
-    }
+    mediaRegistryNode = virXMLNewNode(NULL, "MediaRegistry");

     xmlAddChild(machineNode, mediaRegistryNode);
     for (i = 0; i < machine->mediaRegistry->notherMedia; i++) {
@@ -1121,11 +1109,7 @@ virVBoxSnapshotConfSaveVboxFile(virVBoxSnapshotConfMachinePtr machine,
         }
         xmlAddChild(mediaRegistryNode, cur);
     }
-    hardDisksNode = xmlNewNode(NULL, BAD_CAST "HardDisks");
-    if (!hardDisksNode) {
-        virReportOOMError();
-        goto cleanup;
-    }
+    hardDisksNode = virXMLNewNode(NULL, "HardDisks");
     for (i = 0; i < machine->mediaRegistry->ndisks; i++) {
         xmlNodePtr child = virVBoxSnapshotConfCreateHardDiskNode(machine->mediaRegistry->disks[i]);
         if (child != NULL)
@@ -1172,7 +1156,7 @@ virVBoxSnapshotConfSaveVboxFile(virVBoxSnapshotConfMachinePtr machine,
     xmlAddChild(machineNode, cur);

     if (machine->snapshot != NULL) {
-        snapshotNode = xmlNewNode(NULL, BAD_CAST "Snapshot");
+        snapshotNode = virXMLNewNode(NULL, "Snapshot");
         xmlAddChild(machineNode, snapshotNode);
         if (virVBoxSnapshotConfSerializeSnapshot(snapshotNode, machine->snapshot) < 0) {
             virReportError(VIR_ERR_XML_ERROR, "%s",
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index df33467646..16e0c45f80 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -12566,10 +12566,7 @@ virshUpdateDiskXML(xmlNodePtr disk_node,

     if (new_source) {
         /* create new source subelement */
-        if (!(source = xmlNewNode(NULL, BAD_CAST "source"))) {
-            vshError(NULL, _("Failed to allocate new source node"));
-            goto cleanup;
-        }
+        source = virXMLNewNode(NULL, "source");

         if (source_block)
             xmlNewProp(source, BAD_CAST "dev", BAD_CAST new_source);
-- 
2.29.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux