[libvirt PATCH 00/38] Refactor XML parsing boilerplate code

This series replaces some recurring boilerplate code in src/conf/ regarding the extraction of a virTristate(Switch|Bool) XML attribute. The boilerplate code looks roughly like this, g_autofree char *str = NULL; if (str = virXMLPropString(node, ...)) { int val; if ((val = virTristateBoolTypeFromString(str)) <= 0) { virReportError(...) return -1; } def->... = val; } with some variations regarding how `str` is free'd in case of later re-use, the exact error message for invalid values, whether or not `VIR_TRISTATE_(SWITCH|BOOL)_ABSENT` is explicitly checked for (`val < 0` vs. `val <= 0`), whether an intermediate variable is used or the value is assigned directly, and in some cases the conditions in the two if-blocks are merged. As a side effect, this makes the error messages for invalid values in these attributes much more consistent and catches some instances where e.g. `<foo bar="default"/>` would incorrectly be accepted. Patch #11 (virDomainChrSourceReconnectDefParseXML) is a good example of what this refactoring is about. Tim Wiederhake (38): virStorageAdapterFCHost: Fix comment virxml: Add virXMLPropYesNo virxml: Add virXMLPropOnOff domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainKeyWrapCipherDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainVirtioOptionsParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainDeviceInfoParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainDiskSourceNetworkParse domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainDiskSourceNVMeParse domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainDiskDefDriverParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainActualNetDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainChrSourceReconnectDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainNetDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainChrSourceDefParseTCP domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainChrSourceDefParseFile domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainChrSourceDefParseLog domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainGraphicsDefParseXMLVNC domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainGraphicsDefParseXMLSDL domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainGraphicsDefParseXMLSpice domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainAudioCommonParse domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainAudioJackParse domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainAudioOSSParse domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainAudioDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainMemballoonDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainShmemDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainPerfEventDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainMemoryDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainIOMMUDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainVsockDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainFeaturesDefParse domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainLoaderDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainVcpuParse backup_conf: Use virXMLProp(OnOff|YesNo) in virDomainBackupDiskDefParseXML backup_conf: Use virXMLProp(OnOff|YesNo) in virDomainBackupDefParse device_conf: Use virXMLProp(OnOff|YesNo) in virPCIDeviceAddressParseXML network_conf: Use virXMLProp(OnOff|YesNo) in virNetworkForwardNatDefParseXML numa_conf: Use virXMLProp(OnOff|YesNo) in virDomainNumaDefParseXML storage_adapter_conf: Use virXMLProp(OnOff|YesNo) in virStorageAdapterParseXMLFCHost storage_conf: Use virXMLProp(OnOff|YesNo) in virStoragePoolDefParseSource src/conf/backup_conf.c | 32 +- src/conf/device_conf.c | 10 +- src/conf/domain_conf.c | 791 +++++++++----------------------- src/conf/network_conf.c | 15 +- src/conf/numa_conf.c | 13 +- src/conf/storage_adapter_conf.c | 17 +- src/conf/storage_adapter_conf.h | 2 +- src/conf/storage_conf.c | 16 +- src/util/virxml.c | 74 +++ src/util/virxml.h | 7 + 10 files changed, 314 insertions(+), 663 deletions(-) -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/storage_adapter_conf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/conf/storage_adapter_conf.h b/src/conf/storage_adapter_conf.h index 4c7da7c8d9..93879acb6e 100644 --- a/src/conf/storage_adapter_conf.h +++ b/src/conf/storage_adapter_conf.h @@ -51,7 +51,7 @@ struct _virStorageAdapterFCHost { char *parent_fabric_wwn; char *wwnn; char *wwpn; - int managed; /* enum virTristateSwitch */ + int managed; /* enum virTristateBool */ }; typedef struct _virStorageAdapter virStorageAdapter; -- 2.26.2

On 3/18/21 9:00 AM, Tim Wiederhake wrote:
Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/storage_adapter_conf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/conf/storage_adapter_conf.h b/src/conf/storage_adapter_conf.h index 4c7da7c8d9..93879acb6e 100644 --- a/src/conf/storage_adapter_conf.h +++ b/src/conf/storage_adapter_conf.h @@ -51,7 +51,7 @@ struct _virStorageAdapterFCHost { char *parent_fabric_wwn; char *wwnn; char *wwpn; - int managed; /* enum virTristateSwitch */ + int managed; /* enum virTristateBool */ };
typedef struct _virStorageAdapter virStorageAdapter;
I'd expand the commit message a bit, like this: virStorageAdapterFCHost: Fix comment for @managed The enum that's used for @managed is virTristateBool and not virTristateSwitch as the comment says. Michal

