1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-01-03 05:17:54 +03:00

Replace virStringListLength by g_strv_length

The glib implementation doesn't tolerate NULL but in most cases we check
before anyways. The rest of the callers adds a NULL check.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2021-02-05 18:03:26 +01:00
parent 1114cf5e7e
commit dcd547aec1
13 changed files with 25 additions and 19 deletions

View File

@ -635,7 +635,7 @@ virCPUarmDecode(virCPUDefPtr cpu,
cpu->vendor = g_strdup(vendor->name); cpu->vendor = g_strdup(vendor->name);
if (cpuData->features) { if (cpuData->features) {
cpu->nfeatures = virStringListLength((const char **)cpuData->features); cpu->nfeatures = g_strv_length(cpuData->features);
cpu->features = g_new0(virCPUFeatureDef, cpu->nfeatures); cpu->features = g_new0(virCPUFeatureDef, cpu->nfeatures);
for (i = 0; i < cpu->nfeatures; i++) { for (i = 0; i < cpu->nfeatures; i++) {

View File

@ -648,7 +648,7 @@ virQEMUDriverConfigLoadProcessEntry(virQEMUDriverConfigPtr cfg,
} }
VIR_FREE(cfg->hugetlbfs); VIR_FREE(cfg->hugetlbfs);
cfg->nhugetlbfs = virStringListLength((const char *const *)hugetlbfs); cfg->nhugetlbfs = g_strv_length(hugetlbfs);
if (hugetlbfs[0]) if (hugetlbfs[0])
cfg->hugetlbfs = g_new0(virHugeTLBFS, cfg->nhugetlbfs); cfg->hugetlbfs = g_new0(virHugeTLBFS, cfg->nhugetlbfs);
@ -847,7 +847,7 @@ virQEMUDriverConfigLoadNVRAMEntry(virQEMUDriverConfigPtr cfg,
return 0; return 0;
} }
cfg->nfirmwares = virStringListLength((const char *const *)nvram); cfg->nfirmwares = g_strv_length(nvram);
cfg->firmwares = g_new0(virFirmwarePtr, cfg->nfirmwares); cfg->firmwares = g_new0(virFirmwarePtr, cfg->nfirmwares);
for (i = 0; nvram[i] != NULL; i++) { for (i = 0; nvram[i] != NULL; i++) {

View File

@ -1191,7 +1191,10 @@ qemuFirmwareFetchParsedConfigs(bool privileged,
if (qemuFirmwareFetchConfigs(&paths, privileged) < 0) if (qemuFirmwareFetchConfigs(&paths, privileged) < 0)
return -1; return -1;
npaths = virStringListLength((const char **)paths); if (!paths)
return 0;
npaths = g_strv_length(paths);
firmwares = g_new0(qemuFirmwarePtr, npaths); firmwares = g_new0(qemuFirmwarePtr, npaths);

View File

@ -250,8 +250,10 @@ qemuVhostUserFetchParsedConfigs(bool privileged,
if (qemuVhostUserFetchConfigs(&paths, privileged) < 0) if (qemuVhostUserFetchConfigs(&paths, privileged) < 0)
return -1; return -1;
npaths = virStringListLength((const char **)paths); if (!paths)
return 0;
npaths = g_strv_length(paths);
vus = g_new0(qemuVhostUserPtr, npaths); vus = g_new0(qemuVhostUserPtr, npaths);
for (i = 0; i < npaths; i++) { for (i = 0; i < npaths; i++) {

View File

@ -1013,7 +1013,8 @@ int virProcessGetStartTime(pid_t pid,
tokens = virStringSplit(tmp, " ", 0); tokens = virStringSplit(tmp, " ", 0);
if (virStringListLength((const char * const *)tokens) < 20) { if (!tokens ||
g_strv_length(tokens) < 20) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Cannot find start time in %s"), _("Cannot find start time in %s"),
filename); filename);

View File

@ -182,8 +182,8 @@ virStringListMerge(char ***dst,
if (!src || !*src) if (!src || !*src)
return 0; return 0;
dst_len = virStringListLength((const char **) *dst); dst_len = g_strv_length(*dst);
src_len = virStringListLength((const char **) *src); src_len = g_strv_length(*src);
if (VIR_REALLOC_N(*dst, dst_len + src_len + 1) < 0) if (VIR_REALLOC_N(*dst, dst_len + src_len + 1) < 0)
return -1; return -1;

View File

@ -394,7 +394,7 @@ static int testConfParseStringList(const void *opaque G_GNUC_UNUSED)
if (virConfGetValueStringList(conf, "string_list", false, &str) < 0) if (virConfGetValueStringList(conf, "string_list", false, &str) < 0)
goto cleanup; goto cleanup;
if (virStringListLength((const char *const*)str) != 2) { if (!str || g_strv_length(str) != 2) {
fprintf(stderr, "expected a 2 element list\n"); fprintf(stderr, "expected a 2 element list\n");
goto cleanup; goto cleanup;
} }
@ -418,7 +418,7 @@ static int testConfParseStringList(const void *opaque G_GNUC_UNUSED)
if (virConfGetValueStringList(conf, "string", true, &str) < 0) if (virConfGetValueStringList(conf, "string", true, &str) < 0)
goto cleanup; goto cleanup;
if (virStringListLength((const char *const*)str) != 1) { if (!str || g_strv_length(str) != 1) {
fprintf(stderr, "expected a 1 element list\n"); fprintf(stderr, "expected a 1 element list\n");
goto cleanup; goto cleanup;
} }

View File

@ -243,10 +243,10 @@ testStringSearch(const void *opaque)
goto cleanup; goto cleanup;
} }
if (virStringListLength((const char * const *)matches) != nmatches) { if (g_strv_length(matches) != nmatches) {
fprintf(stderr, "expected %zu matches on %s but got %zd matches\n", fprintf(stderr, "expected %zu matches on %s but got %u matches\n",
data->expectNMatches, data->str, data->expectNMatches, data->str,
virStringListLength((const char * const *)matches)); g_strv_length(matches));
goto cleanup; goto cleanup;
} }

View File

@ -87,7 +87,7 @@ char **
virshCommaStringListComplete(const char *input, virshCommaStringListComplete(const char *input,
const char **options) const char **options)
{ {
const size_t optionsLen = virStringListLength(options); const size_t optionsLen = g_strv_length((char **) options);
g_autofree char *inputCopy = NULL; g_autofree char *inputCopy = NULL;
g_auto(GStrv) inputList = NULL; g_auto(GStrv) inputList = NULL;
g_auto(GStrv) ret = NULL; g_auto(GStrv) ret = NULL;

View File

@ -14391,7 +14391,7 @@ cmdSetUserSSHKeys(vshControl *ctl, const vshCmd *cmd)
if (!(keys = virStringSplit(buffer, "\n", -1))) if (!(keys = virStringSplit(buffer, "\n", -1)))
goto cleanup; goto cleanup;
nkeys = virStringListLength((const char **) keys); nkeys = g_strv_length(keys);
if (nkeys == 0) { if (nkeys == 0) {
vshError(ctl, _("File %s contains no keys"), from); vshError(ctl, _("File %s contains no keys"), from);
goto cleanup; goto cleanup;

View File

@ -1325,7 +1325,7 @@ cmdCPUBaseline(vshControl *ctl, const vshCmd *cmd)
return false; return false;
result = virConnectBaselineCPU(priv->conn, (const char **)list, result = virConnectBaselineCPU(priv->conn, (const char **)list,
virStringListLength((const char **)list), g_strv_length(list),
flags); flags);
if (result) { if (result) {
@ -1798,7 +1798,7 @@ cmdHypervisorCPUBaseline(vshControl *ctl,
result = virConnectBaselineHypervisorCPU(priv->conn, emulator, arch, result = virConnectBaselineHypervisorCPU(priv->conn, emulator, arch,
machine, virttype, machine, virttype,
(const char **)list, (const char **)list,
virStringListLength((const char **)list), g_strv_length(list),
flags); flags);
if (result) { if (result) {

View File

@ -103,7 +103,7 @@ static int virLoginShellGetShellArgv(virConfPtr conf,
(*shargv)[0] = g_strdup("/bin/sh"); (*shargv)[0] = g_strdup("/bin/sh");
*shargvlen = 1; *shargvlen = 1;
} else { } else {
*shargvlen = virStringListLength((const char *const *)*shargv); *shargvlen = g_strv_length(*shargv);
} }
return 0; return 0;
} }

View File

@ -2676,7 +2676,7 @@ vshCompleterFilter(char ***list,
if (!list || !*list) if (!list || !*list)
return 0; return 0;
list_len = virStringListLength((const char **) *list); list_len = g_strv_length(*list);
newList = g_new0(char *, list_len + 1); newList = g_new0(char *, list_len + 1);
for (i = 0; i < list_len; i++) { for (i = 0; i < list_len; i++) {