use g_autoptr for all xmlBuffers

AUTOPTR_CLEANUP_FUNC is set to xmlBufferFree() in util/virxml.h (This
is actually new - added accidentally (but fortunately harmlessly!) in
commit 257aba2daf. I had added it along with the hunks in this patch,
then decided to remove it and submit separately, but missed taking out
the hunk in virxml.h)

Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Laine Stump 2020-06-18 19:16:33 -04:00
parent b7a92bce07
commit a726feb693
4 changed files with 8 additions and 22 deletions

View File

@ -29579,7 +29579,7 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
def->description); def->description);
if (def->metadata) { if (def->metadata) {
xmlBufferPtr xmlbuf; g_autoptr(xmlBuffer) xmlbuf = NULL;
int oldIndentTreeOutput = xmlIndentTreeOutput; int oldIndentTreeOutput = xmlIndentTreeOutput;
/* Indentation on output requires that we previously set /* Indentation on output requires that we previously set
@ -29596,12 +29596,10 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata, if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata,
virBufferGetIndent(buf) / 2, 1) < 0) { virBufferGetIndent(buf) / 2, 1) < 0) {
xmlBufferFree(xmlbuf);
xmlIndentTreeOutput = oldIndentTreeOutput; xmlIndentTreeOutput = oldIndentTreeOutput;
goto error; goto error;
} }
virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf)); virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf));
xmlBufferFree(xmlbuf);
xmlIndentTreeOutput = oldIndentTreeOutput; xmlIndentTreeOutput = oldIndentTreeOutput;
} }

View File

@ -2508,7 +2508,7 @@ virNetworkDefFormatBuf(virBufferPtr buf,
virBufferAsprintf(buf, "<uuid>%s</uuid>\n", uuidstr); virBufferAsprintf(buf, "<uuid>%s</uuid>\n", uuidstr);
if (def->metadata) { if (def->metadata) {
xmlBufferPtr xmlbuf; g_autoptr(xmlBuffer) xmlbuf = NULL;
int oldIndentTreeOutput = xmlIndentTreeOutput; int oldIndentTreeOutput = xmlIndentTreeOutput;
/* Indentation on output requires that we previously set /* Indentation on output requires that we previously set
@ -2525,12 +2525,10 @@ virNetworkDefFormatBuf(virBufferPtr buf,
if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata, if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata,
virBufferGetIndent(buf) / 2, 1) < 0) { virBufferGetIndent(buf) / 2, 1) < 0) {
xmlBufferFree(xmlbuf);
xmlIndentTreeOutput = oldIndentTreeOutput; xmlIndentTreeOutput = oldIndentTreeOutput;
return -1; return -1;
} }
virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf)); virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf));
xmlBufferFree(xmlbuf);
xmlIndentTreeOutput = oldIndentTreeOutput; xmlIndentTreeOutput = oldIndentTreeOutput;
} }

View File

@ -953,8 +953,7 @@ char *
virXMLNodeToString(xmlDocPtr doc, virXMLNodeToString(xmlDocPtr doc,
xmlNodePtr node) xmlNodePtr node)
{ {
xmlBufferPtr xmlbuf = NULL; g_autoptr(xmlBuffer) xmlbuf = NULL;
char *ret = NULL;
if (!(xmlbuf = xmlBufferCreate())) { if (!(xmlbuf = xmlBufferCreate())) {
virReportOOMError(); virReportOOMError();
@ -964,15 +963,10 @@ virXMLNodeToString(xmlDocPtr doc,
if (xmlNodeDump(xmlbuf, doc, node, 0, 1) == 0) { if (xmlNodeDump(xmlbuf, doc, node, 0, 1) == 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to convert the XML node tree")); _("failed to convert the XML node tree"));
goto cleanup; return NULL;
} }
ret = g_strdup((const char *)xmlBufferContent(xmlbuf)); return g_strdup((const char *)xmlBufferContent(xmlbuf));
cleanup:
xmlBufferFree(xmlbuf);
return ret;
} }

View File

@ -697,8 +697,8 @@ virVMXConvertToUTF8(const char *encoding, const char *string)
{ {
char *result = NULL; char *result = NULL;
xmlCharEncodingHandlerPtr handler; xmlCharEncodingHandlerPtr handler;
xmlBufferPtr input = NULL; g_autoptr(xmlBuffer) input = NULL;
xmlBufferPtr utf8 = NULL; g_autoptr(xmlBuffer) utf8 = NULL;
handler = xmlFindCharEncodingHandler(encoding); handler = xmlFindCharEncodingHandler(encoding);
@ -720,14 +720,10 @@ virVMXConvertToUTF8(const char *encoding, const char *string)
goto cleanup; goto cleanup;
} }
result = (char *)utf8->content; result = (char *)g_steal_pointer(&utf8->content);
utf8->content = NULL;
cleanup: cleanup:
xmlCharEncCloseFunc(handler); xmlCharEncCloseFunc(handler);
xmlBufferFree(input);
xmlBufferFree(utf8);
return result; return result;
} }