[libvirt PATCH v3 00/51] 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. V1: https://listman.redhat.com/archives/libvir-list/2021-March/msg00853.html V2: https://listman.redhat.com/archives/libvir-list/2021-March/msg00994.html Changes since V2: * Fixed the -Wtautological-unsigned-enum-zero-compare issues in the first part of the series. These issues were solved later in the series, but this change makes it easier to bisect in the future. I was thinking about only re-sending the first couple of patches, but the latter part would have endet up with quite a few conflicts, so I am sending it in its entirety, again. Sorry for the spam! Cheers, Tim Tim Wiederhake (51): conf: Use virTristateXXX in virStorageSource conf: Use virTristateXXX in virStorageSourceNVMeDef conf: Use virTristateXXX in virDomainDeviceInfo conf: Use virTristateXXX in virDomainDiskDef conf: Use virTristateXXX in virDomainActualNetDef conf: Use virTristateXXX in virDomainNetDef conf: Use virTristateXXX in virDomainChrSourceDef conf: Use virTristateXXX in virDomainGraphicsDef conf: Use virTristateXXX in virDomainMemballoonDef conf: Use virTristateXXX in virDomainLoaderDef conf: Use virTristateXXX in virDomainDef conf: Use virTristateXXX in virStorageAdapterFCHost conf: Use virTristateXXX in virStoragePoolSourceDevice conf: Use virTristateXXX in virPCIDeviceAddress virxml: Add virXMLPropTristateBool virxml: Add virXMLPropTristateSwitch domain_conf: Use virXMLPropTristateXXX in virDomainKeyWrapCipherDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainVirtioOptionsParseXML domain_conf: Use virXMLPropTristateXXX in virDomainDeviceInfoParseXML domain_conf: Use virXMLPropTristateXXX in virDomainDiskSourceNetworkParse domain_conf: Use virXMLPropTristateXXX in virDomainDiskSourceNVMeParse domain_conf: Use virXMLPropTristateXXX in virDomainDiskDefDriverParseXML domain_conf: Use virXMLPropTristateXXX in virDomainActualNetDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainChrSourceReconnectDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainNetDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainChrSourceDefParseTCP domain_conf: Use virXMLPropTristateXXX in virDomainChrSourceDefParseFile domain_conf: Use virXMLPropTristateXXX in virDomainChrSourceDefParseLog domain_conf: Use virXMLPropTristateXXX in virDomainGraphicsDefParseXMLVNC domain_conf: Use virXMLPropTristateXXX in virDomainGraphicsDefParseXMLSDL domain_conf: Use virXMLPropTristateXXX in virDomainGraphicsDefParseXMLSpice domain_conf: Use virXMLPropTristateXXX in virDomainAudioCommonParse domain_conf: Use virXMLPropTristateXXX in virDomainAudioJackParse domain_conf: Use virXMLPropTristateXXX in virDomainAudioOSSParse domain_conf: Use virXMLPropTristateXXX in virDomainAudioDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainMemballoonDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainShmemDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainPerfEventDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainMemoryDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainIOMMUDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainVsockDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainFeaturesDefParse domain_conf: Use virXMLPropTristateXXX in virDomainLoaderDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainVcpuParse backup_conf: Use virXMLPropTristateXXX in virDomainBackupDiskDefParseXML backup_conf: Use virXMLPropTristateXXX in virDomainBackupDefParse device_conf: Use virXMLPropTristateXXX in virPCIDeviceAddressParseXML network_conf: Use virXMLPropTristateXXX in virNetworkForwardNatDefParseXML numa_conf: Use virXMLPropTristateXXX in virDomainNumaDefParseXML storage_adapter_conf: Use virXMLPropTristateXXX in virStorageAdapterParseXMLFCHost storage_conf: Use virXMLPropTristateXXX in virStoragePoolDefParseSource src/conf/backup_conf.c | 32 +- src/conf/device_conf.c | 8 +- src/conf/device_conf.h | 4 +- src/conf/domain_conf.c | 794 +++++++------------------------- src/conf/domain_conf.h | 28 +- src/conf/network_conf.c | 15 +- src/conf/numa_conf.c | 14 +- src/conf/storage_adapter_conf.c | 14 +- src/conf/storage_adapter_conf.h | 2 +- src/conf/storage_conf.c | 17 +- src/conf/storage_conf.h | 2 +- src/conf/storage_source_conf.h | 4 +- src/libvirt_private.syms | 2 + src/qemu/qemu_command.c | 3 +- src/qemu/qemu_hotplug.c | 2 +- src/util/virpci.h | 2 +- src/util/virxml.c | 82 ++++ src/util/virxml.h | 9 + 18 files changed, 301 insertions(+), 733 deletions(-) -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 12 ++++++++---- src/conf/storage_source_conf.h | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 7671050134..3d24479f28 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -8338,11 +8338,15 @@ 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); + if ((haveTLS = virXMLPropString(node, "tls"))) { + int value; + + if ((value = virTristateBoolTypeFromString(haveTLS)) <= 0) { + virReportError(VIR_ERR_XML_ERROR, + _("unknown disk source 'tls' setting '%s'"), haveTLS); return -1; + } + src->haveTLS = value; } if ((flags & VIR_DOMAIN_DEF_PARSE_STATUS) && diff --git a/src/conf/storage_source_conf.h b/src/conf/storage_source_conf.h index f42bb1c67d..e6702a1ffc 100644 --- a/src/conf/storage_source_conf.h +++ b/src/conf/storage_source_conf.h @@ -356,7 +356,7 @@ struct _virStorageSource { char *nodestorage; /* name of the storage object */ /* An optional setting to enable usage of TLS for the storage source */ - int haveTLS; /* enum virTristateBool */ + virTristateBool haveTLS; /* Indication whether the haveTLS value was altered due to qemu.conf * setting when haveTLS is missing from the domain config file */ -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 4 +++- src/conf/storage_source_conf.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 3d24479f28..27eb98d93c 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -8478,12 +8478,14 @@ virDomainDiskSourceNVMeParse(xmlNodePtr node, } if ((managed = virXMLPropString(node, "managed"))) { - if ((nvme->managed = virTristateBoolTypeFromString(managed)) <= 0) { + int value; + if ((value = virTristateBoolTypeFromString(managed)) <= 0) { virReportError(VIR_ERR_XML_ERROR, _("malformed managed value '%s'"), managed); return -1; } + nvme->managed = value; } if (!(address = virXPathNode("./address", ctxt))) { diff --git a/src/conf/storage_source_conf.h b/src/conf/storage_source_conf.h index e6702a1ffc..1783dc195e 100644 --- a/src/conf/storage_source_conf.h +++ b/src/conf/storage_source_conf.h @@ -251,7 +251,7 @@ typedef struct _virStorageSourceNVMeDef virStorageSourceNVMeDef; typedef virStorageSourceNVMeDef *virStorageSourceNVMeDefPtr; struct _virStorageSourceNVMeDef { unsigned long long namespc; - int managed; /* enum virTristateBool */ + virTristateBool managed; virPCIDeviceAddress pciAddr; /* Don't forget to update virStorageSourceNVMeDefCopy */ -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/device_conf.h | 4 ++-- src/conf/domain_conf.c | 26 ++++++++++++++++---------- src/qemu/qemu_command.c | 3 ++- src/qemu/qemu_hotplug.c | 2 +- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/conf/device_conf.h b/src/conf/device_conf.h index a51bdf10ee..289af1153a 100644 --- a/src/conf/device_conf.h +++ b/src/conf/device_conf.h @@ -153,8 +153,8 @@ struct _virDomainDeviceInfo { } master; /* rombar and romfile are only used for pci hostdev and network * devices. */ - int romenabled; /* enum virTristateBool */ - int rombar; /* enum virTristateSwitch */ + virTristateBool romenabled; + virTristateSwitch rombar; char *romfile; /* bootIndex is only used for disk, network interface, hostdev * and redirdev devices */ diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 27eb98d93c..43789fa2c7 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -6681,17 +6681,23 @@ virDomainDeviceInfoParseXML(virDomainXMLOptionPtr xmlopt, if ((flags & VIR_DOMAIN_DEF_PARSE_ALLOW_ROM) && (rom = virXPathNode("./rom", ctxt))) { - if ((romenabled = virXPathString("string(./rom/@enabled)", ctxt)) && - ((info->romenabled = virTristateBoolTypeFromString(romenabled)) <= 0)) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown rom enabled value '%s'"), romenabled); - goto cleanup; + if ((romenabled = virXPathString("string(./rom/@enabled)", ctxt))) { + int value; + if ((value = virTristateBoolTypeFromString(romenabled)) <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown rom enabled value '%s'"), romenabled); + goto cleanup; + } + info->romenabled = value; } - if ((rombar = virXPathString("string(./rom/@bar)", ctxt)) && - ((info->rombar = virTristateSwitchTypeFromString(rombar)) <= 0)) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown rom bar value '%s'"), rombar); - goto cleanup; + if ((rombar = virXPathString("string(./rom/@bar)", ctxt))) { + int value; + if ((value = virTristateSwitchTypeFromString(rombar)) <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown rom bar value '%s'"), rombar); + goto cleanup; + } + info->rombar = value; } info->romfile = virXMLPropString(rom, "file"); diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 5717f7b98d..73c28ed7a0 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -660,7 +660,8 @@ qemuBuildRomStr(virBufferPtr buf, case VIR_TRISTATE_SWITCH_ON: virBufferAddLit(buf, ",rombar=1"); break; - default: + case VIR_TRISTATE_SWITCH_ABSENT: + case VIR_TRISTATE_SWITCH_LAST: break; } if (info->romfile) { diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index a66354426d..cc49f10198 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -3665,7 +3665,7 @@ qemuDomainChangeNet(virQEMUDriverPtr driver, /* device alias is checked already in virDomainDefCompatibleDevice */ - if (newdev->info.rombar == VIR_TRISTATE_BOOL_ABSENT) + if (newdev->info.rombar == VIR_TRISTATE_SWITCH_ABSENT) newdev->info.rombar = olddev->info.rombar; if (olddev->info.rombar != newdev->info.rombar) { virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 39 ++++++++++++++++++++++++--------------- src/conf/domain_conf.h | 6 +++--- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 43789fa2c7..014f318dcd 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -9196,27 +9196,36 @@ 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); - return -1; + if ((tmp = virXMLPropString(cur, "ioeventfd"))) { + int value; + if ((value = virTristateSwitchTypeFromString(tmp)) <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown disk ioeventfd mode '%s'"), tmp); + return -1; + } + def->ioeventfd = value; } VIR_FREE(tmp); - if ((tmp = virXMLPropString(cur, "event_idx")) && - (def->event_idx = virTristateSwitchTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown disk event_idx mode '%s'"), tmp); - return -1; + if ((tmp = virXMLPropString(cur, "event_idx"))) { + int value; + if ((value = virTristateSwitchTypeFromString(tmp)) <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown disk event_idx mode '%s'"), tmp); + return -1; + } + def->event_idx = value; } VIR_FREE(tmp); - 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); - return -1; + if ((tmp = virXMLPropString(cur, "copy_on_read"))) { + int value; + if ((value = virTristateSwitchTypeFromString(tmp)) <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown disk copy_on_read mode '%s'"), tmp); + return -1; + } + def->copy_on_read = value; } VIR_FREE(tmp); diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 87bc7e8625..853cab96b5 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -568,9 +568,9 @@ struct _virDomainDiskDef { int error_policy; /* enum virDomainDiskErrorPolicy */ int rerror_policy; /* enum virDomainDiskErrorPolicy */ int iomode; /* enum virDomainDiskIo */ - int ioeventfd; /* enum virTristateSwitch */ - int event_idx; /* enum virTristateSwitch */ - int copy_on_read; /* enum virTristateSwitch */ + virTristateSwitch ioeventfd; + virTristateSwitch event_idx; + virTristateSwitch copy_on_read; int snapshot; /* virDomainSnapshotLocation, snapshot_conf.h */ int startupPolicy; /* enum virDomainStartupPolicy */ bool transient; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 17 +++++++++-------- src/conf/domain_conf.h | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 014f318dcd..8e94860b51 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10551,14 +10551,15 @@ 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); - goto error; + if ((trustGuestRxFilters = virXMLPropString(node, "trustGuestRxFilters"))) { + int value; + if ((value = virTristateBoolTypeFromString(trustGuestRxFilters)) <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown trustGuestRxFilters value '%s'"), + trustGuestRxFilters); + goto error; + } + actual->trustGuestRxFilters = value; } virtPortNode = virXPathNode("./virtualport", ctxt); diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 853cab96b5..90079d7e64 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -994,7 +994,7 @@ struct _virDomainActualNetDef { virNetDevVPortProfilePtr virtPortProfile; virNetDevBandwidthPtr bandwidth; virNetDevVlan vlan; - int trustGuestRxFilters; /* enum virTristateBool */ + virTristateBool trustGuestRxFilters; virTristateBool isolatedPort; unsigned int class_id; /* class ID for bandwidth 'floor' */ }; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 17 +++++++++-------- src/conf/domain_conf.h | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 8e94860b51..9f59756119 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10854,14 +10854,15 @@ 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); - goto error; + if ((trustGuestRxFilters = virXMLPropString(node, "trustGuestRxFilters"))) { + int value; + if ((value = virTristateBoolTypeFromString(trustGuestRxFilters)) <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown trustGuestRxFilters value '%s'"), + trustGuestRxFilters); + goto error; + } + def->trustGuestRxFilters = value; } cur = node->children; diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 90079d7e64..15dc416384 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1102,7 +1102,7 @@ struct _virDomainNetDef { GHashTable *filterparams; virNetDevBandwidthPtr bandwidth; virNetDevVlan vlan; - int trustGuestRxFilters; /* enum virTristateBool */ + virTristateBool trustGuestRxFilters; virTristateBool isolatedPort; int linkstate; unsigned int mtu; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 34 +++++++++++++++++++++------------- src/conf/domain_conf.h | 6 +++--- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 9f59756119..e99699028a 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -11891,12 +11891,14 @@ virDomainChrSourceDefParseTCP(virDomainChrSourceDefPtr def, def->data.tcp.service = virXMLPropString(source, "service"); if ((tmp = virXMLPropString(source, "tls"))) { - if ((def->data.tcp.haveTLS = virTristateBoolTypeFromString(tmp)) <= 0) { + int value; + if ((value = virTristateBoolTypeFromString(tmp)) <= 0) { virReportError(VIR_ERR_XML_ERROR, _("unknown chardev 'tls' setting '%s'"), tmp); return -1; } + def->data.tcp.haveTLS = value; VIR_FREE(tmp); } @@ -11975,12 +11977,15 @@ virDomainChrSourceDefParseFile(virDomainChrSourceDefPtr def, def->data.file.path = virXMLPropString(source, "path"); - if ((append = virXMLPropString(source, "append")) && - (def->data.file.append = virTristateSwitchTypeFromString(append)) <= 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Invalid append attribute value '%s'"), - append); - return -1; + if ((append = virXMLPropString(source, "append"))) { + int value; + if ((value = virTristateSwitchTypeFromString(append)) <= 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Invalid append attribute value '%s'"), + append); + return -1; + } + def->data.file.append = value; } return 0; @@ -12016,12 +12021,15 @@ virDomainChrSourceDefParseLog(virDomainChrSourceDefPtr def, 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); - return -1; + if ((append = virXMLPropString(log, "append"))) { + int value; + if ((value = virTristateSwitchTypeFromString(append)) <= 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Invalid append attribute value '%s'"), + append); + return -1; + } + def->logappend = value; } return 0; diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 15dc416384..6619b9f006 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1232,7 +1232,7 @@ struct _virDomainChrSourceDef { /* no <source> for null, vc, stdio */ struct { char *path; - int append; /* enum virTristateSwitch */ + virTristateSwitch append; } file; /* pty, file, pipe, or device */ struct { char *master; @@ -1244,7 +1244,7 @@ struct _virDomainChrSourceDef { bool listen; int protocol; bool tlscreds; - int haveTLS; /* enum virTristateBool */ + virTristateBool haveTLS; bool tlsFromConfig; virDomainChrSourceReconnectDef reconnect; } tcp; @@ -1265,7 +1265,7 @@ struct _virDomainChrSourceDef { } spiceport; } data; char *logfile; - int logappend; + virTristateSwitch logappend; size_t nseclabels; virSecurityDeviceLabelDefPtr *seclabels; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 6619b9f006..09b697432d 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1862,7 +1862,7 @@ struct _virDomainGraphicsDef { int image; int jpeg; int zlib; - int playback; + virTristateSwitch playback; int streaming; virTristateBool copypaste; virTristateBool filetransfer; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 26 ++++++++++++++++---------- src/conf/domain_conf.h | 4 ++-- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index e99699028a..0480fc610d 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -14547,18 +14547,24 @@ 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); - goto error; + if ((deflate = virXMLPropString(node, "autodeflate"))) { + int value; + if ((value = virTristateSwitchTypeFromString(deflate)) <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("invalid autodeflate attribute value '%s'"), deflate); + goto error; + } + def->autodeflate = value; } - 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); - goto error; + if ((freepage_reporting = virXMLPropString(node, "freePageReporting"))) { + int value; + if ((value = virTristateSwitchTypeFromString(freepage_reporting)) <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("invalid freePageReporting attribute value '%s'"), freepage_reporting); + goto error; + } + def->free_page_reporting = value; } ctxt->node = node; diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 09b697432d..2d342effb1 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1921,8 +1921,8 @@ struct _virDomainMemballoonDef { int model; virDomainDeviceInfo info; int period; /* seconds between collections */ - int autodeflate; /* enum virTristateSwitch */ - int free_page_reporting; /* enum virTristateSwitch */ + virTristateSwitch autodeflate; + virTristateSwitch free_page_reporting; virDomainVirtioOptionsPtr virtio; }; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 26 ++++++++++++++++---------- src/conf/domain_conf.h | 4 ++-- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 0480fc610d..9e106b8846 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -19198,18 +19198,24 @@ 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); - return -1; + if (readonly_str) { + int value; + if ((value = virTristateBoolTypeFromString(readonly_str)) <= 0) { + virReportError(VIR_ERR_XML_DETAIL, + _("unknown readonly value: %s"), readonly_str); + return -1; + } + loader->readonly = value; } - if (secure_str && - (loader->secure = virTristateBoolTypeFromString(secure_str)) <= 0) { - virReportError(VIR_ERR_XML_DETAIL, - _("unknown secure value: %s"), secure_str); - return -1; + if (secure_str) { + int value; + if ((value = virTristateBoolTypeFromString(secure_str)) <= 0) { + virReportError(VIR_ERR_XML_DETAIL, + _("unknown secure value: %s"), secure_str); + return -1; + } + loader->secure = value; } if (type_str) { diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 2d342effb1..d6ca5e9725 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -2152,9 +2152,9 @@ VIR_ENUM_DECL(virDomainLoader); struct _virDomainLoaderDef { char *path; - int readonly; /* enum virTristateBool */ + virTristateBool readonly; virDomainLoader type; - int secure; /* enum virTristateBool */ + virTristateBool secure; char *nvram; /* path to non-volatile RAM */ char *templt; /* user override of path to master nvram */ }; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index d6ca5e9725..25af058241 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -2755,7 +2755,7 @@ struct _virDomainDef { virDomainHPTResizing hpt_resizing; unsigned long long hpt_maxpagesize; /* Stored in KiB */ char *hyperv_vendor_id; - int apic_eoi; + virTristateSwitch apic_eoi; bool tseg_specified; unsigned long long tseg_size; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/storage_adapter_conf.c | 4 +++- src/conf/storage_adapter_conf.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/conf/storage_adapter_conf.c b/src/conf/storage_adapter_conf.c index 77ecb8d5f2..69062b4b58 100644 --- a/src/conf/storage_adapter_conf.c +++ b/src/conf/storage_adapter_conf.c @@ -68,13 +68,15 @@ virStorageAdapterParseXMLFCHost(xmlNodePtr node, fchost->parent = virXMLPropString(node, "parent"); if ((managed = virXMLPropString(node, "managed"))) { - if ((fchost->managed = virTristateBoolTypeFromString(managed)) < 0) { + int value; + if ((value = virTristateBoolTypeFromString(managed)) < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unknown fc_host managed setting '%s'"), managed); VIR_FREE(managed); return -1; } + fchost->managed = value; } fchost->parent_wwnn = virXMLPropString(node, "parent_wwnn"); diff --git a/src/conf/storage_adapter_conf.h b/src/conf/storage_adapter_conf.h index 4c7da7c8d9..3f64cda9af 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 */ + virTristateBool managed; }; typedef struct _virStorageAdapter virStorageAdapter; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/storage_conf.c | 7 ++++--- src/conf/storage_conf.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c index 2e07c81f8a..6116b04d44 100644 --- a/src/conf/storage_conf.c +++ b/src/conf/storage_conf.c @@ -617,14 +617,15 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, partsep = virXMLPropString(nodeset[i], "part_separator"); if (partsep) { - dev.part_separator = virTristateBoolTypeFromString(partsep); - if (dev.part_separator <= 0) { + int value = virTristateBoolTypeFromString(partsep); + if (value <= 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("invalid part_separator setting '%s'"), partsep); virStoragePoolSourceDeviceClear(&dev); goto cleanup; } + dev.part_separator = value; } if (VIR_APPEND_ELEMENT(source->devices, source->ndevice, dev) < 0) { @@ -1097,7 +1098,7 @@ virStoragePoolSourceFormat(virBufferPtr buf, virBufferEscapeString(buf, "<device path='%s'", src->devices[i].path); if (src->devices[i].part_separator != - VIR_TRISTATE_SWITCH_ABSENT) { + VIR_TRISTATE_BOOL_ABSENT) { virBufferAsprintf(buf, " part_separator='%s'", virTristateBoolTypeToString(src->devices[i].part_separator)); } diff --git a/src/conf/storage_conf.h b/src/conf/storage_conf.h index 647eb847bf..8d417af7bb 100644 --- a/src/conf/storage_conf.h +++ b/src/conf/storage_conf.h @@ -168,7 +168,7 @@ struct _virStoragePoolSourceDevice { virStoragePoolSourceDeviceExtentPtr freeExtents; char *path; int format; /* Pool specific source format */ - int part_separator; /* enum virTristateSwitch */ + virTristateBool part_separator; /* When the source device is a physical disk, * the geometry data is needed -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/device_conf.c | 16 +++++++++------- src/util/virpci.h | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/conf/device_conf.c b/src/conf/device_conf.c index 714ac50762..0dd60985e9 100644 --- a/src/conf/device_conf.c +++ b/src/conf/device_conf.c @@ -246,13 +246,15 @@ 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); - return -1; - + if (multi) { + int value; + if ((value = virTristateSwitchTypeFromString(multi)) <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("Unknown value '%s' for <address> 'multifunction' attribute"), + multi); + return -1; + } + addr->multi = value; } if (!virPCIDeviceAddressIsEmpty(addr) && !virPCIDeviceAddressIsValid(addr, true)) return -1; diff --git a/src/util/virpci.h b/src/util/virpci.h index 9b37a12883..2c86642ea7 100644 --- a/src/util/virpci.h +++ b/src/util/virpci.h @@ -65,7 +65,7 @@ struct _virPCIDeviceAddress { unsigned int bus; unsigned int slot; unsigned int function; - int multi; /* virTristateSwitch */ + virTristateSwitch multi; int extFlags; /* enum virPCIDeviceAddressExtensionFlags */ virZPCIDeviceAddress zpci; /* Don't forget to update virPCIDeviceAddressCopy if needed. */ -- 2.26.2

