]> xenbits.xensource.com Git - libvirt.git/commitdiff
virTypedParamsValidate: Allow typed params to be both _UINT and _ULLONG
authorPeter Krempa <pkrempa@redhat.com>
Wed, 19 Apr 2023 08:29:14 +0000 (10:29 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 2 May 2023 12:32:47 +0000 (14:32 +0200)
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 <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/util/virtypedparam.c
src/util/virtypedparam.h

index 73b9420e5e047df577d1f7e592d379b1c4c3f04f..3bb8b125e956d97cc29ac97c21eda15491b56164 100644 (file)
@@ -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;
index c2f58e994cfdc859dd6e995525f7ba02cdacb180..7454ef3ce0f986561e5d9b696f12eb990a0acb9d 100644 (file)
 #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:
  *