Move the variable holding the snapshot definition into the loop and use automatic clearing for it. Adjust the code for parity. Note that the clearing of 'snapdef' on success of 'virDomainSnapshotAssignDef' will be refactored in upcoming patches. Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx> --- src/qemu/qemu_driver.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 65ac5ef367..b3588f9478 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -343,7 +343,6 @@ qemuDomainSnapshotLoad(virDomainObj *vm, g_autofree char *snapDir = NULL; g_autoptr(DIR) dir = NULL; struct dirent *entry; - virDomainSnapshotDef *def = NULL; virDomainMomentObj *snap = NULL; virDomainMomentObj *current = NULL; bool cur; @@ -367,6 +366,7 @@ qemuDomainSnapshotLoad(virDomainObj *vm, goto cleanup; while ((direrr = virDirRead(dir, &entry, NULL)) > 0) { + g_autoptr(virDomainSnapshotDef) snapdef = NULL; g_autofree char *xmlStr = NULL; g_autofree char *fullpath = NULL; @@ -384,11 +384,11 @@ qemuDomainSnapshotLoad(virDomainObj *vm, continue; } - def = virDomainSnapshotDefParseString(xmlStr, - qemu_driver->xmlopt, - priv->qemuCaps, &cur, - flags); - if (def == NULL) { + snapdef = virDomainSnapshotDefParseString(xmlStr, + qemu_driver->xmlopt, + priv->qemuCaps, &cur, + flags); + if (snapdef == NULL) { /* Nothing we can do here, skip this one */ virReportError(VIR_ERR_INTERNAL_ERROR, _("Failed to parse snapshot XML from file '%s'"), @@ -396,10 +396,10 @@ qemuDomainSnapshotLoad(virDomainObj *vm, continue; } - snap = virDomainSnapshotAssignDef(vm->snapshots, def); - if (snap == NULL) { - virObjectUnref(def); - } else if (cur) { + snap = virDomainSnapshotAssignDef(vm->snapshots, snapdef); + if (snap) + snapdef = NULL; + if (cur && snap) { if (current) virReportError(VIR_ERR_INTERNAL_ERROR, _("Too many snapshots claiming to be current for domain %s"), -- 2.31.1