virTypedParamsValidate: Refactor variable declaration and cleanup

Use automatic memory cleanup for the 'keys' and 'sorted' helpers and
remove the 'cleanup' label. Since this patch is modifying variable
declarations ensure that all declarations conform with our coding style.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2023-04-19 10:37:09 +02:00
parent 111eeba7a7
commit b5ee977d17

View File

@ -59,12 +59,15 @@ int
virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...) virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
{ {
va_list ap; va_list ap;
int ret = -1; size_t i;
size_t i, j; size_t j;
const char *name, *last_name = NULL; const char *name;
const char *last_name = NULL;
int type; int type;
size_t nkeys = 0, nkeysalloc = 0; size_t nkeys = 0;
virTypedParameterPtr sorted = NULL, keys = NULL; size_t nkeysalloc = 0;
g_autofree virTypedParameterPtr sorted = NULL;
g_autofree virTypedParameterPtr keys = NULL;
va_start(ap, nparams); va_start(ap, nparams);
@ -82,7 +85,8 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
if (virStrcpyStatic(keys[nkeys].field, name) < 0) { if (virStrcpyStatic(keys[nkeys].field, name) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Field name '%1$s' too long"), name); _("Field name '%1$s' too long"), name);
goto cleanup; va_end(ap);
return -1;
} }
keys[nkeys].type = type & ~VIR_TYPED_PARAM_MULTIPLE; keys[nkeys].type = type & ~VIR_TYPED_PARAM_MULTIPLE;
@ -93,6 +97,8 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
name = va_arg(ap, const char *); name = va_arg(ap, const char *);
} }
va_end(ap);
qsort(keys, nkeys, sizeof(*keys), virTypedParamsSortName); qsort(keys, nkeys, sizeof(*keys), virTypedParamsSortName);
for (i = 0, j = 0; i < nparams && j < nkeys;) { for (i = 0, j = 0; i < nparams && j < nkeys;) {
@ -104,7 +110,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
virReportError(VIR_ERR_INVALID_ARG, virReportError(VIR_ERR_INVALID_ARG,
_("parameter '%1$s' occurs multiple times"), _("parameter '%1$s' occurs multiple times"),
sorted[i].field); sorted[i].field);
goto cleanup; return -1;
} }
if (sorted[i].type != keys[j].type) { if (sorted[i].type != keys[j].type) {
const char *badtype; const char *badtype;
@ -116,7 +122,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
_("invalid type '%1$s' for parameter '%2$s', expected '%3$s'"), _("invalid type '%1$s' for parameter '%2$s', expected '%3$s'"),
badtype, sorted[i].field, badtype, sorted[i].field,
virTypedParameterTypeToString(keys[j].type)); virTypedParameterTypeToString(keys[j].type));
goto cleanup; return -1;
} }
last_name = sorted[i].field; last_name = sorted[i].field;
i++; i++;
@ -127,15 +133,10 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
_("parameter '%1$s' not supported"), _("parameter '%1$s' not supported"),
sorted[i].field); sorted[i].field);
goto cleanup; return -1;
} }
ret = 0; return 0;
cleanup:
va_end(ap);
VIR_FREE(sorted);
VIR_FREE(keys);
return ret;
} }