'xmlBufferCreate' returns NULL only on allocation failure. Add a wrapper
which will call 'abort()' in such case in a centralised spot. It doesn't
make much sense to continue execution from here.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/conf/domain_conf.c | 5 +----
src/conf/network_conf.c | 5 +----
src/libvirt_private.syms | 1 +
src/util/virxml.c | 19 +++++++++++++------
src/util/virxml.h | 3 +++
src/vmx/vmx.c | 5 ++---
6 files changed, 21 insertions(+), 17 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 46620d38ed..7a3374b5be 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -28692,10 +28692,7 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
* Thankfully, libxml maps what looks like globals into
* thread-local uses, so we are thread-safe. */
xmlIndentTreeOutput = 1;
- if (!(xmlbuf = xmlBufferCreate())) {
- virReportOOMError();
- return -1;
- }
+ xmlbuf = virXMLBufferCreate();
if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata,
virBufferGetIndent(buf) / 2, 1) < 0) {
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index f32710b781..69d99a60e0 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -2513,10 +2513,7 @@ virNetworkDefFormatBuf(virBufferPtr buf,
* Thankfully, libxml maps what looks like globals into
* thread-local uses, so we are thread-safe. */
xmlIndentTreeOutput = 1;
- if (!(xmlbuf = xmlBufferCreate())) {
- virReportOOMError();
- return -1;
- }
+ xmlbuf = virXMLBufferCreate();
if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata,
virBufferGetIndent(buf) / 2, 1) < 0) {
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 6b7261b987..dd54550b60 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3529,6 +3529,7 @@ virVsockSetGuestCid;
# util/virxml.h
virParseScaledValue;
+virXMLBufferCreate;
virXMLCheckIllegalChars;
virXMLExtractNamespaceXML;
virXMLFormatElement;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 0354251941..3fed2b2a6e 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -941,12 +941,7 @@ char *
virXMLNodeToString(xmlDocPtr doc,
xmlNodePtr node)
{
- g_autoptr(xmlBuffer) xmlbuf = NULL;
-
- if (!(xmlbuf = xmlBufferCreate())) {
- virReportOOMError();
- return NULL;
- }
+ g_autoptr(xmlBuffer) xmlbuf = virXMLBufferCreate();
if (xmlNodeDump(xmlbuf, doc, node, 0, 1) == 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -1467,3 +1462,15 @@ virParseScaledValue(const char *xpath,
*val = bytes;
return 1;
}
+
+
+xmlBufferPtr
+virXMLBufferCreate(void)
+{
+ xmlBufferPtr ret;
+
+ if (!(ret = xmlBufferCreate()))
+ abort();
+
+ return ret;
+}
diff --git a/src/util/virxml.h b/src/util/virxml.h
index e696dd25f5..24a2234506 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -286,3 +286,6 @@ int virParseScaledValue(const char *xpath,
unsigned long long scale,
unsigned long long max,
bool required);
+
+xmlBufferPtr
+virXMLBufferCreate(void);
diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c
index db535ba260..e6c0900a65 100644
--- a/src/vmx/vmx.c
+++ b/src/vmx/vmx.c
@@ -771,7 +771,7 @@ virVMXConvertToUTF8(const char *encoding, const char *string)
char *result = NULL;
xmlCharEncodingHandlerPtr handler;
g_autoptr(xmlBuffer) input = NULL;
- g_autoptr(xmlBuffer) utf8 = NULL;
+ g_autoptr(xmlBuffer) utf8 = virXMLBufferCreate();
handler = xmlFindCharEncodingHandler(encoding);
@@ -781,8 +781,7 @@ virVMXConvertToUTF8(const char *encoding, const char *string)
return NULL;
}
- if (!(input = xmlBufferCreateStatic((char *)string, strlen(string))) ||
- !(utf8 = xmlBufferCreate())) {
+ if (!(input = xmlBufferCreateStatic((char *)string, strlen(string)))) {
virReportOOMError();
goto cleanup;
}
--
2.29.2