conf: Add error checking to virDomainSnapshotDiskDefFormat

Commit id '43f2ccdc' called virDomainDiskSourceDefFormatInternal
rather than formatting the the disk source inline. However, it
did not handle the case where the helper failed. Over time the
helper has been renamed to virDomainDiskSourceFormat. Similar to
other consumers, if virDomainDiskSourceFormat fails, then the
formatting could be off, so it's better to fail than to continue
on with some possibly bad data. Alter the function and the caller
to check status and jump to error in that case.

Found by Coverity

Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
John Ferlan 2017-11-08 07:13:20 -05:00
parent fb1fae94a1
commit c028c71930

View File

@ -662,7 +662,7 @@ virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr def,
return ret; return ret;
} }
static void static int
virDomainSnapshotDiskDefFormat(virBufferPtr buf, virDomainSnapshotDiskDefFormat(virBufferPtr buf,
virDomainSnapshotDiskDefPtr disk, virDomainSnapshotDiskDefPtr disk,
virDomainXMLOptionPtr xmlopt) virDomainXMLOptionPtr xmlopt)
@ -670,7 +670,7 @@ virDomainSnapshotDiskDefFormat(virBufferPtr buf,
int type = disk->src->type; int type = disk->src->type;
if (!disk->name) if (!disk->name)
return; return 0;
virBufferEscapeString(buf, "<disk name='%s'", disk->name); virBufferEscapeString(buf, "<disk name='%s'", disk->name);
if (disk->snapshot > 0) if (disk->snapshot > 0)
@ -679,7 +679,7 @@ virDomainSnapshotDiskDefFormat(virBufferPtr buf,
if (!disk->src->path && disk->src->format == 0) { if (!disk->src->path && disk->src->format == 0) {
virBufferAddLit(buf, "/>\n"); virBufferAddLit(buf, "/>\n");
return; return 0;
} }
virBufferAsprintf(buf, " type='%s'>\n", virStorageTypeToString(type)); virBufferAsprintf(buf, " type='%s'>\n", virStorageTypeToString(type));
@ -688,10 +688,12 @@ virDomainSnapshotDiskDefFormat(virBufferPtr buf,
if (disk->src->format > 0) if (disk->src->format > 0)
virBufferEscapeString(buf, "<driver type='%s'/>\n", virBufferEscapeString(buf, "<driver type='%s'/>\n",
virStorageFileFormatTypeToString(disk->src->format)); virStorageFileFormatTypeToString(disk->src->format));
virDomainDiskSourceFormat(buf, disk->src, 0, 0, xmlopt); if (virDomainDiskSourceFormat(buf, disk->src, 0, 0, xmlopt) < 0)
return -1;
virBufferAdjustIndent(buf, -2); virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</disk>\n"); virBufferAddLit(buf, "</disk>\n");
return 0;
} }
@ -741,8 +743,10 @@ virDomainSnapshotDefFormat(const char *domain_uuid,
if (def->ndisks) { if (def->ndisks) {
virBufferAddLit(&buf, "<disks>\n"); virBufferAddLit(&buf, "<disks>\n");
virBufferAdjustIndent(&buf, 2); virBufferAdjustIndent(&buf, 2);
for (i = 0; i < def->ndisks; i++) for (i = 0; i < def->ndisks; i++) {
virDomainSnapshotDiskDefFormat(&buf, &def->disks[i], xmlopt); if (virDomainSnapshotDiskDefFormat(&buf, &def->disks[i], xmlopt) < 0)
goto error;
}
virBufferAdjustIndent(&buf, -2); virBufferAdjustIndent(&buf, -2);
virBufferAddLit(&buf, "</disks>\n"); virBufferAddLit(&buf, "</disks>\n");
} }