qemuMigrationCapsToJSON: Refactor variable cleanup

Use automatic memory allocation and move variables into correct scope to
simplify the code and remove the need for a 'error:' label.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2020-08-19 13:20:13 +02:00
parent 47a9f078f0
commit 99e4467bb1

View File

@ -759,14 +759,12 @@ virJSONValuePtr
qemuMigrationCapsToJSON(virBitmapPtr caps, qemuMigrationCapsToJSON(virBitmapPtr caps,
virBitmapPtr states) virBitmapPtr states)
{ {
virJSONValuePtr json = NULL; g_autoptr(virJSONValue) json = virJSONValueNewArray();
virJSONValuePtr cap = NULL;
qemuMigrationCapability bit; qemuMigrationCapability bit;
const char *name;
json = virJSONValueNewArray();
for (bit = 0; bit < QEMU_MIGRATION_CAP_LAST; bit++) { for (bit = 0; bit < QEMU_MIGRATION_CAP_LAST; bit++) {
g_autoptr(virJSONValue) cap = virJSONValueNewObject();
const char *name = qemuMigrationCapabilityTypeToString(bit);
bool supported = false; bool supported = false;
bool state = false; bool state = false;
@ -776,27 +774,19 @@ qemuMigrationCapsToJSON(virBitmapPtr caps,
ignore_value(virBitmapGetBit(states, bit, &state)); ignore_value(virBitmapGetBit(states, bit, &state));
cap = virJSONValueNewObject();
name = qemuMigrationCapabilityTypeToString(bit);
if (virJSONValueObjectAppendString(cap, "capability", name) < 0) if (virJSONValueObjectAppendString(cap, "capability", name) < 0)
goto error; return NULL;
if (virJSONValueObjectAppendBoolean(cap, "state", state) < 0) if (virJSONValueObjectAppendBoolean(cap, "state", state) < 0)
goto error; return NULL;
if (virJSONValueArrayAppend(json, cap) < 0) if (virJSONValueArrayAppend(json, cap) < 0)
goto error; return NULL;
cap = NULL; cap = NULL;
} }
return json; return g_steal_pointer(&json);
error:
virJSONValueFree(json);
virJSONValueFree(cap);
return NULL;
} }