This refactors multiple aspects of the function:
1) Use automatic memory freeing
2) Remove need to check element count in the returned arrays
3) Fixes questionable code linebreaks
4) Removes reuse of variables
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/qemu/qemu_capabilities.c | 33 ++++++++++++---------------------
1 file changed, 12 insertions(+), 21 deletions(-)
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 576fb6429b..bd7a24cf38 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -3167,36 +3167,27 @@ static int
virQEMUCapsProbeQMPTPM(virQEMUCaps *qemuCaps,
qemuMonitor *mon)
{
- int nentries;
+ g_auto(GStrv) models = NULL;
+ g_auto(GStrv) types = NULL;
size_t i;
- char **entries = NULL;
- if ((nentries = qemuMonitorGetTPMModels(mon, &entries)) < 0)
+ if (qemuMonitorGetTPMModels(mon, &models) < 0)
return -1;
- if (nentries > 0) {
- for (i = 0; i < G_N_ELEMENTS(virQEMUCapsTPMModelsToCaps); i++) {
- const char *needle = virDomainTPMModelTypeToString(
- virQEMUCapsTPMModelsToCaps[i].type);
- if (g_strv_contains((const char **)entries, needle))
- virQEMUCapsSet(qemuCaps,
- virQEMUCapsTPMModelsToCaps[i].caps);
- }
+ for (i = 0; i < G_N_ELEMENTS(virQEMUCapsTPMModelsToCaps); i++) {
+ const char *needle =
virDomainTPMModelTypeToString(virQEMUCapsTPMModelsToCaps[i].type);
+ if (g_strv_contains((const char **)models, needle))
+ virQEMUCapsSet(qemuCaps, virQEMUCapsTPMModelsToCaps[i].caps);
}
- g_strfreev(entries);
- if ((nentries = qemuMonitorGetTPMTypes(mon, &entries)) < 0)
+ if (qemuMonitorGetTPMTypes(mon, &types) < 0)
return -1;
- if (nentries > 0) {
- for (i = 0; i < G_N_ELEMENTS(virQEMUCapsTPMTypesToCaps); i++) {
- const char *needle = virDomainTPMBackendTypeToString(
- virQEMUCapsTPMTypesToCaps[i].type);
- if (g_strv_contains((const char **)entries, needle))
- virQEMUCapsSet(qemuCaps, virQEMUCapsTPMTypesToCaps[i].caps);
- }
+ for (i = 0; i < G_N_ELEMENTS(virQEMUCapsTPMTypesToCaps); i++) {
+ const char *needle =
virDomainTPMBackendTypeToString(virQEMUCapsTPMTypesToCaps[i].type);
+ if (g_strv_contains((const char **)types, needle))
+ virQEMUCapsSet(qemuCaps, virQEMUCapsTPMTypesToCaps[i].caps);
}
- g_strfreev(entries);
return 0;
}
--
2.31.1