The only reason for the error label in this function is to call
virBufferFreeAndReset(). It's actually more common for a failed format
function to just leave the virBuffer alone and let the caller free it
when there is a failure, and in fact the only caller of this function
that *wasn't* already calling virBufferFreeAndReset() on failure was
virDomainDefFormat() (via virDomainDefFormatInternal()).
That is easily solved by modifying virDomainDefFormat() to declare its
virBuffer buf with g_auto(), so that virBufferFreeAndReset() is
automatically called.
Signed-off-by: Laine Stump <laine(a)redhat.com>
---
src/conf/domain_conf.c | 88 ++++++++++++++++++++----------------------
1 file changed, 42 insertions(+), 46 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 243590854f..0307ffcbd6 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -29534,7 +29534,7 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
if (!(type = virDomainVirtTypeToString(def->virtType))) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unexpected domain type %d"), def->virtType);
- goto error;
+ return -1;
}
if (def->id == -1)
@@ -29579,13 +29579,13 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
xmlIndentTreeOutput = 1;
if (!(xmlbuf = xmlBufferCreate())) {
virReportOOMError();
- goto error;
+ return -1;
}
if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata,
virBufferGetIndent(buf) / 2, 1) < 0) {
xmlIndentTreeOutput = oldIndentTreeOutput;
- goto error;
+ return -1;
}
virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf));
xmlIndentTreeOutput = oldIndentTreeOutput;
@@ -29608,13 +29608,13 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
def->mem.cur_balloon);
if (virDomainDefFormatBlkiotune(buf, def) < 0)
- goto error;
+ return -1;
virDomainMemtuneFormat(buf, &def->mem);
virDomainMemorybackingFormat(buf, &def->mem);
if (virDomainCpuDefFormat(buf, def) < 0)
- goto error;
+ return -1;
if (def->niothreadids > 0) {
virBufferAsprintf(buf, "<iothreads>%zu</iothreads>\n",
@@ -29632,10 +29632,10 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
}
if (virDomainCputuneDefFormat(buf, def, flags) < 0)
- goto error;
+ return -1;
if (virDomainNumatuneFormatXML(buf, def->numa) < 0)
- goto error;
+ return -1;
if (def->resource)
virDomainResourceDefFormat(buf, def->resource);
@@ -29720,7 +29720,7 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unexpected boot device type %d"),
def->os.bootDevs[n]);
- goto error;
+ return -1;
}
virBufferAsprintf(buf, "<boot dev='%s'/>\n",
boottype);
}
@@ -29752,7 +29752,7 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
if (mode == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unexpected smbios mode %d"),
def->os.smbios_mode);
- goto error;
+ return -1;
}
virBufferAsprintf(buf, "<smbios mode='%s'/>\n", mode);
}
@@ -29783,10 +29783,10 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
}
if (virDomainDefFormatFeatures(buf, def) < 0)
- goto error;
+ return -1;
if (virCPUDefFormatBufFull(buf, def->cpu, def->numa) < 0)
- goto error;
+ return -1;
virBufferAsprintf(buf, "<clock offset='%s'",
virDomainClockOffsetTypeToString(def->clock.offset));
@@ -29817,7 +29817,7 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
virBufferAdjustIndent(buf, 2);
for (n = 0; n < def->clock.ntimers; n++) {
if (virDomainTimerDefFormat(buf, def->clock.timers[n]) < 0)
- goto error;
+ return -1;
}
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</clock>\n");
@@ -29826,20 +29826,20 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
if (virDomainEventActionDefFormat(buf, def->onPoweroff,
"on_poweroff",
virDomainLifecycleActionTypeToString) < 0)
- goto error;
+ return -1;
if (virDomainEventActionDefFormat(buf, def->onReboot,
"on_reboot",
virDomainLifecycleActionTypeToString) < 0)
- goto error;
+ return -1;
if (virDomainEventActionDefFormat(buf, def->onCrash,
"on_crash",
virDomainLifecycleActionTypeToString) < 0)
- goto error;
+ return -1;
if (def->onLockFailure != VIR_DOMAIN_LOCK_FAILURE_DEFAULT &&
virDomainEventActionDefFormat(buf, def->onLockFailure,
"on_lockfailure",
virDomainLockFailureTypeToString) < 0)
- goto error;
+ return -1;
if (def->pm.s3 || def->pm.s4) {
virBufferAddLit(buf, "<pm>\n");
@@ -29866,35 +29866,35 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
for (n = 0; n < def->ndisks; n++)
if (virDomainDiskDefFormat(buf, def->disks[n], flags, xmlopt) < 0)
- goto error;
+ return -1;
for (n = 0; n < def->ncontrollers; n++)
if (virDomainControllerDefFormat(buf, def->controllers[n], flags) < 0)
- goto error;
+ return -1;
for (n = 0; n < def->nleases; n++)
if (virDomainLeaseDefFormat(buf, def->leases[n]) < 0)
- goto error;
+ return -1;
for (n = 0; n < def->nfss; n++)
if (virDomainFSDefFormat(buf, def->fss[n], flags) < 0)
- goto error;
+ return -1;
for (n = 0; n < def->nnets; n++)
if (virDomainNetDefFormat(buf, def->nets[n], xmlopt, flags) < 0)
- goto error;
+ return -1;
for (n = 0; n < def->nsmartcards; n++)
if (virDomainSmartcardDefFormat(buf, def->smartcards[n], flags) < 0)
- goto error;
+ return -1;
for (n = 0; n < def->nserials; n++)
if (virDomainChrDefFormat(buf, def->serials[n], flags) < 0)
- goto error;
+ return -1;
for (n = 0; n < def->nparallels; n++)
if (virDomainChrDefFormat(buf, def->parallels[n], flags) < 0)
- goto error;
+ return -1;
for (n = 0; n < def->nconsoles; n++) {
virDomainChrDef console;
@@ -29912,36 +29912,36 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
memcpy(&console, def->consoles[n], sizeof(console));
}
if (virDomainChrDefFormat(buf, &console, flags) < 0)
- goto error;
+ return -1;
}
for (n = 0; n < def->nchannels; n++)
if (virDomainChrDefFormat(buf, def->channels[n], flags) < 0)
- goto error;
+ return -1;
for (n = 0; n < def->ninputs; n++) {
if (virDomainInputDefFormat(buf, def->inputs[n], flags) < 0)
- goto error;
+ return -1;
}
for (n = 0; n < def->ntpms; n++) {
if (virDomainTPMDefFormat(buf, def->tpms[n], flags) < 0)
- goto error;
+ return -1;
}
for (n = 0; n < def->ngraphics; n++) {
if (virDomainGraphicsDefFormat(buf, def->graphics[n], flags) < 0)
- goto error;
+ return -1;
}
for (n = 0; n < def->nsounds; n++) {
if (virDomainSoundDefFormat(buf, def->sounds[n], flags) < 0)
- goto error;
+ return -1;
}
for (n = 0; n < def->nvideos; n++) {
if (virDomainVideoDefFormat(buf, def->videos[n], flags) < 0)
- goto error;
+ return -1;
}
for (n = 0; n < def->nhostdevs; n++) {
@@ -29951,13 +29951,13 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
*/
if (!def->hostdevs[n]->parentnet &&
virDomainHostdevDefFormat(buf, def->hostdevs[n], flags) < 0) {
- goto error;
+ return -1;
}
}
for (n = 0; n < def->nredirdevs; n++) {
if (virDomainRedirdevDefFormat(buf, def->redirdevs[n], flags) < 0)
- goto error;
+ return -1;
}
if (def->redirfilter)
@@ -29965,7 +29965,7 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
for (n = 0; n < def->nhubs; n++) {
if (virDomainHubDefFormat(buf, def->hubs[n], flags) < 0)
- goto error;
+ return -1;
}
if (def->watchdog)
@@ -29976,7 +29976,7 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
for (n = 0; n < def->nrngs; n++) {
if (virDomainRNGDefFormat(buf, def->rngs[n], flags))
- goto error;
+ return -1;
}
if (def->nvram)
@@ -29984,26 +29984,26 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
for (n = 0; n < def->npanics; n++) {
if (virDomainPanicDefFormat(buf, def->panics[n]) < 0)
- goto error;
+ return -1;
}
for (n = 0; n < def->nshmems; n++) {
if (virDomainShmemDefFormat(buf, def->shmems[n], flags) < 0)
- goto error;
+ return -1;
}
for (n = 0; n < def->nmems; n++) {
if (virDomainMemoryDefFormat(buf, def->mems[n], def, flags) < 0)
- goto error;
+ return -1;
}
if (def->iommu &&
virDomainIOMMUDefFormat(buf, def->iommu) < 0)
- goto error;
+ return -1;
if (def->vsock &&
virDomainVsockDefFormat(buf, def->vsock) < 0)
- goto error;
+ return -1;
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</devices>\n");
@@ -30018,17 +30018,13 @@ virDomainDefFormatInternalSetRootName(virDomainDefPtr def,
if (def->namespaceData && def->ns.format) {
if ((def->ns.format)(buf, def->namespaceData) < 0)
- goto error;
+ return -1;
}
virBufferAdjustIndent(buf, -2);
virBufferAsprintf(buf, "</%s>\n", rootname);
return 0;
-
- error:
- virBufferFreeAndReset(buf);
- return -1;
}
/* Converts VIR_DOMAIN_XML_COMMON_FLAGS into VIR_DOMAIN_DEF_FORMAT_*
@@ -30056,7 +30052,7 @@ virDomainDefFormat(virDomainDefPtr def,
virDomainXMLOptionPtr xmlopt,
unsigned int flags)
{
- virBuffer buf = VIR_BUFFER_INITIALIZER;
+ g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
virCheckFlags(VIR_DOMAIN_DEF_FORMAT_COMMON_FLAGS, NULL);
if (virDomainDefFormatInternal(def, xmlopt, &buf, flags) < 0)
--
2.25.4