From a5bc5f0ecf82e0e3cb1dd030888ed11174c5b704 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Mon, 14 Jun 2021 16:34:28 +0200 Subject: [PATCH] virQEMUCapsProbeQMPTPM: Refactor handling of string lists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Ján Tomko --- 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 6896b43d0e..059d6badf2 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -3160,36 +3160,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; }