提交 791c90cb 编写于 作者: J Jason J. Herne 提交者: Cole Robinson

Libvirt: virTypedParamsValidate: Fix detection of multiple parameters

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/systemSigned-off-by: NJason J. Herne <jjherne@linux.vnet.ibm.com>
Reviewed-by: NEric Farman <farman@linux.vnet.ibm.com>
(cherry picked from commit 0e570a6a)
上级 58650d53
...@@ -66,7 +66,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...) ...@@ -66,7 +66,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
va_list ap; va_list ap;
int ret = -1; int ret = -1;
size_t i, j; size_t i, j;
const char *name; const char *name, *last_name = NULL;
int type; int type;
size_t nkeys = 0, nkeysalloc = 0; size_t nkeys = 0, nkeysalloc = 0;
virTypedParameterPtr sorted = NULL, keys = NULL; virTypedParameterPtr sorted = NULL, keys = NULL;
...@@ -106,7 +106,8 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...) ...@@ -106,7 +106,8 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
if (STRNEQ(sorted[i].field, keys[j].field)) { if (STRNEQ(sorted[i].field, keys[j].field)) {
j++; j++;
} else { } 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, virReportError(VIR_ERR_INVALID_ARG,
_("parameter '%s' occurs multiple times"), _("parameter '%s' occurs multiple times"),
sorted[i].field); sorted[i].field);
...@@ -125,6 +126,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...) ...@@ -125,6 +126,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
virTypedParameterTypeToString(keys[j].type)); virTypedParameterTypeToString(keys[j].type));
goto cleanup; goto cleanup;
} }
last_name = sorted[i].field;
i++; i++;
} }
} }
......
...@@ -56,6 +56,7 @@ testTypedParamsValidate(const void *opaque) ...@@ -56,6 +56,7 @@ testTypedParamsValidate(const void *opaque)
"foobar", VIR_TYPED_PARAM_STRING | test->foobar_flags, "foobar", VIR_TYPED_PARAM_STRING | test->foobar_flags,
"foo", VIR_TYPED_PARAM_INT, "foo", VIR_TYPED_PARAM_INT,
"bar", VIR_TYPED_PARAM_UINT, "bar", VIR_TYPED_PARAM_UINT,
"zzz", VIR_TYPED_PARAM_UINT,
NULL); NULL);
if (test->expected_errcode) { if (test->expected_errcode) {
...@@ -258,6 +259,19 @@ testTypedParamsValidator(void) ...@@ -258,6 +259,19 @@ testTypedParamsValidator(void)
) )
.expected_errcode = 0, .expected_errmessage = NULL, .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 .name = NULL
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册