From b0015df263d1870a5558a17b28e722dc445a3ba3 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 27 Jan 2022 17:14:32 +0100 Subject: [PATCH] cmdStartGetFDs: Modernize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Calculate the length of the FD list beforehand to avoid multiple expansions and mainly simplify the code and use automatic freeing to remove the error code path. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- tools/virsh-domain.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 4ff531f891..77eac86f6a 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -4025,7 +4025,7 @@ cmdStartGetFDs(vshControl *ctl, { const char *fdopt; g_auto(GStrv) fdlist = NULL; - int *fds = NULL; + g_autofree int *fds = NULL; size_t nfds = 0; size_t i; @@ -4040,23 +4040,19 @@ cmdStartGetFDs(vshControl *ctl, return -1; } - for (i = 0; fdlist[i] != NULL; i++) { - int fd; - if (virStrToLong_i(fdlist[i], NULL, 10, &fd) < 0) { + nfds = g_strv_length(fdlist); + fds = g_new0(int, nfds); + + for (i = 0; i < nfds; i++) { + if (virStrToLong_i(fdlist[i], NULL, 10, fds + i) < 0) { vshError(ctl, _("Unable to parse FD number '%s'"), fdlist[i]); - goto error; + return -1; } - VIR_EXPAND_N(fds, nfds, 1); - fds[nfds - 1] = fd; } - *fdsret = fds; + *fdsret = g_steal_pointer(&fds); *nfdsret = nfds; return 0; - - error: - VIR_FREE(fds); - return -1; } static bool