Convenience function to return value of a yes / no attribute. Does not use virTristateBoolTypeFromString to disallow "default". Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/util/virxml.c | 37 +++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 4 ++++ 2 files changed, 41 insertions(+) diff --git a/src/util/virxml.c b/src/util/virxml.c index 060b7530fc..47e8414bd5 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -556,6 +556,43 @@ virXMLNodeContentString(xmlNodePtr node) } +/** + * virXMLPropYesNo: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * @value: The returned virTristateBool value + * + * Convenience function to return value of a yes / no attribute. + * + * Returns 1 in case of success in which case @value is set, + * or 0 if the attribute is not present, + * or -1 and reports an error on failure. + */ +int +virXMLPropYesNo(xmlNodePtr node, const char* name, virTristateBool *value) +{ + g_autofree char *tmp = virXMLPropString(node, name); + + if (!tmp) + return 0; + + if (STREQ("yes", tmp)) { + *value = VIR_TRISTATE_BOOL_YES; + return 1; + } + + if (STREQ("no", tmp)) { + *value = VIR_TRISTATE_BOOL_NO; + return 1; + } + + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in node '%s': '%s'. Expected 'yes' or 'no'"), + name, node->name, tmp); + return -1; +} + + /** * virXPathBoolean: * @xpath: the XPath string to evaluate diff --git a/src/util/virxml.h b/src/util/virxml.h index d32f77b867..072948b3e2 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -28,6 +28,7 @@ #include <libxml/relaxng.h> #include "virbuffer.h" +#include "virenum.h" xmlXPathContextPtr virXMLXPathContextNew(xmlDocPtr xml) G_GNUC_WARN_UNUSED_RESULT; @@ -77,6 +78,9 @@ char * virXMLPropStringLimit(xmlNodePtr node, const char *name, size_t maxlen); char * virXMLNodeContentString(xmlNodePtr node); +int virXMLPropYesNo(xmlNodePtr node, + const char *name, + virTristateBool *value); /* Internal function; prefer the macros below. */ xmlDocPtr virXMLParseHelper(int domcode, -- 2.26.2

On 3/18/21 9:00 AM, Tim Wiederhake wrote:
Convenience function to return value of a yes / no attribute.
Does not use virTristateBoolTypeFromString to disallow "default".
Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/util/virxml.c | 37 +++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 4 ++++ 2 files changed, 41 insertions(+)
diff --git a/src/util/virxml.c b/src/util/virxml.c index 060b7530fc..47e8414bd5 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -556,6 +556,43 @@ virXMLNodeContentString(xmlNodePtr node) }
+/** + * virXMLPropYesNo: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * @value: The returned virTristateBool value + * + * Convenience function to return value of a yes / no attribute. + * + * Returns 1 in case of success in which case @value is set, + * or 0 if the attribute is not present, + * or -1 and reports an error on failure. + */ +int +virXMLPropYesNo(xmlNodePtr node, const char* name, virTristateBool *value) +{ + g_autofree char *tmp = virXMLPropString(node, name); + + if (!tmp) + return 0; + + if (STREQ("yes", tmp)) { + *value = VIR_TRISTATE_BOOL_YES; + return 1; + } + + if (STREQ("no", tmp)) { + *value = VIR_TRISTATE_BOOL_NO; + return 1; + } + + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in node '%s': '%s'. Expected 'yes' or 'no'"), + name, node->name, tmp); + return -1;
How about: int virXMLPropYesNo(xmlNodePtr node, const char* name, virTristateBool *value) { g_autofree char *tmp = virXMLPropString(node, name); int val; if (!tmp) return 0; if ((val = virTristateBoolTypeFromString(tmp)) <= 0) { virReportError(VIR_ERR_XML_ERROR, _("Invalid value for attribute '%s' in node '%s': '%s'. Expected 'yes' or 'no'"), name, node->name, tmp); return -1; } *value = val; return 1; } "default" which is VIR_TRISTATE_BOOL_ABSENT is explicitly set to 0, so that things like g_new0() initialize the value to _ABSENT. Also, the function should be listed in private symbols we export: diff --git i/src/libvirt_private.syms w/src/libvirt_private.syms index 526dcee11a..85bebca23a 100644 --- i/src/libvirt_private.syms +++ w/src/libvirt_private.syms @@ -3545,6 +3545,7 @@ virXMLParseHelper; virXMLPickShellSafeComment; virXMLPropString; virXMLPropStringLimit; +virXMLPropYesNo; virXMLSaveFile; virXMLValidateAgainstSchema; virXMLValidatorFree; I'm surprised linked did not run into any issues. No need to resend, I can fix it before pushing. The similar applies to the next patch. Michal

On Thu, Mar 18, 2021 at 04:03:17PM +0100, Michal Privoznik wrote:
On 3/18/21 9:00 AM, Tim Wiederhake wrote:
Convenience function to return value of a yes / no attribute.
Does not use virTristateBoolTypeFromString to disallow "default".
Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/util/virxml.c | 37 +++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 4 ++++ 2 files changed, 41 insertions(+)
diff --git a/src/util/virxml.c b/src/util/virxml.c index 060b7530fc..47e8414bd5 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -556,6 +556,43 @@ virXMLNodeContentString(xmlNodePtr node) } +/** + * virXMLPropYesNo: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * @value: The returned virTristateBool value + * + * Convenience function to return value of a yes / no attribute. + * + * Returns 1 in case of success in which case @value is set, + * or 0 if the attribute is not present, + * or -1 and reports an error on failure. + */ +int +virXMLPropYesNo(xmlNodePtr node, const char* name, virTristateBool *value)
I kind of feel this should be called virXMLPropTristateBool and the next patch virXMLPropTristateSwitch, so that they both match their output variable types.
+{ + g_autofree char *tmp = virXMLPropString(node, name); + + if (!tmp) + return 0; + + if (STREQ("yes", tmp)) { + *value = VIR_TRISTATE_BOOL_YES; + return 1; + } + + if (STREQ("no", tmp)) { + *value = VIR_TRISTATE_BOOL_NO; + return 1; + } + + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in node '%s': '%s'. Expected 'yes' or 'no'"), + name, node->name, tmp); + return -1;
How about:
int virXMLPropYesNo(xmlNodePtr node, const char* name, virTristateBool *value) { g_autofree char *tmp = virXMLPropString(node, name); int val;
if (!tmp) return 0;
if ((val = virTristateBoolTypeFromString(tmp)) <= 0) { virReportError(VIR_ERR_XML_ERROR, _("Invalid value for attribute '%s' in node '%s': '%s'. Expected 'yes' or 'no'"), name, node->name, tmp); return -1; }
*value = val; return 1; }
Yep, I think we really should do this, not open code the same thing. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Thu, Mar 18, 2021 at 09:00:41AM +0100, Tim Wiederhake wrote:
Convenience function to return value of a yes / no attribute.
Does not use virTristateBoolTypeFromString to disallow "default".
Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/util/virxml.c | 37 +++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 4 ++++ 2 files changed, 41 insertions(+)
diff --git a/src/util/virxml.c b/src/util/virxml.c index 060b7530fc..47e8414bd5 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -556,6 +556,43 @@ virXMLNodeContentString(xmlNodePtr node) }
+/** + * virXMLPropYesNo: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * @value: The returned virTristateBool value + * + * Convenience function to return value of a yes / no attribute. + * + * Returns 1 in case of success in which case @value is set, + * or 0 if the attribute is not present, + * or -1 and reports an error on failure. + */ +int +virXMLPropYesNo(xmlNodePtr node, const char* name, virTristateBool *value) +{ + g_autofree char *tmp = virXMLPropString(node, name); + + if (!tmp) + return 0; +
I was wondering if we can return -1 or 0 only by adding addition argument to the function, for example "bool mandatory". The condition here could be changed to this: if (!tmp) { if (mandatory) { virReportError(VIR_ERR_XML_ERROR, _(Missing mandatory attribute '%s' in element '%s'.), name, node->name); return -1; } return 0; } This would unify our error message for all the yes/no on/off options as well.
+ if (STREQ("yes", tmp)) { + *value = VIR_TRISTATE_BOOL_YES; + return 1; + } + + if (STREQ("no", tmp)) { + *value = VIR_TRISTATE_BOOL_NO; + return 1; + } + + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in node '%s': '%s'. Expected 'yes' or 'no'"),
s/node/element/ ? We should unify our terminology and I'm not sure if we prefer element or node. Pavel

Convenience function to return value of an on / off attribute. Does not use virTristateSwitchTypeFromString to disallow "default". Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/util/virxml.c | 37 +++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 3 +++ 2 files changed, 40 insertions(+) diff --git a/src/util/virxml.c b/src/util/virxml.c index 47e8414bd5..7c06530d25 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -593,6 +593,43 @@ virXMLPropYesNo(xmlNodePtr node, const char* name, virTristateBool *value) } +/** + * virXMLPropOnOff: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * @value: The returned virTristateSwitch value + * + * Convenience function to return value of an on / off attribute. + * + * Returns 1 in case of success in which case @value is set, + * or 0 if the attribute is not present, + * or -1 and reports an error on failure. + */ +int +virXMLPropOnOff(xmlNodePtr node, const char* name, virTristateSwitch *value) +{ + g_autofree char *tmp = virXMLPropString(node, name); + + if (!tmp) + return 0; + + if (STREQ("on", tmp)) { + *value = VIR_TRISTATE_SWITCH_ON; + return 1; + } + + if (STREQ("off", tmp)) { + *value = VIR_TRISTATE_SWITCH_OFF; + return 1; + } + + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in node '%s': '%s'. Expected 'on' or 'off'"), + name, node->name, tmp); + return -1; +} + + /** * virXPathBoolean: * @xpath: the XPath string to evaluate diff --git a/src/util/virxml.h b/src/util/virxml.h index 072948b3e2..d0577723ef 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -81,6 +81,9 @@ char * virXMLNodeContentString(xmlNodePtr node); int virXMLPropYesNo(xmlNodePtr node, const char *name, virTristateBool *value); +int virXMLPropOnOff(xmlNodePtr node, + const char *name, + virTristateSwitch *value); /* Internal function; prefer the macros below. */ xmlDocPtr virXMLParseHelper(int domcode, -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 47756ff0be..b6e505b384 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1441,10 +1441,9 @@ static int virDomainKeyWrapCipherDefParseXML(virDomainKeyWrapDefPtr keywrap, xmlNodePtr node) { - int state_type; + virTristateSwitch state_type = VIR_TRISTATE_SWITCH_ABSENT; int name_type; g_autofree char *name = NULL; - g_autofree char *state = NULL; if (!(name = virXMLPropString(node, "name"))) { virReportError(VIR_ERR_CONF_SYNTAX, "%s", @@ -1458,15 +1457,11 @@ virDomainKeyWrapCipherDefParseXML(virDomainKeyWrapDefPtr keywrap, return -1; } - if (!(state = virXMLPropString(node, "state"))) { - virReportError(VIR_ERR_CONF_SYNTAX, - _("missing state for cipher named %s"), name); + if (virXMLPropOnOff(node, "state", &state_type) < 0) return -1; - } - - if ((state_type = virTristateSwitchTypeFromString(state)) < 0) { + if (state_type == VIR_TRISTATE_SWITCH_ABSENT) { virReportError(VIR_ERR_CONF_SYNTAX, - _("%s is not a supported cipher state"), state); + _("missing state for cipher named %s"), name); return -1; } -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index b6e505b384..3a4b01ad1e 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1590,9 +1590,7 @@ static int virDomainVirtioOptionsParseXML(xmlNodePtr driver, virDomainVirtioOptionsPtr *virtio) { - int val; virDomainVirtioOptionsPtr res; - g_autofree char *str = NULL; if (*virtio || !driver) return 0; @@ -1601,34 +1599,14 @@ virDomainVirtioOptionsParseXML(xmlNodePtr driver, res = *virtio; - if ((str = virXMLPropString(driver, "iommu"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("invalid iommu value")); - return -1; - } - res->iommu = val; - } - VIR_FREE(str); + if (virXMLPropOnOff(driver, "iommu", &res->iommu) < 0) + return -1; - if ((str = virXMLPropString(driver, "ats"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("invalid ats value")); - return -1; - } - res->ats = val; - } - VIR_FREE(str); + if (virXMLPropOnOff(driver, "ats", &res->ats) < 0) + return -1; - if ((str = virXMLPropString(driver, "packed"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("invalid packed value")); - return -1; - } - res->packed = val; - } + if (virXMLPropOnOff(driver, "packed", &res->packed) < 0) + return -1; return 0; } -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 3a4b01ad1e..e1b2baf621 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -6620,8 +6620,6 @@ virDomainDeviceInfoParseXML(virDomainXMLOptionPtr xmlopt, xmlNodePtr boot = NULL; xmlNodePtr rom = NULL; int ret = -1; - g_autofree char *romenabled = NULL; - g_autofree char *rombar = NULL; g_autofree char *aliasStr = NULL; virDomainDeviceInfoClear(info); @@ -6673,18 +6671,17 @@ virDomainDeviceInfoParseXML(virDomainXMLOptionPtr xmlopt, } if (rom) { - if ((romenabled = virXMLPropString(rom, "enabled")) && - ((info->romenabled = virTristateBoolTypeFromString(romenabled)) <= 0)) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown rom enabled value '%s'"), romenabled); + virTristateBool romenabled = VIR_TRISTATE_BOOL_ABSENT; + virTristateSwitch rombar = VIR_TRISTATE_SWITCH_ABSENT; + + if (virXMLPropYesNo(rom, "enabled", &romenabled) < 0) goto cleanup; - } - if ((rombar = virXMLPropString(rom, "bar")) && - ((info->rombar = virTristateSwitchTypeFromString(rombar)) <= 0)) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown rom bar value '%s'"), rombar); + + if (virXMLPropOnOff(rom, "bar", &rombar) < 0) goto cleanup; - } + + info->romenabled = romenabled; + info->rombar = rombar; info->romfile = virXMLPropString(rom, "file"); if (info->romenabled == VIR_TRISTATE_BOOL_NO && -- 2.26.2