Convenience function to return value of a yes / no attribute. Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/libvirt_private.syms | 1 + src/util/virxml.c | 41 ++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 5 +++++ 3 files changed, 47 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 526dcee11a..70525cef8c 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3545,6 +3545,7 @@ virXMLParseHelper; virXMLPickShellSafeComment; virXMLPropString; virXMLPropStringLimit; +virXMLPropTristateBool; virXMLSaveFile; virXMLValidateAgainstSchema; virXMLValidatorFree; diff --git a/src/util/virxml.c b/src/util/virxml.c index 4a6fe09468..81b7bb1386 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -558,6 +558,47 @@ virXMLNodeContentString(xmlNodePtr node) } +/** + * virXMLPropTristateBool: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * @required: Change the return value to -1 if the attribute is not present + * @result: 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 +virXMLPropTristateBool(xmlNodePtr node, const char* name, bool required, + virTristateBool *result) +{ + g_autofree char *tmp = virXMLPropString(node, name); + int val; + + if (!tmp) { + if (!required) + return 0; + virReportError(VIR_ERR_XML_ERROR, + _("Missing required attribute '%s' in element '%s'"), + name, node->name); + return -1; + } + + if ((val = virTristateBoolTypeFromString(tmp)) <= 0) { + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in element '%s': '%s'. Expected 'yes' or 'no'"), + name, node->name, tmp); + return -1; + } + + *result = val; + return 1; +} + + /** * virXPathBoolean: * @xpath: the XPath string to evaluate diff --git a/src/util/virxml.h b/src/util/virxml.h index d32f77b867..3041c37df3 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,10 @@ char * virXMLPropStringLimit(xmlNodePtr node, const char *name, size_t maxlen); char * virXMLNodeContentString(xmlNodePtr node); +int virXMLPropTristateBool(xmlNodePtr node, + const char *name, + bool mandatory, + virTristateBool *result); /* Internal function; prefer the macros below. */ xmlDocPtr virXMLParseHelper(int domcode, -- 2.26.2

Convenience function to return value of an on / off attribute. Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/libvirt_private.syms | 1 + src/util/virxml.c | 41 ++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 6 +++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 70525cef8c..9aed890c58 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3546,6 +3546,7 @@ virXMLPickShellSafeComment; virXMLPropString; virXMLPropStringLimit; virXMLPropTristateBool; +virXMLPropTristateSwitch; virXMLSaveFile; virXMLValidateAgainstSchema; virXMLValidatorFree; diff --git a/src/util/virxml.c b/src/util/virxml.c index 81b7bb1386..aaad6453d2 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -599,6 +599,47 @@ virXMLPropTristateBool(xmlNodePtr node, const char* name, bool required, } +/** + * virXMLPropTristateSwitch: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * @required: Change the return value to -1 if the attribute is not present + * @result: 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 +virXMLPropTristateSwitch(xmlNodePtr node, const char* name, bool required, + virTristateSwitch *result) +{ + g_autofree char *tmp = virXMLPropString(node, name); + int val; + + if (!tmp) { + if (!required) + return 0; + virReportError(VIR_ERR_XML_ERROR, + _("Missing required attribute '%s' in element '%s'"), + name, node->name); + return -1; + } + + if ((val = virTristateSwitchTypeFromString(tmp)) <= 0) { + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in element '%s': '%s'. Expected 'on' or 'off'"), + name, node->name, tmp); + return -1; + } + + *result = val; + return 1; +} + + /** * virXPathBoolean: * @xpath: the XPath string to evaluate diff --git a/src/util/virxml.h b/src/util/virxml.h index 3041c37df3..e844cb0713 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -80,8 +80,12 @@ char * virXMLPropStringLimit(xmlNodePtr node, char * virXMLNodeContentString(xmlNodePtr node); int virXMLPropTristateBool(xmlNodePtr node, const char *name, - bool mandatory, + bool required, virTristateBool *result); +int virXMLPropTristateSwitch(xmlNodePtr node, + const char *name, + bool required, + virTristateSwitch *result); /* Internal function; prefer the macros below. */ xmlDocPtr virXMLParseHelper(int domcode, -- 2.26.2

On 3/19/21 4:57 PM, Tim Wiederhake wrote:
Convenience function to return value of an on / off attribute.
Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/libvirt_private.syms | 1 + src/util/virxml.c | 41 ++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 6 +++++- 3 files changed, 47 insertions(+), 1 deletion(-)
diff --git a/src/util/virxml.h b/src/util/virxml.h index 3041c37df3..e844cb0713 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -80,8 +80,12 @@ char * virXMLPropStringLimit(xmlNodePtr node, char * virXMLNodeContentString(xmlNodePtr node); int virXMLPropTristateBool(xmlNodePtr node, const char *name, - bool mandatory, + bool required, virTristateBool *result);
I guess this doesn't belong here. I'll squash it into the previous patch.
+int virXMLPropTristateSwitch(xmlNodePtr node, + const char *name, + bool required, + virTristateSwitch *result);
/* Internal function; prefer the macros below. */ xmlDocPtr virXMLParseHelper(int domcode,
Michal

On Mon, Mar 22, 2021 at 01:23:28PM +0100, Michal Privoznik wrote:
On 3/19/21 4:57 PM, Tim Wiederhake wrote:
Convenience function to return value of an on / off attribute.
Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/libvirt_private.syms | 1 + src/util/virxml.c | 41 ++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 6 +++++- 3 files changed, 47 insertions(+), 1 deletion(-)
diff --git a/src/util/virxml.h b/src/util/virxml.h index 3041c37df3..e844cb0713 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -80,8 +80,12 @@ char * virXMLPropStringLimit(xmlNodePtr node, char * virXMLNodeContentString(xmlNodePtr node); int virXMLPropTristateBool(xmlNodePtr node, const char *name, - bool mandatory, + bool required, virTristateBool *result);
I guess this doesn't belong here. I'll squash it into the previous patch.
I wonder if we should avoid the boolean arg entirely though. Looking at a call if (virXMLPropTristateBool(node, "foo", true, &result) < 0) you have no idea whether "true" means mandatory or optional unless you're very familiar with the code. In GTK/GNOME they generally discourage use of bool parameters in favour of enums for this reason. eg they would have a VIR_XML_TRISTATE_REQUIRED enum flag. This is quite verbose though, so perhaps we can just use trivial wrapper methods: virXMLPropTristateRequiredBool virXMLPropTristateOptionalBool 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 Mon, 2021-03-22 at 12:31 +0000, Daniel P. Berrangé wrote:
On Mon, Mar 22, 2021 at 01:23:28PM +0100, Michal Privoznik wrote:
On 3/19/21 4:57 PM, Tim Wiederhake wrote:
Convenience function to return value of an on / off attribute.
Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/libvirt_private.syms | 1 + src/util/virxml.c | 41 ++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 6 +++++- 3 files changed, 47 insertions(+), 1 deletion(-)
diff --git a/src/util/virxml.h b/src/util/virxml.h index 3041c37df3..e844cb0713 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -80,8 +80,12 @@ char * virXMLPropStringLimit(xmlNodePtr node, char * virXMLNodeContentString(xmlNodePtr node); int virXMLPropTristateBool(xmlNodePtr node, const char *name, - bool mandatory, + bool required, virTristateBool *result);
I guess this doesn't belong here. I'll squash it into the previous patch.
I wonder if we should avoid the boolean arg entirely though. Looking at a call
if (virXMLPropTristateBool(node, "foo", true, &result) < 0)
you have no idea whether "true" means mandatory or optional unless you're very familiar with the code. In GTK/GNOME they generally discourage use of bool parameters in favour of enums for this reason. eg they would have a VIR_XML_TRISTATE_REQUIRED enum flag.
This is quite verbose though, so perhaps we can just use trivial wrapper methods:
virXMLPropTristateRequiredBool virXMLPropTristateOptionalBool
Regards, Daniel
I agree that having a couple of bool arguments is not perfect, and my first (unpublished and proof-of-concept) version had one function per variant. The problem with this approach becomes apparent in the yet-to-be- published series that refactors reading integer attributes. In addition to the "required or optional" variant, these also have "allow two's complement wraparound for unsigned int or not" variants and "zero is a valid number or not" variants. Having dedicated enums for this might be an option, especially as I believe I will be able to reuse e.g. the enum for "attribute is required or not". What is your opinion on using bools for the time being, see how the refactoring for "int attributes" and "enum attributes" turns out, and then revisiting this issue? Regards, Tim

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 9e106b8846..7dfbca12e5 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1447,10 +1447,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", @@ -1464,17 +1463,8 @@ virDomainKeyWrapCipherDefParseXML(virDomainKeyWrapDefPtr keywrap, return -1; } - if (!(state = virXMLPropString(node, "state"))) { - virReportError(VIR_ERR_CONF_SYNTAX, - _("missing state for cipher named %s"), name); - return -1; - } - - if ((state_type = virTristateSwitchTypeFromString(state)) < 0) { - virReportError(VIR_ERR_CONF_SYNTAX, - _("%s is not a supported cipher state"), state); + if (virXMLPropTristateSwitch(node, "state", true, &state_type) < 0) return -1; - } switch ((virDomainKeyWrapCipherName) name_type) { case VIR_DOMAIN_KEY_WRAP_CIPHER_NAME_AES: -- 2.26.2

On 3/19/21 4:57 PM, Tim Wiederhake wrote:
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 9e106b8846..7dfbca12e5 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1447,10 +1447,9 @@ static int virDomainKeyWrapCipherDefParseXML(virDomainKeyWrapDefPtr keywrap, xmlNodePtr node) { - int state_type; + virTristateSwitch state_type = VIR_TRISTATE_SWITCH_ABSENT;
Strictly speaking, initializing this is not necessary, because the value is required (by passing true below). At the same time, I like having initialized variables. So this is correct.
int name_type; g_autofree char *name = NULL; - g_autofree char *state = NULL;
if (!(name = virXMLPropString(node, "name"))) { virReportError(VIR_ERR_CONF_SYNTAX, "%s", @@ -1464,17 +1463,8 @@ virDomainKeyWrapCipherDefParseXML(virDomainKeyWrapDefPtr keywrap, return -1; }
- if (!(state = virXMLPropString(node, "state"))) { - virReportError(VIR_ERR_CONF_SYNTAX, - _("missing state for cipher named %s"), name); - return -1; - } - - if ((state_type = virTristateSwitchTypeFromString(state)) < 0) { - virReportError(VIR_ERR_CONF_SYNTAX, - _("%s is not a supported cipher state"), state); + if (virXMLPropTristateSwitch(node, "state", true, &state_type) < 0) return -1; - }
switch ((virDomainKeyWrapCipherName) name_type) { case VIR_DOMAIN_KEY_WRAP_CIPHER_NAME_AES:
Michal

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 7dfbca12e5..6997b7d743 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1591,9 +1591,7 @@ static int virDomainVirtioOptionsParseXML(xmlNodePtr driver, virDomainVirtioOptionsPtr *virtio) { - int val; virDomainVirtioOptionsPtr res; - g_autofree char *str = NULL; if (*virtio || !driver) return 0; @@ -1602,34 +1600,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 (virXMLPropTristateSwitch(driver, "iommu", false, &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 (virXMLPropTristateSwitch(driver, "ats", false, &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 (virXMLPropTristateSwitch(driver, "packed", false, &res->packed) < 0) + return -1; return 0; } -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 6997b7d743..920078a706 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; VIR_XPATH_NODE_AUTORESTORE(ctxt) @@ -6649,24 +6647,12 @@ virDomainDeviceInfoParseXML(virDomainXMLOptionPtr xmlopt, if ((flags & VIR_DOMAIN_DEF_PARSE_ALLOW_ROM) && (rom = virXPathNode("./rom", ctxt))) { - if ((romenabled = virXPathString("string(./rom/@enabled)", ctxt))) { - int value; - if ((value = virTristateBoolTypeFromString(romenabled)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown rom enabled value '%s'"), romenabled); - goto cleanup; - } - info->romenabled = value; - } - if ((rombar = virXPathString("string(./rom/@bar)", ctxt))) { - int value; - if ((value = virTristateSwitchTypeFromString(rombar)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown rom bar value '%s'"), rombar); - goto cleanup; - } - info->rombar = value; - } + if (virXMLPropTristateBool(rom, "enabled", false, &info->romenabled) < 0) + goto cleanup; + + if (virXMLPropTristateSwitch(rom, "bar", false, &info->rombar) < 0) + goto cleanup; + info->romfile = virXMLPropString(rom, "file"); if (info->romenabled == VIR_TRISTATE_BOOL_NO && -- 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 920078a706..4fd4fabb3f 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -8274,7 +8274,6 @@ 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; xmlNodePtr tmpnode; @@ -8298,16 +8297,8 @@ virDomainDiskSourceNetworkParse(xmlNodePtr node, return -1; } - if ((haveTLS = virXMLPropString(node, "tls"))) { - int value; - - if ((value = virTristateBoolTypeFromString(haveTLS)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("unknown disk source 'tls' setting '%s'"), haveTLS); - return -1; - } - src->haveTLS = value; - } + if (virXMLPropTristateBool(node, "tls", false, &src->haveTLS) < 0) + return -1; 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, 2 insertions(+), 11 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 4fd4fabb3f..f2fb3c8dd5 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -8397,7 +8397,6 @@ virDomainDiskSourceNVMeParse(xmlNodePtr node, g_autoptr(virStorageSourceNVMeDef) nvme = NULL; g_autofree char *type = NULL; g_autofree char *namespc = NULL; - g_autofree char *managed = NULL; xmlNodePtr address; nvme = g_new0(virStorageSourceNVMeDef, 1); @@ -8428,16 +8427,8 @@ virDomainDiskSourceNVMeParse(xmlNodePtr node, return -1; } - if ((managed = virXMLPropString(node, "managed"))) { - int value; - if ((value = virTristateBoolTypeFromString(managed)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("malformed managed value '%s'"), - managed); - return -1; - } - nvme->managed = value; - } + if (virXMLPropTristateBool(node, "managed", false, &nvme->managed) < 0) + return -1; 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 | 36 ++++++------------------------------ 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index f2fb3c8dd5..f17c979e31 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -9132,38 +9132,14 @@ virDomainDiskDefDriverParseXML(virDomainDiskDefPtr def, } VIR_FREE(tmp); - if ((tmp = virXMLPropString(cur, "ioeventfd"))) { - int value; - if ((value = virTristateSwitchTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown disk ioeventfd mode '%s'"), tmp); - return -1; - } - def->ioeventfd = value; - } - VIR_FREE(tmp); + if (virXMLPropTristateSwitch(cur, "ioeventfd", false, &def->ioeventfd) < 0) + return -1; - if ((tmp = virXMLPropString(cur, "event_idx"))) { - int value; - if ((value = virTristateSwitchTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown disk event_idx mode '%s'"), tmp); - return -1; - } - def->event_idx = value; - } - VIR_FREE(tmp); + if (virXMLPropTristateSwitch(cur, "event_idx", false, &def->event_idx) < 0) + return -1; - if ((tmp = virXMLPropString(cur, "copy_on_read"))) { - int value; - if ((value = virTristateSwitchTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown disk copy_on_read mode '%s'"), tmp); - return -1; - } - def->copy_on_read = value; - } - VIR_FREE(tmp); + if (virXMLPropTristateSwitch(cur, "copy_on_read", false, &def->copy_on_read) < 0) + return -1; 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 | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index f17c979e31..6a43fb2588 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10435,7 +10435,6 @@ 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; actual = g_new0(virDomainActualNetDef, 1); @@ -10463,16 +10462,9 @@ virDomainActualNetDefParseXML(xmlNodePtr node, goto error; } - if ((trustGuestRxFilters = virXMLPropString(node, "trustGuestRxFilters"))) { - int value; - if ((value = virTristateBoolTypeFromString(trustGuestRxFilters)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown trustGuestRxFilters value '%s'"), - trustGuestRxFilters); - goto error; - } - actual->trustGuestRxFilters = value; - } + if (virXMLPropTristateBool(node, "trustGuestRxFilters", false, + &actual->trustGuestRxFilters) < 0) + goto error; 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 6a43fb2588..7e17ded2a1 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10618,7 +10618,6 @@ virDomainChrSourceReconnectDefParseXML(virDomainChrSourceReconnectDefPtr def, xmlNodePtr node, xmlXPathContextPtr ctxt) { - int tmpVal; VIR_XPATH_NODE_AUTORESTORE(ctxt) xmlNodePtr cur; g_autofree char *tmp = NULL; @@ -10626,16 +10625,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 (virXMLPropTristateBool(cur, "enabled", false, &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 | 180 +++++++++++------------------------------ 1 file changed, 49 insertions(+), 131 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 7e17ded2a1..97eb1b6f8a 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10719,7 +10719,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; @@ -10729,7 +10728,6 @@ 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; const char *prefix = xmlopt ? xmlopt->config.netPrefix : NULL; @@ -10749,16 +10747,9 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt, def->type = VIR_DOMAIN_NET_TYPE_USER; } - if ((trustGuestRxFilters = virXMLPropString(node, "trustGuestRxFilters"))) { - int value; - if ((value = virTristateBoolTypeFromString(trustGuestRxFilters)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown trustGuestRxFilters value '%s'"), - trustGuestRxFilters); - goto error; - } - def->trustGuestRxFilters = value; - } + if (virXMLPropTristateBool(node, "trustGuestRxFilters", false, + &def->trustGuestRxFilters) < 0) + goto error; cur = node->children; while (cur != NULL) { @@ -11328,128 +11319,55 @@ 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 (virXMLPropTristateSwitch(tmpNode, "csum", false, + &def->driver.virtio.host.csum) < 0) + goto error; + + if (virXMLPropTristateSwitch(tmpNode, "gso", false, + &def->driver.virtio.host.gso) < 0) + goto error; + + if (virXMLPropTristateSwitch(tmpNode, "tso4", false, + &def->driver.virtio.host.tso4) < 0) + goto error; + + if (virXMLPropTristateSwitch(tmpNode, "tso6", false, + &def->driver.virtio.host.tso6) < 0) + goto error; + + if (virXMLPropTristateSwitch(tmpNode, "ecn", false, + &def->driver.virtio.host.ecn) < 0) + goto error; + + if (virXMLPropTristateSwitch(tmpNode, "ufo", false, + &def->driver.virtio.host.ufo) < 0) + goto error; + + if (virXMLPropTristateSwitch(tmpNode, "mrg_rxbuf", false, + &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 (virXMLPropTristateSwitch(tmpNode, "csum", false, + &def->driver.virtio.guest.csum) < 0) + goto error; + + if (virXMLPropTristateSwitch(tmpNode, "tso4", false, + &def->driver.virtio.guest.tso4) < 0) + goto error; + + if (virXMLPropTristateSwitch(tmpNode, "tso6", false, + &def->driver.virtio.guest.tso6) < 0) + goto error; + + if (virXMLPropTristateSwitch(tmpNode, "ecn", false, + &def->driver.virtio.guest.ecn) < 0) + goto error; + + if (virXMLPropTristateSwitch(tmpNode, "ufo", false, + &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 | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 97eb1b6f8a..7fb096e6d9 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -11703,17 +11703,8 @@ virDomainChrSourceDefParseTCP(virDomainChrSourceDefPtr def, def->data.tcp.host = virXMLPropString(source, "host"); def->data.tcp.service = virXMLPropString(source, "service"); - if ((tmp = virXMLPropString(source, "tls"))) { - int value; - if ((value = virTristateBoolTypeFromString(tmp)) <= 0) { - virReportError(VIR_ERR_XML_ERROR, - _("unknown chardev 'tls' setting '%s'"), - tmp); - return -1; - } - def->data.tcp.haveTLS = value; - VIR_FREE(tmp); - } + if (virXMLPropTristateBool(source, "tls", false, &def->data.tcp.haveTLS) < 0) + return -1; 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 | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 7fb096e6d9..0f3be88235 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -11777,20 +11777,11 @@ static int virDomainChrSourceDefParseFile(virDomainChrSourceDefPtr def, xmlNodePtr source) { - g_autofree char *append = NULL; - def->data.file.path = virXMLPropString(source, "path"); - if ((append = virXMLPropString(source, "append"))) { - int value; - if ((value = virTristateSwitchTypeFromString(append)) <= 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Invalid append attribute value '%s'"), - append); - return -1; - } - def->data.file.append = value; - } + if (virXMLPropTristateSwitch(source, "append", false, + &def->data.file.append) < 0) + return -1; return 0; } -- 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 0f3be88235..842a134220 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -11812,20 +11812,10 @@ static int virDomainChrSourceDefParseLog(virDomainChrSourceDefPtr def, xmlNodePtr log) { - g_autofree char *append = NULL; - def->logfile = virXMLPropString(log, "file"); - if ((append = virXMLPropString(log, "append"))) { - int value; - if ((value = virTristateSwitchTypeFromString(append)) <= 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Invalid append attribute value '%s'"), - append); - return -1; - } - def->logappend = value; - } + if (virXMLPropTristateSwitch(log, "append", false, &def->logappend) < 0) + return -1; return 0; } -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 842a134220..35320fe1e5 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13043,7 +13043,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) @@ -13102,15 +13101,9 @@ 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 ((virXMLPropTristateBool(node, "powerControl", false, + &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 35320fe1e5..4684496522 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13139,10 +13139,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; @@ -13161,20 +13159,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 (virXMLPropTristateBool(glNode, "enable", false, &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 | 75 ++++++------------------------------------ 1 file changed, 10 insertions(+), 65 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 4684496522..d78d09a4b9 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13396,23 +13396,10 @@ virDomainGraphicsDefParseXMLSpice(virDomainGraphicsDefPtr def, def->data.spice.zlib = compressionVal; } else if (virXMLNodeNameEqual(cur, "playback")) { - int compressionVal; - g_autofree char *compression = virXMLPropString(cur, "compression"); - - if (!compression) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("spice playback missing compression")); - return -1; - } - - if ((compressionVal = - virTristateSwitchTypeFromString(compression)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("unknown spice playback compression")); + if (virXMLPropTristateSwitch(cur, "compression", true, + &def->data.spice.playback) < 0) return -1; - } - def->data.spice.playback = compressionVal; } else if (virXMLNodeNameEqual(cur, "streaming")) { int modeVal; g_autofree char *mode = virXMLPropString(cur, "mode"); @@ -13431,62 +13418,20 @@ virDomainGraphicsDefParseXMLSpice(virDomainGraphicsDefPtr def, def->data.spice.streaming = modeVal; } else if (virXMLNodeNameEqual(cur, "clipboard")) { - int copypasteVal; - g_autofree char *copypaste = virXMLPropString(cur, "copypaste"); - - if (!copypaste) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("spice clipboard missing copypaste")); + if (virXMLPropTristateBool(cur, "copypaste", true, + &def->data.spice.copypaste) < 0) 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 (!enable) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("spice filetransfer missing enable")); + if (virXMLPropTristateBool(cur, "enable", true, + &def->data.spice.filetransfer) < 0) return -1; - } - - if ((enableVal = - virTristateBoolTypeFromString(enable)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown enable value '%s'"), enable); - 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"); + def->data.spice.rendernode = virXMLPropString(cur, + "rendernode"); - if (!enable) { - 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); + if (virXMLPropTristateBool(cur, "enable", true, + &def->data.spice.gl) < 0) 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 d78d09a4b9..dff554874f 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13716,8 +13716,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; @@ -13726,21 +13724,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 (virXMLPropTristateBool(node, "mixingEngine", false, &def->mixingEngine) < 0) return -1; - } - if (fixedSettings && - ((def->fixedSettings = - virTristateBoolTypeFromString(fixedSettings)) <= 0)) { - virReportError(VIR_ERR_XML_ERROR, - _("unknown 'fixedSettings' value '%s'"), fixedSettings); + if (virXMLPropTristateBool(node, "fixedSettings", false, &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 dff554874f..afd37e3e49 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13822,19 +13822,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 (virXMLPropTristateBool(node, "exactName", false, &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 afd37e3e49..c341091c99 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13837,18 +13837,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 (virXMLPropTristateBool(node, "tryPoll", false, &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 | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index c341091c99..0b5d8e5164 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13969,23 +13969,15 @@ 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 (virXMLPropTristateBool(node, "tryMMap", false, + &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 (virXMLPropTristateBool(node, "exclusive", false, + &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 | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 0b5d8e5164..6b98d34eb4 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -14211,8 +14211,6 @@ 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; def = g_new0(virDomainMemballoonDef, 1); @@ -14229,25 +14227,12 @@ virDomainMemballoonDefParseXML(virDomainXMLOptionPtr xmlopt, goto error; } - if ((deflate = virXMLPropString(node, "autodeflate"))) { - int value; - if ((value = virTristateSwitchTypeFromString(deflate)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid autodeflate attribute value '%s'"), deflate); - goto error; - } - def->autodeflate = value; - } + if (virXMLPropTristateSwitch(node, "autodeflate", false, &def->autodeflate) < 0) + goto error; - if ((freepage_reporting = virXMLPropString(node, "freePageReporting"))) { - int value; - if ((value = virTristateSwitchTypeFromString(freepage_reporting)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid freePageReporting attribute value '%s'"), freepage_reporting); - goto error; - } - def->free_page_reporting = value; - } + if (virXMLPropTristateSwitch(node, "freePageReporting", false, + &def->free_page_reporting) < 0) + goto error; 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 | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 6b98d34eb4..ef1a9cdab2 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -14358,17 +14358,9 @@ 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 (virXMLPropTristateSwitch(msi, "ioeventfd", false, + &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 | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index ef1a9cdab2..4debb895e9 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -15442,7 +15442,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")); @@ -15461,18 +15461,9 @@ virDomainPerfEventDefParseXML(virDomainPerfDefPtr perf, return -1; } - if (!(enabled = virXMLPropString(node, "enabled"))) { - virReportError(VIR_ERR_XML_ERROR, - _("missing state of perf event '%s'"), name); - return -1; - } - - if ((perf->events[event] = virTristateBoolTypeFromString(enabled)) < 0) { - virReportError(VIR_ERR_XML_ERROR, - _("invalid state '%s' of perf event '%s'"), - enabled, name); + if (virXMLPropTristateBool(node, "enabled", true, &enabled) < 0) return -1; - } + perf->events[event] = enabled; 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 4debb895e9..0b009bb237 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -15705,16 +15705,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 (virXMLPropTristateBool(memdevNode, "discard", false, &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 0b009bb237..c7d2e86dfe 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -15776,40 +15776,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 (virXMLPropTristateSwitch(driver, "intremap", false, &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 (virXMLPropTristateSwitch(driver, "caching_mode", false, &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 (virXMLPropTristateSwitch(driver, "iotlb", false, &iommu->iotlb) < 0) + return NULL; + + if (virXMLPropTristateSwitch(driver, "eim", false, &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 c7d2e86dfe..3289c9fa12 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -15841,17 +15841,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 (virXMLPropTristateBool(cid, "auto", false, &vsock->auto_cid) < 0) + return NULL; } if (virDomainDeviceInfoParseXML(xmlopt, node, ctxt, &vsock->info, flags) < 0) -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 146 ++++++++++------------------------------- 1 file changed, 33 insertions(+), 113 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 3289c9fa12..dfa8b98aae 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -18236,6 +18236,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, @@ -18245,16 +18246,8 @@ 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 (virXMLPropTristateSwitch(nodes[i], "eoi", false, &def->apic_eoi) < 0) + return -1; G_GNUC_FALLTHROUGH; case VIR_DOMAIN_FEATURE_ACPI: case VIR_DOMAIN_FEATURE_PAE: @@ -18286,16 +18279,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 (virXMLPropTristateSwitch(nodes[i], "state", false, &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: @@ -18403,18 +18390,9 @@ 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)); - return -1; - } - if ((def->features[val] = virTristateSwitchTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown state attribute '%s' of feature '%s'"), - tmp, virDomainFeatureTypeToString(val)); + if (virXMLPropTristateSwitch(nodes[i], "state", true, &triSwitch) < 0) return -1; - } + def->features[val] = triSwitch; break; /* coverity[dead_error_begin] */ @@ -18426,13 +18404,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, @@ -18443,23 +18420,10 @@ virDomainFeaturesDefParse(virDomainDefPtr def, ctxt->node = nodes[i]; - if (!(tmp = virXMLPropString(nodes[i], "state"))) { - virReportError(VIR_ERR_XML_ERROR, - _("missing 'state' attribute for " - "HyperV Enlightenment feature '%s'"), - nodes[i]->name); - 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); + if (virXMLPropTristateSwitch(nodes[i], "state", true, &state) < 0) return -1; - } - def->hyperv_features[feature] = value; + def->hyperv_features[feature] = state; switch ((virDomainHyperv) feature) { case VIR_DOMAIN_HYPERV_RELAXED: @@ -18477,7 +18441,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, @@ -18496,7 +18460,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], @@ -18533,13 +18497,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"), @@ -18547,33 +18510,21 @@ virDomainFeaturesDefParse(virDomainDefPtr def, return -1; } - if (!(tmp = virXMLPropString(nodes[i], "state"))) { - 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"); + if (virXMLPropTristateSwitch(nodes[i], "state", true, &state) < 0) return -1; - } - - def->hyperv_stimer_direct = value; + def->hyperv_stimer_direct = state; } 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) { @@ -18587,23 +18538,9 @@ virDomainFeaturesDefParse(virDomainDefPtr def, case VIR_DOMAIN_KVM_HIDDEN: case VIR_DOMAIN_KVM_DEDICATED: case VIR_DOMAIN_KVM_POLLCONTROL: - if (!(tmp = virXMLPropString(nodes[i], "state"))) { - virReportError(VIR_ERR_XML_ERROR, - _("missing 'state' attribute for " - "KVM feature '%s'"), - nodes[i]->name); - return -1; - } - - if ((value = virTristateSwitchTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid value of state argument " - "for KVM feature '%s'"), - nodes[i]->name); + if (virXMLPropTristateSwitch(nodes[i], "state", true, &state) < 0) return -1; - } - - def->kvm_features[feature] = value; + def->kvm_features[feature] = state; break; /* coverity[dead_error_begin] */ @@ -18616,14 +18553,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, @@ -18632,30 +18568,17 @@ virDomainFeaturesDefParse(virDomainDefPtr def, return -1; } - if (!(tmp = virXMLPropString(nodes[i], "state"))) { - virReportError(VIR_ERR_XML_ERROR, - _("missing 'state' attribute for " - "Xen feature '%s'"), - nodes[i]->name); - return -1; - } - - if ((value = virTristateSwitchTypeFromString(tmp)) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid value of state argument " - "for Xen feature '%s'"), - nodes[i]->name); + if (virXMLPropTristateSwitch(nodes[i], "state", true, &state) < 0) return -1; - } - def->xen_features[feature] = value; + def->xen_features[feature] = state; 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"))) { @@ -18725,7 +18648,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, @@ -18733,16 +18656,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 (virXMLPropTristateSwitch(nodes[i], "state", false, &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 | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index dfa8b98aae..55405d129b 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -18712,14 +18712,12 @@ 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"); - if (!fwAutoSelect) { - readonly_str = virXMLPropString(node, "readonly"); + if (virXMLPropTristateBool(node, "readonly", false, &loader->readonly) < 0) + return -1; + type_str = virXMLPropString(node, "type"); if (!(loader->path = virXMLNodeContentString(node))) return -1; @@ -18728,25 +18726,8 @@ virDomainLoaderDefParseXML(xmlNodePtr node, VIR_FREE(loader->path); } - if (readonly_str) { - int value; - if ((value = virTristateBoolTypeFromString(readonly_str)) <= 0) { - virReportError(VIR_ERR_XML_DETAIL, - _("unknown readonly value: %s"), readonly_str); - return -1; - } - loader->readonly = value; - } - - if (secure_str) { - int value; - if ((value = virTristateBoolTypeFromString(secure_str)) <= 0) { - virReportError(VIR_ERR_XML_DETAIL, - _("unknown secure value: %s"), secure_str); - return -1; - } - loader->secure = value; - } + if (virXMLPropTristateBool(node, "secure", false, &loader->secure) < 0) + return -1; if (type_str) { int type; -- 2.26.2

Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- src/conf/domain_conf.c | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 55405d129b..97c7a3ec28 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -18998,7 +18998,7 @@ virDomainVcpuParse(virDomainDefPtr def, for (i = 0; i < n; i++) { virDomainVcpuDefPtr vcpu; - int state; + virTristateBool state; unsigned int id; unsigned int order; @@ -19020,31 +19020,13 @@ virDomainVcpuParse(virDomainDefPtr def, vcpu = virDomainDefGetVcpu(def, id); - if (!(tmp = virXMLPropString(nodes[i], "enabled"))) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("missing vcpu enabled state")); + if (virXMLPropTristateBool(nodes[i], "enabled", true, &state) < 0) 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 (virXMLPropTristateBool(nodes[i], "hotpluggable", false, + &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..ce85ed8bad 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 (virXMLPropTristateBool(node, "backup", false, &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 ce85ed8bad..d3ea9ce4a3 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 (virXMLPropTristateBool(node, "tls", false, &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 | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/conf/device_conf.c b/src/conf/device_conf.c index 0dd60985e9..8d0540bb02 100644 --- a/src/conf/device_conf.c +++ b/src/conf/device_conf.c @@ -214,7 +214,6 @@ 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"); memset(addr, 0, sizeof(*addr)); @@ -246,16 +245,9 @@ virPCIDeviceAddressParseXML(xmlNodePtr node, return -1; } - if (multi) { - int value; - if ((value = virTristateSwitchTypeFromString(multi)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("Unknown value '%s' for <address> 'multifunction' attribute"), - multi); - return -1; - } - addr->multi = value; - } + if (virXMLPropTristateSwitch(node, "multifunction", false, &addr->multi) < 0) + return -1; + 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..bbd18ba163 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 (virXMLPropTristateBool(node, "ipv6", false, &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 | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/conf/numa_conf.c b/src/conf/numa_conf.c index 64b93fd7d1..2555eeaef9 100644 --- a/src/conf/numa_conf.c +++ b/src/conf/numa_conf.c @@ -1079,17 +1079,9 @@ 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 (virXMLPropTristateBool(nodes[i], "discard", false, + &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 | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/conf/storage_adapter_conf.c b/src/conf/storage_adapter_conf.c index 69062b4b58..a19920c8b2 100644 --- a/src/conf/storage_adapter_conf.c +++ b/src/conf/storage_adapter_conf.c @@ -64,28 +64,16 @@ static int virStorageAdapterParseXMLFCHost(xmlNodePtr node, virStorageAdapterFCHostPtr fchost) { - char *managed = NULL; + if (virXMLPropTristateBool(node, "managed", false, &fchost->managed) < 0) + return -1; fchost->parent = virXMLPropString(node, "parent"); - if ((managed = virXMLPropString(node, "managed"))) { - int value; - if ((value = virTristateBoolTypeFromString(managed)) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown fc_host managed setting '%s'"), - managed); - VIR_FREE(managed); - return -1; - } - fchost->managed = value; - } - 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, 4 insertions(+), 12 deletions(-) diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c index 6116b04d44..286cdf105d 100644 --- a/src/conf/storage_conf.c +++ b/src/conf/storage_conf.c @@ -605,7 +605,6 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, goto cleanup; for (i = 0; i < nsource; i++) { - g_autofree char *partsep = NULL; virStoragePoolSourceDevice dev = { .path = NULL }; dev.path = virXMLPropString(nodeset[i], "path"); @@ -615,17 +614,10 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt, goto cleanup; } - partsep = virXMLPropString(nodeset[i], "part_separator"); - if (partsep) { - int value = virTristateBoolTypeFromString(partsep); - if (value <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid part_separator setting '%s'"), - partsep); - virStoragePoolSourceDeviceClear(&dev); - goto cleanup; - } - dev.part_separator = value; + if (virXMLPropTristateBool(nodeset[i], "part_separator", false, + &dev.part_separator) < 0) { + virStoragePoolSourceDeviceClear(&dev); + goto cleanup; } if (VIR_APPEND_ELEMENT(source->devices, source->ndevice, dev) < 0) { -- 2.26.2

On 3/19/21 4:56 PM, 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.
V1: https://listman.redhat.com/archives/libvir-list/2021-March/msg00853.html V2: https://listman.redhat.com/archives/libvir-list/2021-March/msg00994.html
Changes since V2: * Fixed the -Wtautological-unsigned-enum-zero-compare issues in the first part of the series. These issues were solved later in the series, but this change makes it easier to bisect in the future.
I was thinking about only re-sending the first couple of patches, but the latter part would have endet up with quite a few conflicts, so I am sending it in its entirety, again. Sorry for the spam!
Cheers, Tim
Alternatively, rewrite to virXMLPropTristateXXX() could have gone in the first, followed by change of struct members to virTristateXXX which would have produced smalled diff. But I guess it doesn't matter.
Tim Wiederhake (51): conf: Use virTristateXXX in virStorageSource conf: Use virTristateXXX in virStorageSourceNVMeDef conf: Use virTristateXXX in virDomainDeviceInfo conf: Use virTristateXXX in virDomainDiskDef conf: Use virTristateXXX in virDomainActualNetDef conf: Use virTristateXXX in virDomainNetDef conf: Use virTristateXXX in virDomainChrSourceDef conf: Use virTristateXXX in virDomainGraphicsDef conf: Use virTristateXXX in virDomainMemballoonDef conf: Use virTristateXXX in virDomainLoaderDef conf: Use virTristateXXX in virDomainDef conf: Use virTristateXXX in virStorageAdapterFCHost conf: Use virTristateXXX in virStoragePoolSourceDevice conf: Use virTristateXXX in virPCIDeviceAddress virxml: Add virXMLPropTristateBool virxml: Add virXMLPropTristateSwitch domain_conf: Use virXMLPropTristateXXX in virDomainKeyWrapCipherDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainVirtioOptionsParseXML domain_conf: Use virXMLPropTristateXXX in virDomainDeviceInfoParseXML domain_conf: Use virXMLPropTristateXXX in virDomainDiskSourceNetworkParse domain_conf: Use virXMLPropTristateXXX in virDomainDiskSourceNVMeParse domain_conf: Use virXMLPropTristateXXX in virDomainDiskDefDriverParseXML domain_conf: Use virXMLPropTristateXXX in virDomainActualNetDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainChrSourceReconnectDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainNetDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainChrSourceDefParseTCP domain_conf: Use virXMLPropTristateXXX in virDomainChrSourceDefParseFile domain_conf: Use virXMLPropTristateXXX in virDomainChrSourceDefParseLog domain_conf: Use virXMLPropTristateXXX in virDomainGraphicsDefParseXMLVNC domain_conf: Use virXMLPropTristateXXX in virDomainGraphicsDefParseXMLSDL domain_conf: Use virXMLPropTristateXXX in virDomainGraphicsDefParseXMLSpice domain_conf: Use virXMLPropTristateXXX in virDomainAudioCommonParse domain_conf: Use virXMLPropTristateXXX in virDomainAudioJackParse domain_conf: Use virXMLPropTristateXXX in virDomainAudioOSSParse domain_conf: Use virXMLPropTristateXXX in virDomainAudioDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainMemballoonDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainShmemDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainPerfEventDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainMemoryDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainIOMMUDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainVsockDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainFeaturesDefParse domain_conf: Use virXMLPropTristateXXX in virDomainLoaderDefParseXML domain_conf: Use virXMLPropTristateXXX in virDomainVcpuParse backup_conf: Use virXMLPropTristateXXX in virDomainBackupDiskDefParseXML backup_conf: Use virXMLPropTristateXXX in virDomainBackupDefParse device_conf: Use virXMLPropTristateXXX in virPCIDeviceAddressParseXML network_conf: Use virXMLPropTristateXXX in virNetworkForwardNatDefParseXML numa_conf: Use virXMLPropTristateXXX in virDomainNumaDefParseXML storage_adapter_conf: Use virXMLPropTristateXXX in virStorageAdapterParseXMLFCHost storage_conf: Use virXMLPropTristateXXX in virStoragePoolDefParseSource
src/conf/backup_conf.c | 32 +- src/conf/device_conf.c | 8 +- src/conf/device_conf.h | 4 +- src/conf/domain_conf.c | 794 +++++++------------------------- src/conf/domain_conf.h | 28 +- src/conf/network_conf.c | 15 +- src/conf/numa_conf.c | 14 +- src/conf/storage_adapter_conf.c | 14 +- src/conf/storage_adapter_conf.h | 2 +- src/conf/storage_conf.c | 17 +- src/conf/storage_conf.h | 2 +- src/conf/storage_source_conf.h | 4 +- src/libvirt_private.syms | 2 + src/qemu/qemu_command.c | 3 +- src/qemu/qemu_hotplug.c | 2 +- src/util/virpci.h | 2 +- src/util/virxml.c | 82 ++++ src/util/virxml.h | 9 + 18 files changed, 301 insertions(+), 733 deletions(-)
Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Will push at the end of day, to give others chance to object. Michal
participants (3)
-
Daniel P. Berrangé
-
Michal Privoznik
-
Tim Wiederhake