util: typedparam: Optionally copy strings passed to virTypedParameterAssignValue

Some code paths already pass in pointers to strings which should be
added directly as the value of the typed parameter. To allow more
universal use of virTypedParameterAssignValue add a flag which allows to
copy the value in place.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2019-08-14 14:52:46 +02:00
parent 803d19a68d
commit 6b39203fac

View File

@ -209,7 +209,8 @@ virTypedParameterToString(virTypedParameterPtr param)
static int static int
virTypedParameterAssignValueVArgs(virTypedParameterPtr param, virTypedParameterAssignValueVArgs(virTypedParameterPtr param,
int type, int type,
va_list ap) va_list ap,
bool copystr)
{ {
param->type = type; param->type = type;
switch (type) { switch (type) {
@ -232,7 +233,13 @@ virTypedParameterAssignValueVArgs(virTypedParameterPtr param,
param->value.b = !!va_arg(ap, int); param->value.b = !!va_arg(ap, int);
break; break;
case VIR_TYPED_PARAM_STRING: case VIR_TYPED_PARAM_STRING:
param->value.s = va_arg(ap, char *); if (copystr) {
if (VIR_STRDUP(param->value.s, va_arg(ap, char *)) < 0)
return -1;
} else {
param->value.s = va_arg(ap, char *);
}
if (!param->value.s && VIR_STRDUP(param->value.s, "") < 0) if (!param->value.s && VIR_STRDUP(param->value.s, "") < 0)
return -1; return -1;
break; break;
@ -265,7 +272,7 @@ virTypedParameterAssign(virTypedParameterPtr param, const char *name,
} }
va_start(ap, type); va_start(ap, type);
ret = virTypedParameterAssignValueVArgs(param, type, ap); ret = virTypedParameterAssignValueVArgs(param, type, ap, false);
va_end(ap); va_end(ap);
return ret; return ret;