On 3/18/21 9:00 AM, Tim Wiederhake wrote:
Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)
Had to rebase this, because meanwhile I pushed another patch that touched this area.
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 3a4b01ad1e..e1b2baf621 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -6620,8 +6620,6 @@ virDomainDeviceInfoParseXML(virDomainXMLOptionPtr xmlopt, xmlNodePtr boot = NULL; xmlNodePtr rom = NULL; int ret = -1; - g_autofree char *romenabled = NULL; - g_autofree char *rombar = NULL; g_autofree char *aliasStr = NULL;
virDomainDeviceInfoClear(info); @@ -6673,18 +6671,17 @@ virDomainDeviceInfoParseXML(virDomainXMLOptionPtr xmlopt, }
if (rom) { - if ((romenabled = virXMLPropString(rom, "enabled")) && - ((info->romenabled = virTristateBoolTypeFromString(romenabled)) <= 0)) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown rom enabled value '%s'"), romenabled); + virTristateBool romenabled = VIR_TRISTATE_BOOL_ABSENT; + virTristateSwitch rombar = VIR_TRISTATE_SWITCH_ABSENT;
Okay, so the reason these variables are here is because virXMLPropYesNo()/virXMLPropOnOff() expects virTristateBool/virTristateSwitch variable, but info->romenabled and info->rombar are ints. They had to be, because previously we stored virTristateXXXTypeFromString() retval - which is type of int - directly into them. But I guess after these patches are merged we can finally switch them to the proper type. Same applies to the next patch (I haven't looked further). It's perfectly okay to do it in a follow up patch.
+ + if (virXMLPropYesNo(rom, "enabled", &romenabled) < 0) goto cleanup; - } - if ((rombar = virXMLPropString(rom, "bar")) && - ((info->rombar = virTristateSwitchTypeFromString(rombar)) <= 0)) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown rom bar value '%s'"), rombar); + + if (virXMLPropOnOff(rom, "bar", &rombar) < 0) goto cleanup; - } + + info->romenabled = romenabled; + info->rombar = rombar; info->romfile = virXMLPropString(rom, "file");
if (info->romenabled == VIR_TRISTATE_BOOL_NO &&
Michal

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index e1b2baf621..55920e7a7d 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -8303,9 +8303,9 @@ virDomainDiskSourceNetworkParse(xmlNodePtr node, { int tlsCfgVal; g_autofree char *protocol = NULL; - g_autofree char *haveTLS = NULL; g_autofree char *tlsCfg = NULL; g_autofree char *sslverifystr = NULL; + virTristateBool haveTLS = VIR_TRISTATE_BOOL_ABSENT; xmlNodePtr tmpnode; if (!(protocol = virXMLPropString(node, "protocol"))) { @@ -8327,12 +8327,9 @@ virDomainDiskSourceNetworkParse(xmlNodePtr node, return -1; } - if ((haveTLS = virXMLPropString(node, "tls")) && - (src->haveTLS = virTristateBoolTypeFromString(haveTLS)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("unknown disk source 'tls' setting '%s'"), haveTLS); - return -1; - } + if (virXMLPropYesNo(node, "tls", &haveTLS) < 0) + return -1; + src->haveTLS = haveTLS; if ((flags & VIR_DOMAIN_DEF_PARSE_STATUS) && (tlsCfg = virXMLPropString(node, "tlsFromConfig"))) { -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 55920e7a7d..e3e38ae30d 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -8428,7 +8428,7 @@ virDomainDiskSourceNVMeParse(xmlNodePtr node, g_autoptr(virStorageSourceNVMeDef) nvme = NULL; g_autofree char *type = NULL; g_autofree char *namespc = NULL; - g_autofree char *managed = NULL; + virTristateBool managed = VIR_TRISTATE_BOOL_ABSENT; xmlNodePtr address; nvme = g_new0(virStorageSourceNVMeDef, 1); @@ -8459,14 +8459,9 @@ virDomainDiskSourceNVMeParse(xmlNodePtr node, return -1; } - if ((managed = virXMLPropString(node, "managed"))) { - if ((nvme->managed = virTristateBoolTypeFromString(managed)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("malformed managed value '%s'"), - managed); - return -1; - } - } + if (virXMLPropYesNo(node, "managed", &managed) < 0) + return -1; + nvme->managed = managed; if (!(address = virXPathNode("./address", ctxt))) { virReportError(VIR_ERR_XML_ERROR, "%s", -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index e3e38ae30d..8dbf371e81 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -9126,6 +9126,9 @@ virDomainDiskDefDriverParseXML(virDomainDiskDefPtr def, xmlXPathContextPtr ctxt) { g_autofree char *tmp = NULL; + virTristateSwitch ioeventfd = VIR_TRISTATE_SWITCH_ABSENT; + virTristateSwitch event_idx = VIR_TRISTATE_SWITCH_ABSENT; + virTristateSwitch copy_on_read = VIR_TRISTATE_SWITCH_ABSENT; VIR_XPATH_NODE_AUTORESTORE(ctxt) ctxt->node = cur; @@ -9165,29 +9168,17 @@ virDomainDiskDefDriverParseXML(virDomainDiskDefPtr def, } VIR_FREE(tmp); - if ((tmp = virXMLPropString(cur, "ioeventfd")) && - (def->ioeventfd = virTristateSwitchTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown disk ioeventfd mode '%s'"), tmp); + if (virXMLPropOnOff(cur, "ioeventfd", &ioeventfd) < 0) return -1; - } - VIR_FREE(tmp); + def->ioeventfd = ioeventfd; - if ((tmp = virXMLPropString(cur, "event_idx")) && - (def->event_idx = virTristateSwitchTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown disk event_idx mode '%s'"), tmp); + if (virXMLPropOnOff(cur, "event_idx", &event_idx) < 0) return -1; - } - VIR_FREE(tmp); + def->event_idx = event_idx; - if ((tmp = virXMLPropString(cur, "copy_on_read")) && - (def->copy_on_read = virTristateSwitchTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown disk copy_on_read mode '%s'"), tmp); + if (virXMLPropOnOff(cur, "copy_on_read", ©_on_read) < 0) return -1; - } - VIR_FREE(tmp); + def->copy_on_read = copy_on_read; if ((tmp = virXMLPropString(cur, "discard")) && (def->discard = virDomainDiskDiscardTypeFromString(tmp)) <= 0) { -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 8dbf371e81..b8dfc77cfc 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10474,8 +10474,8 @@ virDomainActualNetDefParseXML(xmlNodePtr node, g_autofree char *type = NULL; g_autofree char *mode = NULL; g_autofree char *addrtype = NULL; - g_autofree char *trustGuestRxFilters = NULL; g_autofree char *macTableManager = NULL; + virTristateBool trustGuestRxFilters = VIR_TRISTATE_BOOL_ABSENT; actual = g_new0(virDomainActualNetDef, 1); @@ -10502,15 +10502,9 @@ virDomainActualNetDefParseXML(xmlNodePtr node, goto error; } - trustGuestRxFilters = virXMLPropString(node, "trustGuestRxFilters"); - if (trustGuestRxFilters && - ((actual->trustGuestRxFilters - = virTristateBoolTypeFromString(trustGuestRxFilters)) <= 0)) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown trustGuestRxFilters value '%s'"), - trustGuestRxFilters); + if (virXMLPropYesNo(node, "trustGuestRxFilters", &trustGuestRxFilters) < 0) goto error; - } + actual->trustGuestRxFilters = trustGuestRxFilters; virtPortNode = virXPathNode("./virtualport", ctxt); if (virtPortNode) { -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index b8dfc77cfc..52e1668010 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10658,7 +10658,6 @@ virDomainChrSourceReconnectDefParseXML(virDomainChrSourceReconnectDefPtr def, xmlNodePtr node, xmlXPathContextPtr ctxt) { - int tmpVal; VIR_XPATH_NODE_AUTORESTORE(ctxt) xmlNodePtr cur; g_autofree char *tmp = NULL; @@ -10666,16 +10665,8 @@ virDomainChrSourceReconnectDefParseXML(virDomainChrSourceReconnectDefPtr def, ctxt->node = node; if ((cur = virXPathNode("./reconnect", ctxt))) { - if ((tmp = virXMLPropString(cur, "enabled"))) { - if ((tmpVal = virTristateBoolTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_XML_ERROR, - _("invalid reconnect enabled value: '%s'"), - tmp); - return -1; - } - def->enabled = tmpVal; - VIR_FREE(tmp); - } + if (virXMLPropYesNo(cur, "enabled", &def->enabled) < 0) + return -1; if (def->enabled == VIR_TRISTATE_BOOL_YES) { if ((tmp = virXMLPropString(cur, "timeout"))) { -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 166 +++++++++-------------------------------- 1 file changed, 37 insertions(+), 129 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 52e1668010..54c647bfd5 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10759,7 +10759,6 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt, g_autofree char *queues = NULL; g_autofree char *rx_queue_size = NULL; g_autofree char *tx_queue_size = NULL; - g_autofree char *str = NULL; g_autofree char *filter = NULL; g_autofree char *internal = NULL; g_autofree char *mode = NULL; @@ -10769,8 +10768,8 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt, g_autofree char *vhostuser_mode = NULL; g_autofree char *vhostuser_path = NULL; g_autofree char *vhostuser_type = NULL; - g_autofree char *trustGuestRxFilters = NULL; g_autofree char *vhost_path = NULL; + virTristateBool trustGuestRxFilters = VIR_TRISTATE_BOOL_ABSENT; const char *prefix = xmlopt ? xmlopt->config.netPrefix : NULL; if (!(def = virDomainNetDefNew(xmlopt))) @@ -10789,15 +10788,9 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt, def->type = VIR_DOMAIN_NET_TYPE_USER; } - trustGuestRxFilters = virXMLPropString(node, "trustGuestRxFilters"); - if (trustGuestRxFilters && - ((def->trustGuestRxFilters - = virTristateBoolTypeFromString(trustGuestRxFilters)) <= 0)) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown trustGuestRxFilters value '%s'"), - trustGuestRxFilters); + if (virXMLPropYesNo(node, "trustGuestRxFilters", &trustGuestRxFilters) < 0) goto error; - } + def->trustGuestRxFilters = trustGuestRxFilters; cur = node->children; while (cur != NULL) { @@ -11367,128 +11360,43 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt, } if ((tmpNode = virXPathNode("./driver/host", ctxt))) { - if ((str = virXMLPropString(tmpNode, "csum"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown host csum mode '%s'"), - str); - goto error; - } - def->driver.virtio.host.csum = val; - } - VIR_FREE(str); - if ((str = virXMLPropString(tmpNode, "gso"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown host gso mode '%s'"), - str); - goto error; - } - def->driver.virtio.host.gso = val; - } - VIR_FREE(str); - if ((str = virXMLPropString(tmpNode, "tso4"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown host tso4 mode '%s'"), - str); - goto error; - } - def->driver.virtio.host.tso4 = val; - } - VIR_FREE(str); - if ((str = virXMLPropString(tmpNode, "tso6"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown host tso6 mode '%s'"), - str); - goto error; - } - def->driver.virtio.host.tso6 = val; - } - VIR_FREE(str); - if ((str = virXMLPropString(tmpNode, "ecn"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown host ecn mode '%s'"), - str); - goto error; - } - def->driver.virtio.host.ecn = val; - } - VIR_FREE(str); - if ((str = virXMLPropString(tmpNode, "ufo"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown host ufo mode '%s'"), - str); - goto error; - } - def->driver.virtio.host.ufo = val; - } - VIR_FREE(str); - if ((str = virXMLPropString(tmpNode, "mrg_rxbuf"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown host mrg_rxbuf mode '%s'"), - str); - goto error; - } - def->driver.virtio.host.mrg_rxbuf = val; - } - VIR_FREE(str); + if (virXMLPropOnOff(tmpNode, "csum", &def->driver.virtio.host.csum) < 0) + goto error; + + if (virXMLPropOnOff(tmpNode, "gso", &def->driver.virtio.host.gso) < 0) + goto error; + + if (virXMLPropOnOff(tmpNode, "tso4", &def->driver.virtio.host.tso4) < 0) + goto error; + + if (virXMLPropOnOff(tmpNode, "tso6", &def->driver.virtio.host.tso6) < 0) + goto error; + + if (virXMLPropOnOff(tmpNode, "ecn", &def->driver.virtio.host.ecn) < 0) + goto error; + + if (virXMLPropOnOff(tmpNode, "ufo", &def->driver.virtio.host.ufo) < 0) + goto error; + + if (virXMLPropOnOff(tmpNode, "mrg_rxbuf", &def->driver.virtio.host.mrg_rxbuf) < 0) + goto error; } if ((tmpNode = virXPathNode("./driver/guest", ctxt))) { - if ((str = virXMLPropString(tmpNode, "csum"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown guest csum mode '%s'"), - str); - goto error; - } - def->driver.virtio.guest.csum = val; - } - VIR_FREE(str); - if ((str = virXMLPropString(tmpNode, "tso4"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown guest tso4 mode '%s'"), - str); - goto error; - } - def->driver.virtio.guest.tso4 = val; - } - VIR_FREE(str); - if ((str = virXMLPropString(tmpNode, "tso6"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown guest tso6 mode '%s'"), - str); - goto error; - } - def->driver.virtio.guest.tso6 = val; - } - VIR_FREE(str); - if ((str = virXMLPropString(tmpNode, "ecn"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown guest ecn mode '%s'"), - str); - goto error; - } - def->driver.virtio.guest.ecn = val; - } - VIR_FREE(str); - if ((str = virXMLPropString(tmpNode, "ufo"))) { - if ((val = virTristateSwitchTypeFromString(str)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown guest ufo mode '%s'"), - str); - goto error; - } - def->driver.virtio.guest.ufo = val; - } + if (virXMLPropOnOff(tmpNode, "csum", &def->driver.virtio.guest.csum) < 0) + goto error; + + if (virXMLPropOnOff(tmpNode, "tso4", &def->driver.virtio.guest.tso4) < 0) + goto error; + + if (virXMLPropOnOff(tmpNode, "tso6", &def->driver.virtio.guest.tso6) < 0) + goto error; + + if (virXMLPropOnOff(tmpNode, "ecn", &def->driver.virtio.guest.ecn) < 0) + goto error; + + if (virXMLPropOnOff(tmpNode, "ufo", &def->driver.virtio.guest.ufo) < 0) + goto error; } def->backend.vhost = g_steal_pointer(&vhost_path); } -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 54c647bfd5..1cec2edde7 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -11724,23 +11724,18 @@ virDomainChrSourceDefParseTCP(virDomainChrSourceDefPtr def, int mode; int tmpVal; g_autofree char *tmp = NULL; + virTristateBool haveTLS = VIR_TRISTATE_BOOL_ABSENT; if ((mode = virDomainChrSourceDefParseMode(source)) < 0) return -1; + if (virXMLPropYesNo(source, "tls", &haveTLS) < 0) + return -1; + def->data.tcp.listen = mode == VIR_DOMAIN_CHR_SOURCE_MODE_BIND; def->data.tcp.host = virXMLPropString(source, "host"); def->data.tcp.service = virXMLPropString(source, "service"); - - if ((tmp = virXMLPropString(source, "tls"))) { - if ((def->data.tcp.haveTLS = virTristateBoolTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("unknown chardev 'tls' setting '%s'"), - tmp); - return -1; - } - VIR_FREE(tmp); - } + def->data.tcp.haveTLS = haveTLS; if ((flags & VIR_DOMAIN_DEF_PARSE_STATUS) && (tmp = virXMLPropString(source, "tlsFromConfig"))) { -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 1cec2edde7..d9330f9ee7 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -11808,17 +11808,13 @@ static int virDomainChrSourceDefParseFile(virDomainChrSourceDefPtr def, xmlNodePtr source) { - g_autofree char *append = NULL; - - def->data.file.path = virXMLPropString(source, "path"); + virTristateSwitch append = VIR_TRISTATE_SWITCH_ABSENT; - if ((append = virXMLPropString(source, "append")) && - (def->data.file.append = virTristateSwitchTypeFromString(append)) <= 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Invalid append attribute value '%s'"), - append); + if (virXMLPropOnOff(source, "append", &append) < 0) return -1; - } + + def->data.file.path = virXMLPropString(source, "path"); + def->data.file.append = append; return 0; } -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index d9330f9ee7..b4928a94c2 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -11845,17 +11845,13 @@ static int virDomainChrSourceDefParseLog(virDomainChrSourceDefPtr def, xmlNodePtr log) { - g_autofree char *append = NULL; + virTristateSwitch logappend = VIR_TRISTATE_SWITCH_ABSENT; - def->logfile = virXMLPropString(log, "file"); - - if ((append = virXMLPropString(log, "append")) && - (def->logappend = virTristateSwitchTypeFromString(append)) <= 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Invalid append attribute value '%s'"), - append); + if (virXMLPropOnOff(log, "append", &logappend) < 0) return -1; - } + + def->logfile = virXMLPropString(log, "file"); + def->logappend = logappend; return 0; } -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index b4928a94c2..756a35f33b 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13077,7 +13077,6 @@ virDomainGraphicsDefParseXMLVNC(virDomainGraphicsDefPtr def, g_autofree char *websocketGenerated = virXMLPropString(node, "websocketGenerated"); g_autofree char *sharePolicy = virXMLPropString(node, "sharePolicy"); g_autofree char *autoport = virXMLPropString(node, "autoport"); - g_autofree char *powerControl = virXMLPropString(node, "powerControl"); xmlNodePtr audioNode; VIR_XPATH_NODE_AUTORESTORE(ctxt) @@ -13136,15 +13135,8 @@ virDomainGraphicsDefParseXMLVNC(virDomainGraphicsDefPtr def, } } - if (powerControl) { - int powerControlVal = virTristateBoolTypeFromString(powerControl); - if (powerControlVal < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("cannot parse vnc power control '%s'"), powerControl); - return -1; - } - def->data.vnc.powerControl = powerControlVal; - } + if ((virXMLPropYesNo(node, "powerControl", &def->data.vnc.powerControl)) < 0) + return -1; def->data.vnc.keymap = virXMLPropString(node, "keymap"); -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 756a35f33b..011745e73a 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13172,10 +13172,8 @@ virDomainGraphicsDefParseXMLSDL(virDomainGraphicsDefPtr def, xmlXPathContextPtr ctxt) { VIR_XPATH_NODE_AUTORESTORE(ctxt) - int enableVal; xmlNodePtr glNode; g_autofree char *fullscreen = virXMLPropString(node, "fullscreen"); - g_autofree char *enable = NULL; ctxt->node = node; @@ -13194,20 +13192,14 @@ virDomainGraphicsDefParseXMLSDL(virDomainGraphicsDefPtr def, glNode = virXPathNode("./gl", ctxt); if (glNode) { - enable = virXMLPropString(glNode, "enable"); - if (!enable) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("sdl gl element missing enable")); + if (virXMLPropYesNo(glNode, "enable", &def->data.sdl.gl) < 0) return -1; - } - enableVal = virTristateBoolTypeFromString(enable); - if (enableVal < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown enable value '%s'"), enable); + if (def->data.sdl.gl == VIR_TRISTATE_BOOL_ABSENT) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("sdl gl element missing enable")); return -1; } - def->data.sdl.gl = enableVal; } return 0; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 66 +++++++++++------------------------------- 1 file changed, 17 insertions(+), 49 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 011745e73a..360528ef1e 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13429,23 +13429,18 @@ virDomainGraphicsDefParseXMLSpice(virDomainGraphicsDefPtr def, def->data.spice.zlib = compressionVal; } else if (virXMLNodeNameEqual(cur, "playback")) { - int compressionVal; - g_autofree char *compression = virXMLPropString(cur, "compression"); + virTristateSwitch compression = VIR_TRISTATE_SWITCH_ABSENT; - if (!compression) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("spice playback missing compression")); + if (virXMLPropOnOff(cur, "compression", &compression) < 0) return -1; - } - if ((compressionVal = - virTristateSwitchTypeFromString(compression)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("unknown spice playback compression")); + if (compression == VIR_TRISTATE_SWITCH_ABSENT) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("spice playback missing compression")); return -1; } - def->data.spice.playback = compressionVal; + def->data.spice.playback = compression; } else if (virXMLNodeNameEqual(cur, "streaming")) { int modeVal; g_autofree char *mode = virXMLPropString(cur, "mode"); @@ -13464,62 +13459,35 @@ virDomainGraphicsDefParseXMLSpice(virDomainGraphicsDefPtr def, def->data.spice.streaming = modeVal; } else if (virXMLNodeNameEqual(cur, "clipboard")) { - int copypasteVal; - g_autofree char *copypaste = virXMLPropString(cur, "copypaste"); + if (virXMLPropYesNo(cur, "copypaste", &def->data.spice.copypaste) < 0) + return -1; - if (!copypaste) { + if (def->data.spice.copypaste == VIR_TRISTATE_BOOL_ABSENT) { virReportError(VIR_ERR_XML_ERROR, "%s", _("spice clipboard missing copypaste")); return -1; } - - if ((copypasteVal = - virTristateBoolTypeFromString(copypaste)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown copypaste value '%s'"), copypaste); - return -1; - } - - def->data.spice.copypaste = copypasteVal; } else if (virXMLNodeNameEqual(cur, "filetransfer")) { - int enableVal; - g_autofree char *enable = virXMLPropString(cur, "enable"); + if (virXMLPropYesNo(cur, "enable", &def->data.spice.filetransfer) < 0) + return -1; - if (!enable) { + if (def->data.spice.filetransfer == VIR_TRISTATE_BOOL_ABSENT) { virReportError(VIR_ERR_XML_ERROR, "%s", _("spice filetransfer missing enable")); return -1; } + } else if (virXMLNodeNameEqual(cur, "gl")) { + def->data.spice.rendernode = virXMLPropString(cur, + "rendernode"); - if ((enableVal = - virTristateBoolTypeFromString(enable)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown enable value '%s'"), enable); + if (virXMLPropYesNo(cur, "enable", &def->data.spice.gl) < 0) return -1; - } - - def->data.spice.filetransfer = enableVal; - } else if (virXMLNodeNameEqual(cur, "gl")) { - int enableVal; - g_autofree char *enable = virXMLPropString(cur, "enable"); - g_autofree char *rendernode = virXMLPropString(cur, "rendernode"); - if (!enable) { + if (def->data.spice.gl == VIR_TRISTATE_BOOL_ABSENT) { virReportError(VIR_ERR_XML_ERROR, "%s", _("spice gl element missing enable")); return -1; } - - if ((enableVal = - virTristateBoolTypeFromString(enable)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown enable value '%s'"), enable); - return -1; - } - - def->data.spice.gl = enableVal; - def->data.spice.rendernode = g_steal_pointer(&rendernode); - } else if (virXMLNodeNameEqual(cur, "mouse")) { int modeVal; g_autofree char *mode = virXMLPropString(cur, "mode"); -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 360528ef1e..4e7f5031e7 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13772,8 +13772,6 @@ virDomainAudioCommonParse(virDomainAudioIOCommonPtr def, xmlNodePtr node, xmlXPathContextPtr ctxt) { - g_autofree char *mixingEngine = virXMLPropString(node, "mixingEngine"); - g_autofree char *fixedSettings = virXMLPropString(node, "fixedSettings"); g_autofree char *voices = virXMLPropString(node, "voices"); g_autofree char *bufferLength = virXMLPropString(node, "bufferLength"); xmlNodePtr settings; @@ -13782,21 +13780,11 @@ virDomainAudioCommonParse(virDomainAudioIOCommonPtr def, ctxt->node = node; settings = virXPathNode("./settings", ctxt); - if (mixingEngine && - ((def->mixingEngine = - virTristateBoolTypeFromString(mixingEngine)) <= 0)) { - virReportError(VIR_ERR_XML_ERROR, - _("unknown 'mixingEngine' value '%s'"), mixingEngine); + if (virXMLPropYesNo(node, "mixingEngine", &def->mixingEngine) < 0) return -1; - } - if (fixedSettings && - ((def->fixedSettings = - virTristateBoolTypeFromString(fixedSettings)) <= 0)) { - virReportError(VIR_ERR_XML_ERROR, - _("unknown 'fixedSettings' value '%s'"), fixedSettings); + if (virXMLPropYesNo(node, "fixedSettings", &def->fixedSettings) < 0) return -1; - } if (def->fixedSettings == VIR_TRISTATE_BOOL_YES && def->mixingEngine != VIR_TRISTATE_BOOL_YES) { -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 4e7f5031e7..b9ee1e2de7 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13878,19 +13878,12 @@ static int virDomainAudioJackParse(virDomainAudioIOJackPtr def, xmlNodePtr node) { - g_autofree char *exactName = virXMLPropString(node, "exactName"); - def->serverName = virXMLPropString(node, "serverName"); def->clientName = virXMLPropString(node, "clientName"); def->connectPorts = virXMLPropString(node, "connectPorts"); - if (exactName && - ((def->exactName = - virTristateBoolTypeFromString(exactName)) <= 0)) { - virReportError(VIR_ERR_XML_ERROR, - _("unknown 'exactName' value '%s'"), exactName); + if (virXMLPropYesNo(node, "exactName", &def->exactName) < 0) return -1; - } return 0; } -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index b9ee1e2de7..6559c3d2f8 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13893,18 +13893,12 @@ static int virDomainAudioOSSParse(virDomainAudioIOOSSPtr def, xmlNodePtr node) { - g_autofree char *tryPoll = virXMLPropString(node, "tryPoll"); g_autofree char *bufferCount = virXMLPropString(node, "bufferCount"); def->dev = virXMLPropString(node, "dev"); - if (tryPoll && - ((def->tryPoll = - virTristateBoolTypeFromString(tryPoll)) <= 0)) { - virReportError(VIR_ERR_XML_ERROR, - _("unknown 'tryPoll' value '%s'"), tryPoll); + if (virXMLPropYesNo(node, "tryPoll", &def->tryPoll) < 0) return -1; - } if (bufferCount && virStrToLong_ui(bufferCount, NULL, 10, -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 6559c3d2f8..fe35a3960e 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -14025,23 +14025,13 @@ virDomainAudioDefParseXML(virDomainXMLOptionPtr xmlopt G_GNUC_UNUSED, break; case VIR_DOMAIN_AUDIO_TYPE_OSS: { - g_autofree char *tryMMap = virXMLPropString(node, "tryMMap"); - g_autofree char *exclusive = virXMLPropString(node, "exclusive"); g_autofree char *dspPolicy = virXMLPropString(node, "dspPolicy"); - if (tryMMap && ((def->backend.oss.tryMMap = - virTristateBoolTypeFromString(tryMMap)) <= 0)) { - virReportError(VIR_ERR_XML_ERROR, - _("unknown 'tryMMap' value '%s'"), tryMMap); + if (virXMLPropYesNo(node, "tryMMap", &def->backend.oss.tryMMap) < 0) goto error; - } - if (exclusive && ((def->backend.oss.exclusive = - virTristateBoolTypeFromString(exclusive)) <= 0)) { - virReportError(VIR_ERR_XML_ERROR, - _("unknown 'exclusive' value '%s'"), exclusive); + if (virXMLPropYesNo(node, "exclusive", &def->backend.oss.exclusive) < 0) goto error; - } if (dspPolicy) { if (virStrToLong_i(dspPolicy, NULL, 10, -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index fe35a3960e..0c4f7ab879 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -14264,8 +14264,8 @@ virDomainMemballoonDefParseXML(virDomainXMLOptionPtr xmlopt, VIR_XPATH_NODE_AUTORESTORE(ctxt) unsigned int period = 0; g_autofree char *model = NULL; - g_autofree char *freepage_reporting = NULL; - g_autofree char *deflate = NULL; + virTristateSwitch autodeflate = VIR_TRISTATE_SWITCH_ABSENT; + virTristateSwitch free_page_reporting = VIR_TRISTATE_SWITCH_ABSENT; def = g_new0(virDomainMemballoonDef, 1); @@ -14282,19 +14282,13 @@ virDomainMemballoonDefParseXML(virDomainXMLOptionPtr xmlopt, goto error; } - if ((deflate = virXMLPropString(node, "autodeflate")) && - (def->autodeflate = virTristateSwitchTypeFromString(deflate)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid autodeflate attribute value '%s'"), deflate); + if (virXMLPropOnOff(node, "autodeflate", &autodeflate) < 0) goto error; - } + def->autodeflate = autodeflate; - if ((freepage_reporting = virXMLPropString(node, "freePageReporting")) && - (def->free_page_reporting = virTristateSwitchTypeFromString(freepage_reporting)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid freePageReporting attribute value '%s'"), freepage_reporting); + if (virXMLPropOnOff(node, "freePageReporting", &free_page_reporting) < 0) goto error; - } + def->free_page_reporting = free_page_reporting; ctxt->node = node; if (virXPathUInt("string(./stats/@period)", ctxt, &period) < -1) { -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 0c4f7ab879..a1aefdf0bf 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -14413,17 +14413,8 @@ virDomainShmemDefParseXML(virDomainXMLOptionPtr xmlopt, } VIR_FREE(tmp); - if ((tmp = virXMLPropString(msi, "ioeventfd"))) { - int val; - - if ((val = virTristateSwitchTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("invalid msi ioeventfd setting for shmem: '%s'"), - tmp); - goto cleanup; - } - def->msi.ioeventfd = val; - } + if (virXMLPropOnOff(msi, "ioeventfd", &def->msi.ioeventfd) < 0) + goto cleanup; } /* msi option is only relevant with a server */ -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index a1aefdf0bf..c020bf9124 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -15496,7 +15496,7 @@ virDomainPerfEventDefParseXML(virDomainPerfDefPtr perf, { int event; g_autofree char *name = NULL; - g_autofree char *enabled = NULL; + virTristateBool enabled = VIR_TRISTATE_BOOL_ABSENT; if (!(name = virXMLPropString(node, "name"))) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing perf event name")); @@ -15515,16 +15515,12 @@ virDomainPerfEventDefParseXML(virDomainPerfDefPtr perf, return -1; } - if (!(enabled = virXMLPropString(node, "enabled"))) { - virReportError(VIR_ERR_XML_ERROR, - _("missing state of perf event '%s'"), name); + if (virXMLPropYesNo(node, "enabled", &enabled) < 0) return -1; - } - if ((perf->events[event] = virTristateBoolTypeFromString(enabled)) < 0) { + if ((perf->events[event] = enabled) == VIR_TRISTATE_BOOL_ABSENT) { virReportError(VIR_ERR_XML_ERROR, - _("invalid state '%s' of perf event '%s'"), - enabled, name); + _("missing state of perf event '%s'"), name); return -1; } -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index c020bf9124..d4ca60f98c 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -15764,16 +15764,8 @@ virDomainMemoryDefParseXML(virDomainXMLOptionPtr xmlopt, } VIR_FREE(tmp); - if ((tmp = virXMLPropString(memdevNode, "discard"))) { - if ((val = virTristateBoolTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("invalid discard value '%s'"), tmp); - goto error; - } - - def->discard = val; - } - VIR_FREE(tmp); + if (virXMLPropYesNo(memdevNode, "discard", &def->discard) < 0) + goto error; /* Extract NVDIMM UUID. */ if (def->model == VIR_DOMAIN_MEMORY_MODEL_NVDIMM && -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index d4ca60f98c..2969cf0d88 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -15835,40 +15835,17 @@ virDomainIOMMUDefParseXML(xmlNodePtr node, iommu->model = val; if ((driver = virXPathNode("./driver", ctxt))) { - VIR_FREE(tmp); - if ((tmp = virXMLPropString(driver, "intremap"))) { - if ((val = virTristateSwitchTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_XML_ERROR, _("unknown intremap value: %s"), tmp); - return NULL; - } - iommu->intremap = val; - } + if (virXMLPropOnOff(driver, "intremap", &iommu->intremap) < 0) + return NULL; - VIR_FREE(tmp); - if ((tmp = virXMLPropString(driver, "caching_mode"))) { - if ((val = virTristateSwitchTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_XML_ERROR, _("unknown caching_mode value: %s"), tmp); - return NULL; - } - iommu->caching_mode = val; - } - VIR_FREE(tmp); - if ((tmp = virXMLPropString(driver, "iotlb"))) { - if ((val = virTristateSwitchTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_XML_ERROR, _("unknown iotlb value: %s"), tmp); - return NULL; - } - iommu->iotlb = val; - } + if (virXMLPropOnOff(driver, "caching_mode", &iommu->caching_mode) < 0) + return NULL; - VIR_FREE(tmp); - if ((tmp = virXMLPropString(driver, "eim"))) { - if ((val = virTristateSwitchTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_XML_ERROR, _("unknown eim value: %s"), tmp); - return NULL; - } - iommu->eim = val; - } + if (virXMLPropOnOff(driver, "iotlb", &iommu->iotlb) < 0) + return NULL; + + if (virXMLPropOnOff(driver, "eim", &iommu->eim) < 0) + return NULL; VIR_FREE(tmp); if ((tmp = virXMLPropString(driver, "aw_bits"))) { -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 2969cf0d88..39f171ba74 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -15900,17 +15900,8 @@ virDomainVsockDefParseXML(virDomainXMLOptionPtr xmlopt, } } - VIR_FREE(tmp); - if ((tmp = virXMLPropString(cid, "auto"))) { - val = virTristateBoolTypeFromString(tmp); - if (val <= 0) { - virReportError(VIR_ERR_XML_DETAIL, - _("'auto' attribute can be 'yes' or 'no': %s"), - tmp); - return NULL; - } - vsock->auto_cid = val; - } + if (virXMLPropYesNo(cid, "auto", &vsock->auto_cid) < 0) + return NULL; } if (virDomainDeviceInfoParseXML(xmlopt, node, &vsock->info, flags) < 0) -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 138 ++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 90 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 39f171ba74..4f020714bc 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -18292,6 +18292,7 @@ virDomainFeaturesDefParse(virDomainDefPtr def, for (i = 0; i < n; i++) { g_autofree char *tmp = NULL; + virTristateSwitch triSwitch = VIR_TRISTATE_SWITCH_ABSENT; int val = virDomainFeatureTypeFromString((const char *)nodes[i]->name); if (val < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, @@ -18301,16 +18302,9 @@ virDomainFeaturesDefParse(virDomainDefPtr def, switch ((virDomainFeature) val) { case VIR_DOMAIN_FEATURE_APIC: - if ((tmp = virXPathString("string(./features/apic/@eoi)", ctxt))) { - int eoi; - if ((eoi = virTristateSwitchTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown value for attribute eoi: '%s'"), - tmp); - return -1; - } - def->apic_eoi = eoi; - } + if (virXMLPropOnOff(nodes[i], "eoi", &triSwitch) < 0) + return -1; + def->apic_eoi = triSwitch; G_GNUC_FALLTHROUGH; case VIR_DOMAIN_FEATURE_ACPI: case VIR_DOMAIN_FEATURE_PAE: @@ -18342,16 +18336,10 @@ virDomainFeaturesDefParse(virDomainDefPtr def, case VIR_DOMAIN_FEATURE_PVSPINLOCK: case VIR_DOMAIN_FEATURE_VMPORT: case VIR_DOMAIN_FEATURE_SMM: - if ((tmp = virXMLPropString(nodes[i], "state"))) { - if ((def->features[val] = virTristateSwitchTypeFromString(tmp)) == -1) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown state attribute '%s' of feature '%s'"), - tmp, virDomainFeatureTypeToString(val)); - return -1; - } - } else { + if (virXMLPropOnOff(nodes[i], "state", &triSwitch) < 0) + return -1; + if ((def->features[val] = triSwitch) == VIR_TRISTATE_SWITCH_ABSENT) def->features[val] = VIR_TRISTATE_SWITCH_ON; - } break; case VIR_DOMAIN_FEATURE_GIC: @@ -18459,15 +18447,11 @@ virDomainFeaturesDefParse(virDomainDefPtr def, case VIR_DOMAIN_FEATURE_HTM: case VIR_DOMAIN_FEATURE_NESTED_HV: case VIR_DOMAIN_FEATURE_CCF_ASSIST: - if (!(tmp = virXMLPropString(nodes[i], "state"))) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("missing state attribute '%s' of feature '%s'"), - tmp, virDomainFeatureTypeToString(val)); + if (virXMLPropOnOff(nodes[i], "state", &triSwitch) < 0) return -1; - } - if ((def->features[val] = virTristateSwitchTypeFromString(tmp)) < 0) { + if ((def->features[val] = triSwitch) == VIR_TRISTATE_SWITCH_ABSENT) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown state attribute '%s' of feature '%s'"), + _("missing state attribute '%s' of feature '%s'"), tmp, virDomainFeatureTypeToString(val)); return -1; } @@ -18482,13 +18466,12 @@ virDomainFeaturesDefParse(virDomainDefPtr def, if (def->features[VIR_DOMAIN_FEATURE_HYPERV] == VIR_TRISTATE_SWITCH_ON) { int feature; - int value; + virTristateSwitch state = VIR_TRISTATE_SWITCH_ABSENT; xmlNodePtr node = ctxt->node; if ((n = virXPathNodeSet("./features/hyperv/*", ctxt, &nodes)) < 0) return -1; for (i = 0; i < n; i++) { - g_autofree char *tmp = NULL; feature = virDomainHypervTypeFromString((const char *)nodes[i]->name); if (feature < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, @@ -18499,7 +18482,10 @@ virDomainFeaturesDefParse(virDomainDefPtr def, ctxt->node = nodes[i]; - if (!(tmp = virXMLPropString(nodes[i], "state"))) { + if (virXMLPropOnOff(nodes[i], "state", &state) < 0) + return -1; + + if (state == VIR_TRISTATE_SWITCH_ABSENT) { virReportError(VIR_ERR_XML_ERROR, _("missing 'state' attribute for " "HyperV Enlightenment feature '%s'"), @@ -18507,15 +18493,7 @@ virDomainFeaturesDefParse(virDomainDefPtr def, return -1; } - if ((value = virTristateSwitchTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid value of state argument " - "for HyperV Enlightenment feature '%s'"), - nodes[i]->name); - return -1; - } - - def->hyperv_features[feature] = value; + def->hyperv_features[feature] = state; switch ((virDomainHyperv) feature) { case VIR_DOMAIN_HYPERV_RELAXED: @@ -18533,7 +18511,7 @@ virDomainFeaturesDefParse(virDomainDefPtr def, break; case VIR_DOMAIN_HYPERV_SPINLOCKS: - if (value != VIR_TRISTATE_SWITCH_ON) + if (state != VIR_TRISTATE_SWITCH_ON) break; if (virXPathUInt("string(./@retries)", ctxt, @@ -18552,7 +18530,7 @@ virDomainFeaturesDefParse(virDomainDefPtr def, break; case VIR_DOMAIN_HYPERV_VENDOR_ID: - if (value != VIR_TRISTATE_SWITCH_ON) + if (state != VIR_TRISTATE_SWITCH_ON) break; if (!(def->hyperv_vendor_id = virXMLPropString(nodes[i], @@ -18589,13 +18567,12 @@ virDomainFeaturesDefParse(virDomainDefPtr def, } if (def->hyperv_features[VIR_DOMAIN_HYPERV_STIMER] == VIR_TRISTATE_SWITCH_ON) { - int value; + virTristateSwitch state = VIR_TRISTATE_SWITCH_ABSENT; + if ((n = virXPathNodeSet("./features/hyperv/stimer/*", ctxt, &nodes)) < 0) return -1; for (i = 0; i < n; i++) { - g_autofree char *tmp = NULL; - if (STRNEQ((const char *)nodes[i]->name, "direct")) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unsupported Hyper-V stimer feature: %s"), @@ -18603,33 +18580,28 @@ virDomainFeaturesDefParse(virDomainDefPtr def, return -1; } - if (!(tmp = virXMLPropString(nodes[i], "state"))) { + if (virXMLPropOnOff(nodes[i], "state", &state) < 0) + return -1; + def->hyperv_stimer_direct = state; + + if (state == VIR_TRISTATE_SWITCH_ABSENT) { virReportError(VIR_ERR_XML_ERROR, _("missing 'state' attribute for " "Hyper-V stimer '%s' feature"), "direct"); return -1; } - - if ((value = virTristateSwitchTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid value of state argument " - "for Hyper-V stimer '%s' feature"), "direct"); - return -1; - } - - def->hyperv_stimer_direct = value; } VIR_FREE(nodes); } if (def->features[VIR_DOMAIN_FEATURE_KVM] == VIR_TRISTATE_SWITCH_ON) { int feature; - int value; + if ((n = virXPathNodeSet("./features/kvm/*", ctxt, &nodes)) < 0) return -1; for (i = 0; i < n; i++) { - g_autofree char *tmp = NULL; + virTristateSwitch state = VIR_TRISTATE_SWITCH_ABSENT; feature = virDomainKVMTypeFromString((const char *)nodes[i]->name); if (feature < 0) { @@ -18643,7 +18615,11 @@ virDomainFeaturesDefParse(virDomainDefPtr def, case VIR_DOMAIN_KVM_HIDDEN: case VIR_DOMAIN_KVM_DEDICATED: case VIR_DOMAIN_KVM_POLLCONTROL: - if (!(tmp = virXMLPropString(nodes[i], "state"))) { + if (virXMLPropOnOff(nodes[i], "state", &state) < 0) + return -1; + def->kvm_features[feature] = state; + + if (state == VIR_TRISTATE_SWITCH_ABSENT) { virReportError(VIR_ERR_XML_ERROR, _("missing 'state' attribute for " "KVM feature '%s'"), @@ -18651,15 +18627,6 @@ virDomainFeaturesDefParse(virDomainDefPtr def, return -1; } - if ((value = virTristateSwitchTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid value of state argument " - "for KVM feature '%s'"), - nodes[i]->name); - return -1; - } - - def->kvm_features[feature] = value; break; /* coverity[dead_error_begin] */ @@ -18672,14 +18639,13 @@ virDomainFeaturesDefParse(virDomainDefPtr def, if (def->features[VIR_DOMAIN_FEATURE_XEN] == VIR_TRISTATE_SWITCH_ON) { int feature; - int value; g_autofree char *ptval = NULL; - g_autofree char *tmp = NULL; if ((n = virXPathNodeSet("./features/xen/*", ctxt, &nodes)) < 0) return -1; for (i = 0; i < n; i++) { + virTristateSwitch state = VIR_TRISTATE_SWITCH_ABSENT; feature = virDomainXenTypeFromString((const char *)nodes[i]->name); if (feature < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, @@ -18688,7 +18654,12 @@ virDomainFeaturesDefParse(virDomainDefPtr def, return -1; } - if (!(tmp = virXMLPropString(nodes[i], "state"))) { + if (virXMLPropOnOff(nodes[i], "state", &state) < 0) + return -1; + + def->xen_features[feature] = state; + + if (state == VIR_TRISTATE_SWITCH_ABSENT) { virReportError(VIR_ERR_XML_ERROR, _("missing 'state' attribute for " "Xen feature '%s'"), @@ -18696,22 +18667,12 @@ virDomainFeaturesDefParse(virDomainDefPtr def, return -1; } - if ((value = virTristateSwitchTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid value of state argument " - "for Xen feature '%s'"), - nodes[i]->name); - return -1; - } - - def->xen_features[feature] = value; - switch ((virDomainXen) feature) { case VIR_DOMAIN_XEN_E820_HOST: break; case VIR_DOMAIN_XEN_PASSTHROUGH: - if (value != VIR_TRISTATE_SWITCH_ON) + if (state != VIR_TRISTATE_SWITCH_ON) break; if ((ptval = virXMLPropString(nodes[i], "mode"))) { @@ -18781,7 +18742,7 @@ virDomainFeaturesDefParse(virDomainDefPtr def, return -1; for (i = 0; i < n; i++) { - g_autofree char *tmp = NULL; + virTristateSwitch state = VIR_TRISTATE_SWITCH_ABSENT; int val = virDomainProcessCapsFeatureTypeFromString((const char *)nodes[i]->name); if (val < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, @@ -18789,16 +18750,13 @@ virDomainFeaturesDefParse(virDomainDefPtr def, return -1; } - if ((tmp = virXMLPropString(nodes[i], "state"))) { - if ((def->caps_features[val] = virTristateSwitchTypeFromString(tmp)) == -1) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown state attribute '%s' of feature capability '%s'"), - tmp, virDomainProcessCapsFeatureTypeToString(val)); - return -1; - } - } else { - def->caps_features[val] = VIR_TRISTATE_SWITCH_ON; - } + if (virXMLPropOnOff(nodes[i], "state", &state) < 0) + return -1; + + if (state == VIR_TRISTATE_SWITCH_ABSENT) + state = VIR_TRISTATE_SWITCH_ON; + + def->caps_features[val] = state; } VIR_FREE(nodes); return 0; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 4f020714bc..9b4c083801 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -18806,14 +18806,15 @@ virDomainLoaderDefParseXML(xmlNodePtr node, virDomainLoaderDefPtr loader, bool fwAutoSelect) { - g_autofree char *readonly_str = NULL; - g_autofree char *secure_str = NULL; g_autofree char *type_str = NULL; - - secure_str = virXMLPropString(node, "secure"); + virTristateBool readonly = VIR_TRISTATE_BOOL_ABSENT; + virTristateBool secure = VIR_TRISTATE_BOOL_ABSENT; if (!fwAutoSelect) { - readonly_str = virXMLPropString(node, "readonly"); + if (virXMLPropYesNo(node, "readonly", &readonly) < 0) + return -1; + loader->readonly = readonly; + type_str = virXMLPropString(node, "type"); if (!(loader->path = virXMLNodeContentString(node))) return -1; @@ -18822,19 +18823,9 @@ virDomainLoaderDefParseXML(xmlNodePtr node, VIR_FREE(loader->path); } - if (readonly_str && - (loader->readonly = virTristateBoolTypeFromString(readonly_str)) <= 0) { - virReportError(VIR_ERR_XML_DETAIL, - _("unknown readonly value: %s"), readonly_str); + if (virXMLPropYesNo(node, "secure", &secure) < 0) return -1; - } - - if (secure_str && - (loader->secure = virTristateBoolTypeFromString(secure_str)) <= 0) { - virReportError(VIR_ERR_XML_DETAIL, - _("unknown secure value: %s"), secure_str); - return -1; - } + loader->secure = secure; if (type_str) { int type; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 9b4c083801..66d3080744 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -19096,7 +19096,7 @@ virDomainVcpuParse(virDomainDefPtr def, for (i = 0; i < n; i++) { virDomainVcpuDefPtr vcpu; - int state; + virTristateBool state = VIR_TRISTATE_BOOL_ABSENT; unsigned int id; unsigned int order; @@ -19118,31 +19118,18 @@ virDomainVcpuParse(virDomainDefPtr def, vcpu = virDomainDefGetVcpu(def, id); - if (!(tmp = virXMLPropString(nodes[i], "enabled"))) { + if (virXMLPropYesNo(nodes[i], "enabled", &state) < 0) + return -1; + if (state == VIR_TRISTATE_BOOL_ABSENT) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing vcpu enabled state")); return -1; } - if ((state = virTristateBoolTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_XML_ERROR, - _("invalid vcpu 'enabled' value '%s'"), tmp); - return -1; - } - VIR_FREE(tmp); - vcpu->online = state == VIR_TRISTATE_BOOL_YES; - if ((tmp = virXMLPropString(nodes[i], "hotpluggable"))) { - int hotpluggable; - if ((hotpluggable = virTristateBoolTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_XML_ERROR, - _("invalid vcpu 'hotpluggable' value '%s'"), tmp); - return -1; - } - vcpu->hotpluggable = hotpluggable; - VIR_FREE(tmp); - } + if (virXMLPropYesNo(nodes[i], "hotpluggable", &vcpu->hotpluggable) < 0) + return -1; if ((tmp = virXMLPropString(nodes[i], "order"))) { if (virStrToLong_uip(tmp, NULL, 10, &order) < 0) { -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/backup_conf.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/conf/backup_conf.c b/src/conf/backup_conf.c index ba58b2e322..8560f3bf2e 100644 --- a/src/conf/backup_conf.c +++ b/src/conf/backup_conf.c @@ -106,7 +106,6 @@ virDomainBackupDiskDefParseXML(xmlNodePtr node, g_autofree char *type = NULL; g_autofree char *format = NULL; g_autofree char *idx = NULL; - g_autofree char *backup = NULL; g_autofree char *state = NULL; g_autofree char *backupmode = NULL; int tmp; @@ -125,17 +124,10 @@ virDomainBackupDiskDefParseXML(xmlNodePtr node, return -1; } - def->backup = VIR_TRISTATE_BOOL_YES; - - if ((backup = virXMLPropString(node, "backup"))) { - if ((tmp = virTristateBoolTypeFromString(backup)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("invalid disk 'backup' state '%s'"), backup); - return -1; - } - - def->backup = tmp; - } + if (virXMLPropYesNo(node, "backup", &def->backup) < 0) + return -1; + if (def->backup == VIR_TRISTATE_BOOL_ABSENT) + def->backup = VIR_TRISTATE_BOOL_YES; /* don't parse anything else if backup is disabled */ if (def->backup == VIR_TRISTATE_BOOL_NO) -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/backup_conf.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/conf/backup_conf.c b/src/conf/backup_conf.c index 8560f3bf2e..3a1863d2c3 100644 --- a/src/conf/backup_conf.c +++ b/src/conf/backup_conf.c @@ -234,8 +234,6 @@ virDomainBackupDefParse(xmlXPathContextPtr ctxt, def->incremental = virXPathString("string(./incremental)", ctxt); if ((node = virXPathNode("./server", ctxt))) { - g_autofree char *tls = NULL; - if (def->type != VIR_DOMAIN_BACKUP_TYPE_PULL) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("use of <server> requires pull mode backup")); @@ -261,18 +259,8 @@ virDomainBackupDefParse(xmlXPathContextPtr ctxt, return NULL; } - if ((tls = virXMLPropString(node, "tls"))) { - int tmp; - - if ((tmp = virTristateBoolTypeFromString(tls)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("unknown value '%s' of 'tls' attribute"),\ - tls); - return NULL; - } - - def->tls = tmp; - } + if (virXMLPropYesNo(node, "tls", &def->tls) < 0) + return NULL; } if ((n = virXPathNodeSet("./disks/*", ctxt, &nodes)) < 0) -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/device_conf.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/conf/device_conf.c b/src/conf/device_conf.c index 714ac50762..ee248648bd 100644 --- a/src/conf/device_conf.c +++ b/src/conf/device_conf.c @@ -214,7 +214,7 @@ virPCIDeviceAddressParseXML(xmlNodePtr node, g_autofree char *bus = virXMLPropString(node, "bus"); g_autofree char *slot = virXMLPropString(node, "slot"); g_autofree char *function = virXMLPropString(node, "function"); - g_autofree char *multi = virXMLPropString(node, "multifunction"); + virTristateSwitch multifunction = VIR_TRISTATE_SWITCH_ABSENT; memset(addr, 0, sizeof(*addr)); @@ -246,14 +246,10 @@ virPCIDeviceAddressParseXML(xmlNodePtr node, return -1; } - if (multi && - ((addr->multi = virTristateSwitchTypeFromString(multi)) <= 0)) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("Unknown value '%s' for <address> 'multifunction' attribute"), - multi); + if (virXMLPropOnOff(node, "multifunction", &multifunction) < 0) return -1; + addr->multi = multifunction; - } if (!virPCIDeviceAddressIsEmpty(addr) && !virPCIDeviceAddressIsValid(addr, true)) return -1; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/network_conf.c | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index 4cf4aa4840..93972a544a 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -1321,7 +1321,6 @@ virNetworkForwardNatDefParseXML(const char *networkName, g_autofree xmlNodePtr *natPortNodes = NULL; g_autofree char *addrStart = NULL; g_autofree char *addrEnd = NULL; - g_autofree char *ipv6 = NULL; VIR_XPATH_NODE_AUTORESTORE(ctxt) ctxt->node = node; @@ -1333,18 +1332,8 @@ virNetworkForwardNatDefParseXML(const char *networkName, return -1; } - ipv6 = virXMLPropString(node, "ipv6"); - if (ipv6) { - int natIPv6; - if ((natIPv6 = virTristateBoolTypeFromString(ipv6)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("Invalid ipv6 setting '%s' " - "in network '%s' NAT"), - ipv6, networkName); - return -1; - } - def->natIPv6 = natIPv6; - } + if (virXMLPropYesNo(node, "ipv6", &def->natIPv6) < 0) + return -1; /* addresses for SNAT */ nNatAddrs = virXPathNodeSet("./address", ctxt, &natAddrNodes); -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/numa_conf.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/conf/numa_conf.c b/src/conf/numa_conf.c index 64b93fd7d1..d16a44c955 100644 --- a/src/conf/numa_conf.c +++ b/src/conf/numa_conf.c @@ -1079,17 +1079,8 @@ virDomainNumaDefParseXML(virDomainNumaPtr def, VIR_FREE(tmp); } - if ((tmp = virXMLPropString(nodes[i], "discard"))) { - if ((rc = virTristateBoolTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("Invalid 'discard' attribute value '%s'"), - tmp); - goto cleanup; - } - - def->mem_nodes[cur_cell].discard = rc; - VIR_FREE(tmp); - } + if (virXMLPropYesNo(nodes[i], "discard", &def->mem_nodes[cur_cell].discard) < 0) + goto cleanup; /* Parse NUMA distances info */ if (virDomainNumaDefNodeDistanceParseXML(def, ctxt, cur_cell) < 0) -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/storage_adapter_conf.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/conf/storage_adapter_conf.c b/src/conf/storage_adapter_conf.c index 77ecb8d5f2..96e6c733d0 100644 --- a/src/conf/storage_adapter_conf.c +++ b/src/conf/storage_adapter_conf.c @@ -64,26 +64,19 @@ static int virStorageAdapterParseXMLFCHost(xmlNodePtr node, virStorageAdapterFCHostPtr fchost) { - char *managed = NULL; + virTristateBool managed = VIR_TRISTATE_BOOL_ABSENT; - fchost->parent = virXMLPropString(node, "parent"); - if ((managed = virXMLPropString(node, "managed"))) { - if ((fchost->managed = virTristateBoolTypeFromString(managed)) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown fc_host managed setting '%s'"), - managed); - VIR_FREE(managed); - return -1; - } - } + if (virXMLPropYesNo(node, "managed", &managed) < 0) + return -1; + fchost->parent = virXMLPropString(node, "parent"); + fchost->managed = managed; fchost->parent_wwnn = virXMLPropString(node, "parent_wwnn"); fchost->parent_wwpn = virXMLPropString(node, "parent_wwpn"); fchost->parent_fabric_wwn = virXMLPropString(node, "parent_fabric_wwn"); fchost->wwpn = virXMLPropString(node, "wwpn"); fchost->wwnn = virXMLPropString(node, "wwnn"); - VIR_FREE(managed); return 0; } -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/storage_conf.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c index 2e07c81f8a..7dc528b103 100644 --- a/src/conf/storage_conf.c +++ b/src/conf/storage_conf.c @@ -605,7 +605,7 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, goto cleanup; for (i = 0; i < nsource; i++) { - g_autofree char *partsep = NULL; + virTristateBool partsep = VIR_TRISTATE_BOOL_ABSENT; virStoragePoolSourceDevice dev = { .path = NULL }; dev.path = virXMLPropString(nodeset[i], "path"); @@ -615,17 +615,11 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, goto cleanup; } - partsep = virXMLPropString(nodeset[i], "part_separator"); - if (partsep) { - dev.part_separator = virTristateBoolTypeFromString(partsep); - if (dev.part_separator <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid part_separator setting '%s'"), - partsep); - virStoragePoolSourceDeviceClear(&dev); - goto cleanup; - } + if (virXMLPropYesNo(nodeset[i], "part_separator", &partsep) < 0) { + virStoragePoolSourceDeviceClear(&dev); + goto cleanup; } + dev.part_separator = partsep; if (VIR_APPEND_ELEMENT(source->devices, source->ndevice, dev) < 0) { virStoragePoolSourceDeviceClear(&dev); -- 2.26.2

On 3/18/21 9:00 AM, Tim Wiederhake wrote:
This series replaces some recurring boilerplate code in src/conf/ regarding the extraction of a virTristate(Switch|Bool) XML attribute.
The boilerplate code looks roughly like this,
g_autofree char *str = NULL; if (str = virXMLPropString(node, ...)) { int val; if ((val = virTristateBoolTypeFromString(str)) <= 0) { virReportError(...) return -1; } def->... = val; }
with some variations regarding how `str` is free'd in case of later re-use, the exact error message for invalid values, whether or not `VIR_TRISTATE_(SWITCH|BOOL)_ABSENT` is explicitly checked for (`val < 0` vs. `val <= 0`), whether an intermediate variable is used or the value is assigned directly, and in some cases the conditions in the two if-blocks are merged.
As a side effect, this makes the error messages for invalid values in these attributes much more consistent and catches some instances where e.g. `<foo bar="default"/>` would incorrectly be accepted.
Patch #11 (virDomainChrSourceReconnectDefParseXML) is a good example of what this refactoring is about.
Tim Wiederhake (38): virStorageAdapterFCHost: Fix comment virxml: Add virXMLPropYesNo virxml: Add virXMLPropOnOff domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainKeyWrapCipherDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainVirtioOptionsParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainDeviceInfoParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainDiskSourceNetworkParse domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainDiskSourceNVMeParse domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainDiskDefDriverParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainActualNetDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainChrSourceReconnectDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainNetDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainChrSourceDefParseTCP domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainChrSourceDefParseFile domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainChrSourceDefParseLog domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainGraphicsDefParseXMLVNC domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainGraphicsDefParseXMLSDL domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainGraphicsDefParseXMLSpice domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainAudioCommonParse domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainAudioJackParse domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainAudioOSSParse domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainAudioDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainMemballoonDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainShmemDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainPerfEventDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainMemoryDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainIOMMUDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainVsockDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainFeaturesDefParse domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainLoaderDefParseXML domain_conf: Use virXMLProp(OnOff|YesNo) in virDomainVcpuParse backup_conf: Use virXMLProp(OnOff|YesNo) in virDomainBackupDiskDefParseXML backup_conf: Use virXMLProp(OnOff|YesNo) in virDomainBackupDefParse device_conf: Use virXMLProp(OnOff|YesNo) in virPCIDeviceAddressParseXML network_conf: Use virXMLProp(OnOff|YesNo) in virNetworkForwardNatDefParseXML numa_conf: Use virXMLProp(OnOff|YesNo) in virDomainNumaDefParseXML storage_adapter_conf: Use virXMLProp(OnOff|YesNo) in virStorageAdapterParseXMLFCHost storage_conf: Use virXMLProp(OnOff|YesNo) in virStoragePoolDefParseSource
src/conf/backup_conf.c | 32 +- src/conf/device_conf.c | 10 +- src/conf/domain_conf.c | 791 +++++++++----------------------- src/conf/network_conf.c | 15 +- src/conf/numa_conf.c | 13 +- src/conf/storage_adapter_conf.c | 17 +- src/conf/storage_adapter_conf.h | 2 +- src/conf/storage_conf.c | 16 +- src/util/virxml.c | 74 +++ src/util/virxml.h | 7 + 10 files changed, 314 insertions(+), 663 deletions(-)
Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Nice cleanup! And if you agree with my suggestions in 01-02/38 (suggestion from 02 affects also 03), I can squash the changes in and push. Michal

On Thu, 2021-03-18 at 16:03 +0100, Michal Privoznik wrote:
On 3/18/21 9:00 AM, Tim Wiederhake wrote:
(...) 10 files changed, 314 insertions(+), 663 deletions(-)
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Nice cleanup!
And if you agree with my suggestions in 01-02/38 (suggestion from 02 affects also 03), I can squash the changes in and push.
Michal
Thanks for the review! Don't push it yet, I found one more "virTristateBoolTypeFromString" that can be changed and that I missed somehow. I will add that to the series, rebase, incorporate your suggestions, and resend tomorrow. My plan is to do something similar for the "int-like" attributes next. Cheers, Tim
participants (4)
-
Daniel P. Berrangé
-
Michal Privoznik
-
Pavel Hrdina
-
Tim Wiederhake