Creating a copy of the definition we want to add in a migration cookie
makes the code cleaner and less prone to memory leaks or double free
errors.
Signed-off-by: Jiri Denemark <jdenemar(a)redhat.com>
---
src/qemu/qemu_migration.c | 18 ++++++++----------
src/qemu/qemu_migration_cookie.c | 8 +++++---
src/qemu/qemu_migration_cookie.h | 2 +-
3 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index b1d141755..9e0e63cf0 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -3291,11 +3291,13 @@ qemuMigrationRun(virQEMUDriverPtr driver,
if (persist_xml) {
persistDef = qemuMigrationPrepareDef(driver, persist_xml,
NULL, NULL);
- if (!persistDef)
- goto cleanup;
} else {
- persistDef = vm->newDef;
+ persistDef = qemuDomainDefCopy(driver, vm->newDef,
+ VIR_DOMAIN_XML_SECURE |
+ VIR_DOMAIN_XML_MIGRATABLE);
}
+ if (!persistDef)
+ goto cleanup;
}
mig = qemuMigrationEatCookie(driver, vm, cookiein, cookieinlen,
@@ -3505,14 +3507,13 @@ qemuMigrationRun(virQEMUDriverPtr driver,
QEMU_MIGRATION_COOKIE_STATS;
if (ret == 0 &&
- (qemuMigrationCookieAddPersistent(mig, persistDef) < 0 ||
+ (qemuMigrationCookieAddPersistent(mig, &persistDef) < 0 ||
qemuMigrationBakeCookie(mig, driver, vm, cookieout,
cookieoutlen, cookieFlags) < 0)) {
VIR_WARN("Unable to encode migration cookie");
}
- if (persistDef != vm->newDef)
- virDomainDefFree(persistDef);
+ virDomainDefFree(persistDef);
qemuMigrationCookieFree(mig);
if (events)
@@ -4997,10 +4998,7 @@ qemuMigrationFinish(virQEMUDriverPtr driver,
qemuMonitorSetDomainLog(priv->mon, NULL, NULL, NULL);
VIR_FREE(priv->origname);
virDomainObjEndAPI(&vm);
- if (mig) {
- virDomainDefFree(qemuMigrationCookieGetPersistent(mig));
- qemuMigrationCookieFree(mig);
- }
+ qemuMigrationCookieFree(mig);
if (orig_err) {
virSetError(orig_err);
virFreeError(orig_err);
diff --git a/src/qemu/qemu_migration_cookie.c b/src/qemu/qemu_migration_cookie.c
index 0f4bcaddc..bd12f1124 100644
--- a/src/qemu/qemu_migration_cookie.c
+++ b/src/qemu/qemu_migration_cookie.c
@@ -99,6 +99,7 @@ qemuMigrationCookieFree(qemuMigrationCookiePtr mig)
return;
qemuMigrationCookieGraphicsFree(mig->graphics);
+ virDomainDefFree(mig->persistent);
qemuMigrationCookieNetworkFree(mig->network);
qemuMigrationCookieNBDFree(mig->nbd);
@@ -385,7 +386,7 @@ qemuMigrationCookieAddLockstate(qemuMigrationCookiePtr mig,
int
qemuMigrationCookieAddPersistent(qemuMigrationCookiePtr mig,
- virDomainDefPtr def)
+ virDomainDefPtr *def)
{
if (mig->flags & QEMU_MIGRATION_COOKIE_PERSISTENT) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -393,10 +394,11 @@ qemuMigrationCookieAddPersistent(qemuMigrationCookiePtr mig,
return -1;
}
- if (!def)
+ if (!def || !*def)
return 0;
- mig->persistent = def;
+ mig->persistent = *def;
+ *def = NULL;
mig->flags |= QEMU_MIGRATION_COOKIE_PERSISTENT;
mig->flagsMandatory |= QEMU_MIGRATION_COOKIE_PERSISTENT;
return 0;
diff --git a/src/qemu/qemu_migration_cookie.h b/src/qemu/qemu_migration_cookie.h
index 87eeb8682..f4de4da68 100644
--- a/src/qemu/qemu_migration_cookie.h
+++ b/src/qemu/qemu_migration_cookie.h
@@ -145,7 +145,7 @@ qemuMigrationCookieFree(qemuMigrationCookiePtr mig);
int
qemuMigrationCookieAddPersistent(qemuMigrationCookiePtr mig,
- virDomainDefPtr def);
+ virDomainDefPtr *def);
virDomainDefPtr
qemuMigrationCookieGetPersistent(qemuMigrationCookiePtr mig);
--
2.12.1