util: xml: Add wrapper for 'xmlNewNode'

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@redhat.com>
Reviewed-by: Laine Stump <laine@redhat.com>
This commit is contained in:
Peter Krempa 2021-02-24 09:35:05 +01:00
parent 2b0f2a0a07
commit ec6e2a2c97
6 changed files with 30 additions and 34 deletions

View File

@ -30455,11 +30455,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();

View File

@ -3535,6 +3535,7 @@ virXMLBufferCreate;
virXMLCheckIllegalChars;
virXMLExtractNamespaceXML;
virXMLFormatElement;
virXMLNewNode;
virXMLNodeContentString;
virXMLNodeNameEqual;
virXMLNodeSanitizeNamespaces;

View File

@ -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;
}

View File

@ -289,3 +289,7 @@ int virParseScaledValue(const char *xpath,
xmlBufferPtr
virXMLBufferCreate(void);
xmlNodePtr
virXMLNewNode(xmlNsPtr ns,
const char *name);

View File

@ -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",

View File

@ -12559,10 +12559,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);