From: Peter Krempa Date: Thu, 4 Feb 2021 21:53:45 +0000 (+0100) Subject: xenParseXLNamespaceData: Pre-calculate the length of array X-Git-Tag: v7.1.0-rc1~169 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a338253142b17bfa16aef6da796461114bcabd3;p=thirdparty%2Flibvirt.git xenParseXLNamespaceData: Pre-calculate the length of array Precalculate the lenght to avoid use of 'virStringListAdd' in a loop. The code is also simplified by using APIs which don't return errors. Signed-off-by: Peter Krempa Reviewed-by: Michal Privoznik --- diff --git a/src/libxl/xen_xl.c b/src/libxl/xen_xl.c index 69b139354e..6df0859b03 100644 --- a/src/libxl/xen_xl.c +++ b/src/libxl/xen_xl.c @@ -1150,33 +1150,36 @@ static int xenParseXLNamespaceData(virConfPtr conf, virDomainDefPtr def) { virConfValuePtr list = virConfGetValue(conf, "device_model_args"); - g_auto(GStrv) args = NULL; - size_t nargs; + virConfValuePtr next; + size_t nargs = 0; libxlDomainXmlNsDefPtr nsdata = NULL; + size_t n = 0; - if (list && list->type == VIR_CONF_LIST) { - list = list->list; - while (list) { - if ((list->type != VIR_CONF_STRING) || (list->str == NULL)) { - list = list->next; - continue; - } + if (!list || list->type != VIR_CONF_LIST) + return 0; - virStringListAdd(&args, list->str); - list = list->next; - } + list = list->list; + + for (next = list; next; next = next->next) { + if (next->type != VIR_CONF_STRING || !next->str) + continue; + + nargs++; } - if (!args) + if (nargs == 0) return 0; - nargs = g_strv_length(args); - if (nargs > 0) { - nsdata = g_new0(libxlDomainXmlNsDef, 1); + nsdata = g_new0(libxlDomainXmlNsDef, 1); + def->namespaceData = nsdata; + nsdata->args = g_new0(char *, nargs + 1); + nsdata->num_args = nargs; + + for (next = list; next; next = next->next) { + if (next->type != VIR_CONF_STRING || !next->str) + continue; - nsdata->args = g_steal_pointer(&args); - nsdata->num_args = nargs; - def->namespaceData = nsdata; + nsdata->args[n++] = g_strdup(next->str); } return 0;