mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-10-02 12:25:52 +00:00
cmdStartGetFDs: Modernize
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 <pkrempa@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
c98432784a
commit
b0015df263
@ -4025,7 +4025,7 @@ cmdStartGetFDs(vshControl *ctl,
|
|||||||
{
|
{
|
||||||
const char *fdopt;
|
const char *fdopt;
|
||||||
g_auto(GStrv) fdlist = NULL;
|
g_auto(GStrv) fdlist = NULL;
|
||||||
int *fds = NULL;
|
g_autofree int *fds = NULL;
|
||||||
size_t nfds = 0;
|
size_t nfds = 0;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
@ -4040,23 +4040,19 @@ cmdStartGetFDs(vshControl *ctl,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; fdlist[i] != NULL; i++) {
|
nfds = g_strv_length(fdlist);
|
||||||
int fd;
|
fds = g_new0(int, nfds);
|
||||||
if (virStrToLong_i(fdlist[i], NULL, 10, &fd) < 0) {
|
|
||||||
|
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]);
|
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;
|
*nfdsret = nfds;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error:
|
|
||||||
VIR_FREE(fds);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
|
Loading…
Reference in New Issue
Block a user