From 07652410a7af98ca03281c4bfe20415ced26a44a Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Wed, 19 Apr 2023 10:29:14 +0200 Subject: virTypedParamsValidate: Allow typed params to be both _UINT and _ULLONG MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For certain typed parameters we want to extend the supproted range by switching to VIR_TYPED_PARAM_ULLONG. To preserve compatibility we've added APIs such as 'virTypedParamsGetUnsigned' and 'virTypedParamListAddUnsigned' which automatically select the bigger type if necessary. This patch adds a new internal macro VIR_TYPED_PARAM_UNSIGNED which is used with virTypedParamsValidate to allow both types and adjusts the code to handle it properly. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/util/virtypedparam.c | 19 ++++++++++++++----- src/util/virtypedparam.h | 9 +++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c index 73b9420e5e..3bb8b125e9 100644 --- a/src/util/virtypedparam.c +++ b/src/util/virtypedparam.c @@ -63,7 +63,6 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...) size_t j; const char *name; const char *last_name = NULL; - int type; size_t nkeys = 0; size_t nkeysalloc = 0; g_autofree virTypedParameterPtr sorted = NULL; @@ -79,7 +78,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...) name = va_arg(ap, const char *); while (name) { - type = va_arg(ap, int); + int type = va_arg(ap, int); VIR_RESIZE_N(keys, nkeysalloc, nkeys, 1); if (virStrcpyStatic(keys[nkeys].field, name) < 0) { @@ -105,6 +104,9 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...) if (STRNEQ(sorted[i].field, keys[j].field)) { j++; } else { + const char *expecttype = virTypedParameterTypeToString(keys[j].type); + int type = sorted[i].type; + if (STREQ_NULLABLE(last_name, sorted[i].field) && !(keys[j].value.i & VIR_TYPED_PARAM_MULTIPLE)) { virReportError(VIR_ERR_INVALID_ARG, @@ -112,7 +114,15 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...) sorted[i].field); return -1; } - if (sorted[i].type != keys[j].type) { + + if (keys[j].type == VIR_TYPED_PARAM_UNSIGNED && + (type == VIR_TYPED_PARAM_UINT || + type == VIR_TYPED_PARAM_ULLONG)) { + type = VIR_TYPED_PARAM_UNSIGNED; + expecttype = "uint, ullong"; + } + + if (type != keys[j].type) { const char *badtype; badtype = virTypedParameterTypeToString(sorted[i].type); @@ -120,8 +130,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...) badtype = virTypedParameterTypeToString(0); virReportError(VIR_ERR_INVALID_ARG, _("invalid type '%1$s' for parameter '%2$s', expected '%3$s'"), - badtype, sorted[i].field, - virTypedParameterTypeToString(keys[j].type)); + badtype, sorted[i].field, expecttype); return -1; } last_name = sorted[i].field; diff --git a/src/util/virtypedparam.h b/src/util/virtypedparam.h index c2f58e994c..7454ef3ce0 100644 --- a/src/util/virtypedparam.h +++ b/src/util/virtypedparam.h @@ -24,6 +24,15 @@ #include "internal.h" #include "virenum.h" + +/** + * VIR_TYPED_PARAM_UNSIGNED: + * + * Special typed parameter type only used with virTypedParamsValidate to + * indicate that both VIR_TYPED_PARAM_UINT and VIR_TYPED_PARAM_ULLONG types + * are acceptable for given value. + */ +#define VIR_TYPED_PARAM_UNSIGNED (VIR_TYPED_PARAM_LAST + 1) /** * VIR_TYPED_PARAM_MULTIPLE: * -- cgit v1.2.1