]> xenbits.xensource.com Git - libvirt.git/commitdiff
Libvirt: virTypedParamsValidate: Fix detection of multiple parameters
authorJason J. Herne <jjherne@linux.vnet.ibm.com>
Tue, 12 Apr 2016 14:13:27 +0000 (10:13 -0400)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 18 Apr 2016 13:57:20 +0000 (15:57 +0200)
virTypedParamsValidate currently uses an index based check to find
duplicate parameters. This check does not work. Consider the following
simple example:

We have only 2 keys
A  (multiples allowed)
B  (multiples NOT allowed)

We are given the following list of parameters to check:
A
A
B

If you work through the validation loop you will see that our last iteration
through the loop has i=2 and j=1. In this case, i > j and keys[j].value.i will
indicate that multiples are not allowed. Both conditionals are satisfied so
an incorrect error will be given: "parameter '%s' occurs multiple times"

This patch replaces the index based check with code that remembers
the name of the last parameter seen and only triggers the error case if
the current parameter name equals the last one. This works because the
list is sorted and duplicate parameters will be grouped together.

In reality, we hit this bug while using selective block migration to migrate
a guest with 5 disks. 5 was apparently just the right number to push i > j
and hit this bug.

virsh migrate --live guestname --copy-storage-all
              --migrate-disks vdb,vdc,vdd,vde,vdf
              qemu+ssh://dsthost/system

Signed-off-by: Jason J. Herne <jjherne@linux.vnet.ibm.com>
Reviewed-by: Eric Farman <farman@linux.vnet.ibm.com>
src/util/virtypedparam.c
tests/virtypedparamtest.c

index 138fc64381ed9b90cd6d9b4dde4dfca9cdec9f14..23109e1c2e4ff40d697f06e07525b4321d819dd9 100644 (file)
@@ -66,7 +66,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
     va_list ap;
     int ret = -1;
     size_t i, j;
-    const char *name;
+    const char *name, *last_name = NULL;
     int type;
     size_t nkeys = 0, nkeysalloc = 0;
     virTypedParameterPtr sorted = NULL, keys = NULL;
@@ -106,7 +106,8 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
         if (STRNEQ(sorted[i].field, keys[j].field)) {
             j++;
         } else {
-            if (i > j && !(keys[j].value.i & VIR_TYPED_PARAM_MULTIPLE)) {
+            if (STREQ_NULLABLE(last_name, sorted[i].field) &&
+                !(keys[j].value.i & VIR_TYPED_PARAM_MULTIPLE)) {
                 virReportError(VIR_ERR_INVALID_ARG,
                                _("parameter '%s' occurs multiple times"),
                                sorted[i].field);
@@ -125,6 +126,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
                                virTypedParameterTypeToString(keys[j].type));
                 goto cleanup;
             }
+            last_name = sorted[i].field;
             i++;
         }
     }
index f4a57925e597de2d644e52764f3f9367e2d06683..d125491596affa7965c01fed13ce80d0e6f4e310 100644 (file)
@@ -56,6 +56,7 @@ testTypedParamsValidate(const void *opaque)
             "foobar", VIR_TYPED_PARAM_STRING | test->foobar_flags,
             "foo", VIR_TYPED_PARAM_INT,
             "bar", VIR_TYPED_PARAM_UINT,
+            "zzz", VIR_TYPED_PARAM_UINT,
             NULL);
 
     if (test->expected_errcode) {
@@ -258,6 +259,19 @@ testTypedParamsValidator(void)
             )
             .expected_errcode = 0, .expected_errmessage = NULL,
         },
+        {
+            .name = "BUG, non-duplicate marked as duplicate",
+            .foobar_flags = VIR_TYPED_PARAM_MULTIPLE,
+            PARAMS(
+                { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
+                { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
+                { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
+                { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
+                { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
+                { .field = "zzz",    .type = VIR_TYPED_PARAM_UINT },
+            )
+            .expected_errcode = 0, .expected_errmessage = NULL,
+        },
         {
             .name = NULL
         }