[PATCH 0/4] Refactoring node_device_conf

This refactors file node_device_conf in order to remove VIR_FREE (if possible) and removes redundant variables and labels. Kristina Hanicova (4): node_device_conf: Use g_autofree where possible node_device_conf: Refactoring functions in order to remove VIR_FREE node_device_conf: use g_autoptr in virNodeDevCapPCIDevIommuGroupParseXML() node_device_conf: Remove redundant variables/labels src/conf/node_device_conf.c | 324 +++++++++++++----------------------- 1 file changed, 118 insertions(+), 206 deletions(-) -- 2.29.2

Signed-off-by: Kristina Hanicova <khanicov@redhat.com> --- src/conf/node_device_conf.c | 76 +++++++++++-------------------------- 1 file changed, 23 insertions(+), 53 deletions(-) diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c index 1093a461af..20fafd2c15 100644 --- a/src/conf/node_device_conf.c +++ b/src/conf/node_device_conf.c @@ -779,7 +779,7 @@ virNodeDevCapDRMParseXML(xmlXPathContextPtr ctxt, { VIR_XPATH_NODE_AUTORESTORE(ctxt) int ret = -1, val; - char *type = NULL; + g_autofree char *type = NULL; ctxt->node = node; @@ -795,7 +795,6 @@ virNodeDevCapDRMParseXML(xmlXPathContextPtr ctxt, ret = 0; out: - VIR_FREE(type); return ret; } @@ -807,7 +806,7 @@ virNodeDevCapMdevTypesParseXML(xmlXPathContextPtr ctxt, { int ret = -1; xmlNodePtr orignode = NULL; - xmlNodePtr *nodes = NULL; + g_autofree xmlNodePtr *nodes = NULL; int ntypes = -1; virMediatedDeviceTypePtr type = NULL; size_t i; @@ -859,7 +858,6 @@ virNodeDevCapMdevTypesParseXML(xmlXPathContextPtr ctxt, ret = 0; cleanup: - VIR_FREE(nodes); virMediatedDeviceTypeFree(type); ctxt->node = orignode; return ret; @@ -1097,7 +1095,7 @@ virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt, virNodeDevCapStoragePtr storage) { VIR_XPATH_NODE_AUTORESTORE(ctxt) - xmlNodePtr *nodes = NULL; + g_autofree xmlNodePtr *nodes = NULL; size_t i; int n, ret = -1; unsigned long long val; @@ -1122,7 +1120,7 @@ virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt, goto out; for (i = 0; i < n; i++) { - char *type = virXMLPropString(nodes[i], "type"); + g_autofree char *type = virXMLPropString(nodes[i], "type"); if (!type) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -1150,7 +1148,6 @@ virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt, if (virNodeDevCapsDefParseULongLong("number(./media_size[1])", ctxt, &val, def, _("no removable media size supplied for '%s'"), _("invalid removable media size supplied for '%s'")) < 0) { - VIR_FREE(type); goto out; } storage->removable_media_size = val; @@ -1160,11 +1157,8 @@ virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown storage capability type '%s' for '%s'"), type, def->name); - VIR_FREE(type); goto out; } - - VIR_FREE(type); } if (!(storage->flags & VIR_NODE_DEV_CAP_STORAGE_REMOVABLE)) { @@ -1178,7 +1172,6 @@ virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt, ret = 0; out: - VIR_FREE(nodes); return ret; } @@ -1233,7 +1226,7 @@ virNodeDevCapSCSITargetParseXML(xmlXPathContextPtr ctxt, virNodeDevCapSCSITargetPtr scsi_target) { VIR_XPATH_NODE_AUTORESTORE(ctxt) - xmlNodePtr *nodes = NULL; + g_autofree xmlNodePtr *nodes = NULL; int ret = -1, n = 0; size_t i; char *type = NULL; @@ -1295,7 +1288,6 @@ virNodeDevCapSCSITargetParseXML(xmlXPathContextPtr ctxt, out: VIR_FREE(type); - VIR_FREE(nodes); return ret; } @@ -1309,7 +1301,7 @@ virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt, const char *virt_type) { VIR_XPATH_NODE_AUTORESTORE(ctxt) - xmlNodePtr *nodes = NULL; + g_autofree xmlNodePtr *nodes = NULL; int ret = -1, n = 0; size_t i; char *type = NULL; @@ -1397,7 +1389,6 @@ virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt, out: VIR_FREE(type); - VIR_FREE(nodes); return ret; } @@ -1413,7 +1404,7 @@ virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt, size_t i = -1; int ret = -1, n = -1; char *tmp = NULL; - xmlNodePtr *nodes = NULL; + g_autofree xmlNodePtr *nodes = NULL; ctxt->node = node; @@ -1472,7 +1463,6 @@ virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt, ret = 0; out: - VIR_FREE(nodes); VIR_FREE(tmp); return ret; } @@ -1595,8 +1585,8 @@ virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt, virNodeDevCapPCIDevPtr pci_dev) { VIR_XPATH_NODE_AUTORESTORE(ctxt) - xmlNodePtr *addrNodes = NULL; - char *numberStr = NULL; + g_autofree xmlNodePtr *addrNodes = NULL; + g_autofree char *numberStr = NULL; int nAddrNodes, ret = -1; size_t i; virPCIDeviceAddressPtr pciAddr = NULL; @@ -1637,8 +1627,6 @@ virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt, ret = 0; cleanup: - VIR_FREE(numberStr); - VIR_FREE(addrNodes); VIR_FREE(pciAddr); return ret; } @@ -1651,7 +1639,8 @@ virPCIEDeviceInfoLinkParseXML(xmlXPathContextPtr ctxt, { VIR_XPATH_NODE_AUTORESTORE(ctxt) int ret = -1, speed; - char *speedStr = NULL, *portStr = NULL; + g_autofree char *speedStr = NULL; + g_autofree char *portStr = NULL; ctxt->node = linkNode; @@ -1684,8 +1673,6 @@ virPCIEDeviceInfoLinkParseXML(xmlXPathContextPtr ctxt, ret = 0; cleanup: - VIR_FREE(portStr); - VIR_FREE(speedStr); return ret; } @@ -1752,9 +1739,9 @@ virNodeDevPCICapSRIOVVirtualParseXML(xmlXPathContextPtr ctxt, virNodeDevCapPCIDevPtr pci_dev) { int ret = -1; - xmlNodePtr *addresses = NULL; + g_autofree xmlNodePtr *addresses = NULL; int naddresses = virXPathNodeSet("./address", ctxt, &addresses); - char *maxFuncsStr = virXPathString("string(./@maxCount)", ctxt); + g_autofree char *maxFuncsStr = virXPathString("string(./@maxCount)", ctxt); size_t i; if (naddresses < 0) @@ -1787,8 +1774,6 @@ virNodeDevPCICapSRIOVVirtualParseXML(xmlXPathContextPtr ctxt, pci_dev->flags |= VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION; ret = 0; cleanup: - VIR_FREE(addresses); - VIR_FREE(maxFuncsStr); return ret; } @@ -1798,7 +1783,7 @@ virNodeDevPCICapabilityParseXML(xmlXPathContextPtr ctxt, xmlNodePtr node, virNodeDevCapPCIDevPtr pci_dev) { - char *type = virXMLPropString(node, "type"); + g_autofree char *type = virXMLPropString(node, "type"); VIR_XPATH_NODE_AUTORESTORE(ctxt) int ret = -1; @@ -1830,7 +1815,6 @@ virNodeDevPCICapabilityParseXML(xmlXPathContextPtr ctxt, ret = 0; cleanup: - VIR_FREE(type); return ret; } @@ -1844,11 +1828,11 @@ virNodeDevCapPCIDevParseXML(xmlXPathContextPtr ctxt, VIR_XPATH_NODE_AUTORESTORE(ctxt) xmlNodePtr iommuGroupNode; xmlNodePtr pciExpress; - xmlNodePtr *nodes = NULL; + g_autofree xmlNodePtr *nodes = NULL; int n = 0; int ret = -1; virPCIEDeviceInfoPtr pci_express = NULL; - char *tmp = NULL; + g_autofree char *tmp = NULL; size_t i = 0; ctxt->node = node; @@ -1860,7 +1844,6 @@ virNodeDevCapPCIDevParseXML(xmlXPathContextPtr ctxt, _("invalid PCI class supplied for '%s'"), def->name); goto out; } - VIR_FREE(tmp); } else { pci_dev->klass = -1; } @@ -1911,7 +1894,6 @@ virNodeDevCapPCIDevParseXML(xmlXPathContextPtr ctxt, if (virNodeDevPCICapabilityParseXML(ctxt, nodes[i], pci_dev) < 0) goto out; } - VIR_FREE(nodes); if ((iommuGroupNode = virXPathNode("./iommuGroup[1]", ctxt))) { if (virNodeDevCapPCIDevIommuGroupParseXML(ctxt, iommuGroupNode, @@ -1940,8 +1922,6 @@ virNodeDevCapPCIDevParseXML(xmlXPathContextPtr ctxt, ret = 0; out: - VIR_FREE(nodes); - VIR_FREE(tmp); virPCIEDeviceInfoFree(pci_express); return ret; } @@ -1957,7 +1937,7 @@ virNodeDevCapSystemParseXML(xmlXPathContextPtr ctxt, virNodeDevCapSystemFirmwarePtr firmware = &syscap->firmware; VIR_XPATH_NODE_AUTORESTORE(ctxt) int ret = -1; - char *tmp; + g_autofree char *tmp = NULL; ctxt->node = node; @@ -1977,10 +1957,8 @@ virNodeDevCapSystemParseXML(xmlXPathContextPtr ctxt, if (virUUIDParse(tmp, hardware->uuid) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("malformed uuid element for '%s'"), def->name); - VIR_FREE(tmp); goto out; } - VIR_FREE(tmp); firmware->vendor_name = virXPathString("string(./firmware/vendor[1])", ctxt); firmware->version = virXPathString("string(./firmware/version[1])", ctxt); @@ -2063,7 +2041,7 @@ virNodeDevCapsDefParseXML(xmlXPathContextPtr ctxt, const char *virt_type) { virNodeDevCapsDefPtr caps; - char *tmp; + g_autofree char *tmp = NULL; int val, ret = -1; caps = g_new0(virNodeDevCapsDef, 1); @@ -2078,11 +2056,9 @@ virNodeDevCapsDefParseXML(xmlXPathContextPtr ctxt, if ((val = virNodeDevCapTypeFromString(tmp)) < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unknown capability type '%s'"), tmp); - VIR_FREE(tmp); goto error; } caps->data.type = val; - VIR_FREE(tmp); switch (caps->data.type) { case VIR_NODE_DEV_CAP_SYSTEM: @@ -2198,7 +2174,7 @@ virNodeDeviceDefParseXML(xmlXPathContextPtr ctxt, for (i = 0, m = 0; i < n; i++) { xmlNodePtr node = nodes[i]; - char *tmp = virXMLPropString(node, "type"); + g_autofree char *tmp = virXMLPropString(node, "type"); int val; if (!tmp) { @@ -2212,10 +2188,8 @@ virNodeDeviceDefParseXML(xmlXPathContextPtr ctxt, if (val < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unknown devnode type '%s'"), tmp); - VIR_FREE(tmp); goto error; } - VIR_FREE(tmp); switch ((virNodeDevDevnodeType)val) { case VIR_NODE_DEV_DEVNODE_DEV: @@ -2567,7 +2541,7 @@ virNodeDeviceCapsListExport(virNodeDeviceDefPtr def, virNodeDevCapType **list) { virNodeDevCapsDefPtr caps = NULL; - virNodeDevCapType *tmp = NULL; + g_autofree virNodeDevCapType *tmp = NULL; bool want_list = !!list; int ncaps = 0; int ret = -1; @@ -2639,7 +2613,6 @@ virNodeDeviceCapsListExport(virNodeDeviceDefPtr def, *list = g_steal_pointer(&tmp); ret = ncaps; cleanup: - VIR_FREE(tmp); return ret; } @@ -2649,7 +2622,7 @@ virNodeDeviceCapsListExport(virNodeDeviceDefPtr def, int virNodeDeviceGetSCSIHostCaps(virNodeDevCapSCSIHostPtr scsi_host) { - char *tmp = NULL; + g_autofree char *tmp = NULL; int ret = -1; if ((scsi_host->unique_id = @@ -2698,7 +2671,6 @@ virNodeDeviceGetSCSIHostCaps(virNodeDevCapSCSIHostPtr scsi_host) goto cleanup; } - VIR_FREE(tmp); if (!(tmp = virVHBAGetConfig(NULL, scsi_host->host, "npiv_vports_inuse"))) { VIR_WARN("Failed to read npiv_vports_inuse for host%d", @@ -2723,7 +2695,6 @@ virNodeDeviceGetSCSIHostCaps(virNodeDevCapSCSIHostPtr scsi_host) VIR_FREE(scsi_host->wwpn); VIR_FREE(scsi_host->fabric_wwn); } - VIR_FREE(tmp); return ret; } @@ -2733,7 +2704,8 @@ virNodeDeviceGetSCSITargetCaps(const char *sysfsPath, virNodeDevCapSCSITargetPtr scsi_target) { int ret = -1; - char *dir = NULL, *rport = NULL; + g_autofree char *dir = NULL; + g_autofree char *rport = NULL; VIR_DEBUG("Checking if '%s' is an FC remote port", scsi_target->name); @@ -2763,8 +2735,6 @@ virNodeDeviceGetSCSITargetCaps(const char *sysfsPath, VIR_FREE(scsi_target->wwpn); scsi_target->flags &= ~VIR_NODE_DEV_CAP_FLAG_FC_RPORT; } - VIR_FREE(rport); - VIR_FREE(dir); return ret; } -- 2.29.2

By: * declaration of an autofreed variable in for loop * use of a new variable * removal of VIR_FREE Signed-off-by: Kristina Hanicova <khanicov@redhat.com> --- src/conf/node_device_conf.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c index 20fafd2c15..cfc72299de 100644 --- a/src/conf/node_device_conf.c +++ b/src/conf/node_device_conf.c @@ -1229,7 +1229,6 @@ virNodeDevCapSCSITargetParseXML(xmlXPathContextPtr ctxt, g_autofree xmlNodePtr *nodes = NULL; int ret = -1, n = 0; size_t i; - char *type = NULL; ctxt->node = node; @@ -1245,6 +1244,7 @@ virNodeDevCapSCSITargetParseXML(xmlXPathContextPtr ctxt, goto out; for (i = 0; i < n; ++i) { + g_autofree char *type = NULL; type = virXMLPropString(nodes[i], "type"); if (!type) { @@ -1280,14 +1280,11 @@ virNodeDevCapSCSITargetParseXML(xmlXPathContextPtr ctxt, type, def->name); goto out; } - - VIR_FREE(type); } ret = 0; out: - VIR_FREE(type); return ret; } @@ -1304,7 +1301,6 @@ virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt, g_autofree xmlNodePtr *nodes = NULL; int ret = -1, n = 0; size_t i; - char *type = NULL; ctxt->node = node; @@ -1328,6 +1324,7 @@ virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt, goto out; for (i = 0; i < n; i++) { + g_autofree char *type = NULL; type = virXMLPropString(nodes[i], "type"); if (!type) { @@ -1381,14 +1378,11 @@ virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt, type, def->name); goto out; } - - VIR_FREE(type); } ret = 0; out: - VIR_FREE(type); return ret; } @@ -1403,7 +1397,7 @@ virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt, xmlNodePtr lnk; size_t i = -1; int ret = -1, n = -1; - char *tmp = NULL; + g_autofree char *type = NULL; g_autofree xmlNodePtr *nodes = NULL; ctxt->node = node; @@ -1425,6 +1419,7 @@ virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt, net->features = virBitmapNew(VIR_NET_DEV_FEAT_LAST); for (i = 0; i < n; i++) { + g_autofree char *tmp = NULL; int val; if (!(tmp = virXMLPropString(nodes[i], "name"))) { virReportError(VIR_ERR_XML_ERROR, "%s", @@ -1439,15 +1434,13 @@ virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt, goto out; } ignore_value(virBitmapSetBit(net->features, val)); - VIR_FREE(tmp); } net->subtype = VIR_NODE_DEV_CAP_NET_LAST; - tmp = virXPathString("string(./capability/@type)", ctxt); - if (tmp) { - int val = virNodeDevNetCapTypeFromString(tmp); - VIR_FREE(tmp); + type = virXPathString("string(./capability/@type)", ctxt); + if (type) { + int val = virNodeDevNetCapTypeFromString(type); if (val < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("invalid network type supplied for '%s'"), @@ -1463,7 +1456,6 @@ virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt, ret = 0; out: - VIR_FREE(tmp); return ret; } -- 2.29.2

Signed-off-by: Kristina Hanicova <khanicov@redhat.com> --- src/conf/node_device_conf.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c index cfc72299de..a64be74011 100644 --- a/src/conf/node_device_conf.c +++ b/src/conf/node_device_conf.c @@ -1581,7 +1581,6 @@ virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt, g_autofree char *numberStr = NULL; int nAddrNodes, ret = -1; size_t i; - virPCIDeviceAddressPtr pciAddr = NULL; ctxt->node = iommuGroupNode; @@ -1603,14 +1602,9 @@ virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt, goto cleanup; for (i = 0; i < nAddrNodes; i++) { - virPCIDeviceAddress addr = {0}; - if (virPCIDeviceAddressParseXML(addrNodes[i], &addr) < 0) - goto cleanup; - pciAddr = g_new0(virPCIDeviceAddress, 1); - pciAddr->domain = addr.domain; - pciAddr->bus = addr.bus; - pciAddr->slot = addr.slot; - pciAddr->function = addr.function; + g_autoptr(virPCIDeviceAddress) pciAddr = g_new0(virPCIDeviceAddress, 1); + if (virPCIDeviceAddressParseXML(addrNodes[i], pciAddr) < 0) + return -1; if (VIR_APPEND_ELEMENT(pci_dev->iommuGroupDevices, pci_dev->nIommuGroupDevices, pciAddr) < 0) @@ -1619,7 +1613,6 @@ virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt, ret = 0; cleanup: - VIR_FREE(pciAddr); return ret; } -- 2.29.2

On 3/3/21 5:17 PM, Kristina Hanicova wrote:
Signed-off-by: Kristina Hanicova <khanicov@redhat.com> --- src/conf/node_device_conf.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-)
diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c index cfc72299de..a64be74011 100644 --- a/src/conf/node_device_conf.c +++ b/src/conf/node_device_conf.c @@ -1581,7 +1581,6 @@ virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt, g_autofree char *numberStr = NULL; int nAddrNodes, ret = -1; size_t i; - virPCIDeviceAddressPtr pciAddr = NULL;
ctxt->node = iommuGroupNode;
@@ -1603,14 +1602,9 @@ virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt, goto cleanup;
for (i = 0; i < nAddrNodes; i++) { - virPCIDeviceAddress addr = {0}; - if (virPCIDeviceAddressParseXML(addrNodes[i], &addr) < 0) - goto cleanup; - pciAddr = g_new0(virPCIDeviceAddress, 1); - pciAddr->domain = addr.domain; - pciAddr->bus = addr.bus; - pciAddr->slot = addr.slot; - pciAddr->function = addr.function; + g_autoptr(virPCIDeviceAddress) pciAddr = g_new0(virPCIDeviceAddress, 1); + if (virPCIDeviceAddressParseXML(addrNodes[i], pciAddr) < 0)
I know it's pre-existing, but since you're touching these lines anyway, it would be nice to separate variable declaration block and code block with an empty line.
+ return -1; if (VIR_APPEND_ELEMENT(pci_dev->iommuGroupDevices, pci_dev->nIommuGroupDevices, pciAddr) < 0) @@ -1619,7 +1613,6 @@ virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt,
ret = 0; cleanup: - VIR_FREE(pciAddr); return ret; }
Michal

Signed-off-by: Kristina Hanicova <khanicov@redhat.com> --- src/conf/node_device_conf.c | 213 ++++++++++++++---------------------- 1 file changed, 85 insertions(+), 128 deletions(-) diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c index a64be74011..f13d70ca6c 100644 --- a/src/conf/node_device_conf.c +++ b/src/conf/node_device_conf.c @@ -778,7 +778,7 @@ virNodeDevCapDRMParseXML(xmlXPathContextPtr ctxt, virNodeDevCapDRMPtr drm) { VIR_XPATH_NODE_AUTORESTORE(ctxt) - int ret = -1, val; + int val; g_autofree char *type = NULL; ctxt->node = node; @@ -788,14 +788,11 @@ virNodeDevCapDRMParseXML(xmlXPathContextPtr ctxt, if ((val = virNodeDevDRMTypeFromString(type)) < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unknown drm type '%s' for '%s'"), type, def->name); - goto out; + return -1; } drm->type = val; - ret = 0; - - out: - return ret; + return 0; } @@ -1097,7 +1094,7 @@ virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt, VIR_XPATH_NODE_AUTORESTORE(ctxt) g_autofree xmlNodePtr *nodes = NULL; size_t i; - int n, ret = -1; + int n; unsigned long long val; ctxt->node = node; @@ -1107,7 +1104,7 @@ virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_INTERNAL_ERROR, _("no block device path supplied for '%s'"), def->name); - goto out; + return -1; } storage->bus = virXPathString("string(./bus[1])", ctxt); @@ -1117,7 +1114,7 @@ virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt, storage->serial = virXPathString("string(./serial[1])", ctxt); if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0) - goto out; + return -1; for (i = 0; i < n; i++) { g_autofree char *type = virXMLPropString(nodes[i], "type"); @@ -1126,7 +1123,7 @@ virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_INTERNAL_ERROR, _("missing storage capability type for '%s'"), def->name); - goto out; + return -1; } if (STREQ(type, "hotpluggable")) { @@ -1148,7 +1145,7 @@ virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt, if (virNodeDevCapsDefParseULongLong("number(./media_size[1])", ctxt, &val, def, _("no removable media size supplied for '%s'"), _("invalid removable media size supplied for '%s'")) < 0) { - goto out; + return -1; } storage->removable_media_size = val; @@ -1157,7 +1154,7 @@ virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown storage capability type '%s' for '%s'"), type, def->name); - goto out; + return -1; } } @@ -1166,13 +1163,11 @@ virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt, if (virNodeDevCapsDefParseULongLong("number(./size[1])", ctxt, &val, def, _("no size supplied for '%s'"), _("invalid size supplied for '%s'")) < 0) - goto out; + return -1; storage->size = val; } - ret = 0; - out: - return ret; + return 0; } @@ -1183,7 +1178,6 @@ virNodeDevCapSCSIParseXML(xmlXPathContextPtr ctxt, virNodeDevCapSCSIPtr scsi) { VIR_XPATH_NODE_AUTORESTORE(ctxt) - int ret = -1; ctxt->node = node; @@ -1191,31 +1185,29 @@ virNodeDevCapSCSIParseXML(xmlXPathContextPtr ctxt, &scsi->host, def, _("no SCSI host ID supplied for '%s'"), _("invalid SCSI host ID supplied for '%s'")) < 0) - goto out; + return -1; if (virNodeDevCapsDefParseULong("number(./bus[1])", ctxt, &scsi->bus, def, _("no SCSI bus ID supplied for '%s'"), _("invalid SCSI bus ID supplied for '%s'")) < 0) - goto out; + return -1; if (virNodeDevCapsDefParseULong("number(./target[1])", ctxt, &scsi->target, def, _("no SCSI target ID supplied for '%s'"), _("invalid SCSI target ID supplied for '%s'")) < 0) - goto out; + return -1; if (virNodeDevCapsDefParseULong("number(./lun[1])", ctxt, &scsi->lun, def, _("no SCSI LUN ID supplied for '%s'"), _("invalid SCSI LUN ID supplied for '%s'")) < 0) - goto out; + return -1; scsi->type = virXPathString("string(./type[1])", ctxt); - ret = 0; - out: - return ret; + return 0; } @@ -1227,7 +1219,7 @@ virNodeDevCapSCSITargetParseXML(xmlXPathContextPtr ctxt, { VIR_XPATH_NODE_AUTORESTORE(ctxt) g_autofree xmlNodePtr *nodes = NULL; - int ret = -1, n = 0; + int n = 0; size_t i; ctxt->node = node; @@ -1237,11 +1229,11 @@ virNodeDevCapSCSITargetParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_INTERNAL_ERROR, _("no target name supplied for '%s'"), def->name); - goto out; + return -1; } if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0) - goto out; + return -1; for (i = 0; i < n; ++i) { g_autofree char *type = NULL; @@ -1251,7 +1243,7 @@ virNodeDevCapSCSITargetParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_INTERNAL_ERROR, _("missing type for SCSI target capability for '%s'"), def->name); - goto out; + return -1; } if (STREQ(type, "fc_remote_port")) { @@ -1264,7 +1256,7 @@ virNodeDevCapSCSITargetParseXML(xmlXPathContextPtr ctxt, &scsi_target->rport) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("missing rport name for '%s'"), def->name); - goto out; + return -1; } if (virNodeDevCapsDefParseString("string(./wwpn[1])", @@ -1272,20 +1264,17 @@ virNodeDevCapSCSITargetParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_INTERNAL_ERROR, _("missing wwpn identifier for '%s'"), def->name); - goto out; + return -1; } } else { virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown SCSI target capability type '%s' for '%s'"), type, def->name); - goto out; + return -1; } } - ret = 0; - - out: - return ret; + return 0; } @@ -1299,7 +1288,7 @@ virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt, { VIR_XPATH_NODE_AUTORESTORE(ctxt) g_autofree xmlNodePtr *nodes = NULL; - int ret = -1, n = 0; + int n = 0; size_t i; ctxt->node = node; @@ -1309,19 +1298,19 @@ virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt, &scsi_host->host, def, _("no SCSI host ID supplied for '%s'"), _("invalid SCSI host ID supplied for '%s'")) < 0) { - goto out; + return -1; } /* Optional unique_id value */ scsi_host->unique_id = -1; if (virNodeDevCapsDefParseIntOptional("number(./unique_id[1])", ctxt, &scsi_host->unique_id, def, _("invalid unique_id supplied for '%s'")) < 0) { - goto out; + return -1; } } if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0) - goto out; + return -1; for (i = 0; i < n; i++) { g_autofree char *type = NULL; @@ -1331,7 +1320,7 @@ virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_INTERNAL_ERROR, _("missing SCSI host capability type for '%s'"), def->name); - goto out; + return -1; } if (STREQ(type, "vport_ops")) { @@ -1351,7 +1340,7 @@ virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt, _("no WWNN supplied for '%s', and " "auto-generation failed"), def->name); - goto out; + return -1; } } @@ -1363,7 +1352,7 @@ virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt, _("no WWPN supplied for '%s', and " "auto-generation failed"), def->name); - goto out; + return -1; } } @@ -1376,14 +1365,11 @@ virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown SCSI host capability type '%s' for '%s'"), type, def->name); - goto out; + return -1; } } - ret = 0; - - out: - return ret; + return 0; } @@ -1396,7 +1382,7 @@ virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt, VIR_XPATH_NODE_AUTORESTORE(ctxt) xmlNodePtr lnk; size_t i = -1; - int ret = -1, n = -1; + int n = -1; g_autofree char *type = NULL; g_autofree xmlNodePtr *nodes = NULL; @@ -1407,13 +1393,13 @@ virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_INTERNAL_ERROR, _("no network interface supplied for '%s'"), def->name); - goto out; + return -1; } net->address = virXPathString("string(./address[1])", ctxt); if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0) - goto out; + return -1; if (n > 0) net->features = virBitmapNew(VIR_NET_DEV_FEAT_LAST); @@ -1424,14 +1410,14 @@ virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt, if (!(tmp = virXMLPropString(nodes[i], "name"))) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing network device feature name")); - goto out; + return -1; } if ((val = virNetDevFeatureTypeFromString(tmp)) < 0) { virReportError(VIR_ERR_XML_ERROR, _("unknown network device feature '%s'"), tmp); - goto out; + return -1; } ignore_value(virBitmapSetBit(net->features, val)); } @@ -1445,18 +1431,16 @@ virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("invalid network type supplied for '%s'"), def->name); - goto out; + return -1; } net->subtype = val; } lnk = virXPathNode("./link", ctxt); if (lnk && virInterfaceLinkParseXML(lnk, &net->lnk) < 0) - goto out; + return -1; - ret = 0; - out: - return ret; + return 0; } @@ -1467,7 +1451,6 @@ virNodeDevCapUSBInterfaceParseXML(xmlXPathContextPtr ctxt, virNodeDevCapUSBIfPtr usb_if) { VIR_XPATH_NODE_AUTORESTORE(ctxt) - int ret = -1; ctxt->node = node; @@ -1475,31 +1458,29 @@ virNodeDevCapUSBInterfaceParseXML(xmlXPathContextPtr ctxt, &usb_if->number, def, _("no USB interface number supplied for '%s'"), _("invalid USB interface number supplied for '%s'")) < 0) - goto out; + return -1; if (virNodeDevCapsDefParseULong("number(./class[1])", ctxt, &usb_if->klass, def, _("no USB interface class supplied for '%s'"), _("invalid USB interface class supplied for '%s'")) < 0) - goto out; + return -1; if (virNodeDevCapsDefParseULong("number(./subclass[1])", ctxt, &usb_if->subclass, def, _("no USB interface subclass supplied for '%s'"), _("invalid USB interface subclass supplied for '%s'")) < 0) - goto out; + return -1; if (virNodeDevCapsDefParseULong("number(./protocol[1])", ctxt, &usb_if->protocol, def, _("no USB interface protocol supplied for '%s'"), _("invalid USB interface protocol supplied for '%s'")) < 0) - goto out; + return -1; usb_if->description = virXPathString("string(./description[1])", ctxt); - ret = 0; - out: - return ret; + return 0; } @@ -1534,7 +1515,6 @@ virNodeDevCapUSBDevParseXML(xmlXPathContextPtr ctxt, virNodeDevCapUSBDevPtr usb_dev) { VIR_XPATH_NODE_AUTORESTORE(ctxt) - int ret = -1; ctxt->node = node; @@ -1542,32 +1522,30 @@ virNodeDevCapUSBDevParseXML(xmlXPathContextPtr ctxt, &usb_dev->bus, def, _("no USB bus number supplied for '%s'"), _("invalid USB bus number supplied for '%s'")) < 0) - goto out; + return -1; if (virNodeDevCapsDefParseULong("number(./device[1])", ctxt, &usb_dev->device, def, _("no USB device number supplied for '%s'"), _("invalid USB device number supplied for '%s'")) < 0) - goto out; + return -1; if (virNodeDevCapsDefParseHexId("string(./vendor[1]/@id)", ctxt, &usb_dev->vendor, def, _("no USB vendor ID supplied for '%s'"), _("invalid USB vendor ID supplied for '%s'")) < 0) - goto out; + return -1; if (virNodeDevCapsDefParseHexId("string(./product[1]/@id)", ctxt, &usb_dev->product, def, _("no USB product ID supplied for '%s'"), _("invalid USB product ID supplied for '%s'")) < 0) - goto out; + return -1; usb_dev->vendor_name = virXPathString("string(./vendor[1])", ctxt); usb_dev->product_name = virXPathString("string(./product[1])", ctxt); - ret = 0; - out: - return ret; + return 0; } @@ -1579,7 +1557,7 @@ virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt, VIR_XPATH_NODE_AUTORESTORE(ctxt) g_autofree xmlNodePtr *addrNodes = NULL; g_autofree char *numberStr = NULL; - int nAddrNodes, ret = -1; + int nAddrNodes; size_t i; ctxt->node = iommuGroupNode; @@ -1588,18 +1566,18 @@ virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt, if (!numberStr) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing iommuGroup number attribute")); - goto cleanup; + return -1; } if (virStrToLong_ui(numberStr, NULL, 10, &pci_dev->iommuGroupNumber) < 0) { virReportError(VIR_ERR_XML_ERROR, _("invalid iommuGroup number attribute '%s'"), numberStr); - goto cleanup; + return -1; } if ((nAddrNodes = virXPathNodeSet("./address", ctxt, &addrNodes)) < 0) - goto cleanup; + return -1; for (i = 0; i < nAddrNodes; i++) { g_autoptr(virPCIDeviceAddress) pciAddr = g_new0(virPCIDeviceAddress, 1); @@ -1608,12 +1586,10 @@ virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt, if (VIR_APPEND_ELEMENT(pci_dev->iommuGroupDevices, pci_dev->nIommuGroupDevices, pciAddr) < 0) - goto cleanup; + return -1; } - ret = 0; - cleanup: - return ret; + return 0; } @@ -1623,7 +1599,7 @@ virPCIEDeviceInfoLinkParseXML(xmlXPathContextPtr ctxt, virPCIELinkPtr lnk) { VIR_XPATH_NODE_AUTORESTORE(ctxt) - int ret = -1, speed; + int speed; g_autofree char *speedStr = NULL; g_autofree char *portStr = NULL; @@ -1632,7 +1608,7 @@ virPCIEDeviceInfoLinkParseXML(xmlXPathContextPtr ctxt, if (virXPathUInt("number(./@width)", ctxt, &lnk->width) < 0) { virReportError(VIR_ERR_XML_DETAIL, "%s", _("mandatory attribute 'width' is missing or malformed")); - goto cleanup; + return -1; } if ((speedStr = virXPathString("string(./@speed)", ctxt))) { @@ -1640,7 +1616,7 @@ virPCIEDeviceInfoLinkParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_XML_DETAIL, _("malformed 'speed' attribute: %s"), speedStr); - goto cleanup; + return -1; } lnk->speed = speed; } @@ -1650,15 +1626,13 @@ virPCIEDeviceInfoLinkParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_XML_DETAIL, _("malformed 'port' attribute: %s"), portStr); - goto cleanup; + return -1; } } else { lnk->port = -1; } - ret = 0; - cleanup: - return ret; + return 0; } @@ -1669,7 +1643,6 @@ virPCIEDeviceInfoParseXML(xmlXPathContextPtr ctxt, { VIR_XPATH_NODE_AUTORESTORE(ctxt) xmlNodePtr lnk; - int ret = -1; ctxt->node = pciExpressNode; @@ -1678,7 +1651,7 @@ virPCIEDeviceInfoParseXML(xmlXPathContextPtr ctxt, if (virPCIEDeviceInfoLinkParseXML(ctxt, lnk, pci_express->link_cap) < 0) - goto cleanup; + return -1; } if ((lnk = virXPathNode("./link[@validity='sta']", ctxt))) { @@ -1686,12 +1659,10 @@ virPCIEDeviceInfoParseXML(xmlXPathContextPtr ctxt, if (virPCIEDeviceInfoLinkParseXML(ctxt, lnk, pci_express->link_sta) < 0) - goto cleanup; + return -1; } - ret = 0; - cleanup: - return ret; + return 0; } @@ -1723,21 +1694,20 @@ static int virNodeDevPCICapSRIOVVirtualParseXML(xmlXPathContextPtr ctxt, virNodeDevCapPCIDevPtr pci_dev) { - int ret = -1; g_autofree xmlNodePtr *addresses = NULL; int naddresses = virXPathNodeSet("./address", ctxt, &addresses); g_autofree char *maxFuncsStr = virXPathString("string(./@maxCount)", ctxt); size_t i; if (naddresses < 0) - goto cleanup; + return -1; if (maxFuncsStr && virStrToLong_uip(maxFuncsStr, NULL, 10, &pci_dev->max_virtual_functions) < 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("Malformed 'maxCount' parameter")); - goto cleanup; + return -1; } pci_dev->virtual_functions = g_new0(virPCIDeviceAddressPtr, naddresses); @@ -1748,18 +1718,16 @@ virNodeDevPCICapSRIOVVirtualParseXML(xmlXPathContextPtr ctxt, addr = g_new0(virPCIDeviceAddress, 1); if (virPCIDeviceAddressParseXML(addresses[i], addr) < 0) - goto cleanup; + return -1; if (VIR_APPEND_ELEMENT(pci_dev->virtual_functions, pci_dev->num_virtual_functions, addr) < 0) - goto cleanup; + return -1; } pci_dev->flags |= VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION; - ret = 0; - cleanup: - return ret; + return 0; } @@ -1770,26 +1738,25 @@ virNodeDevPCICapabilityParseXML(xmlXPathContextPtr ctxt, { g_autofree char *type = virXMLPropString(node, "type"); VIR_XPATH_NODE_AUTORESTORE(ctxt) - int ret = -1; ctxt->node = node; if (!type) { virReportError(VIR_ERR_XML_ERROR, "%s", _("Missing capability type")); - goto cleanup; + return -1; } if (STREQ(type, "phys_function") && virNodeDevPCICapSRIOVPhysicalParseXML(ctxt, pci_dev) < 0) { - goto cleanup; + return -1; } else if (STREQ(type, "virt_functions") && virNodeDevPCICapSRIOVVirtualParseXML(ctxt, pci_dev) < 0) { - goto cleanup; + return -1; } else if (STREQ(type, "mdev_types")) { if (virNodeDevCapMdevTypesParseXML(ctxt, &pci_dev->mdev_types, &pci_dev->nmdev_types) < 0) - goto cleanup; + return -1; pci_dev->flags |= VIR_NODE_DEV_CAP_FLAG_PCI_MDEV; } else { int hdrType = virPCIHeaderTypeFromString(type); @@ -1798,9 +1765,7 @@ virNodeDevPCICapabilityParseXML(xmlXPathContextPtr ctxt, pci_dev->hdrType = hdrType; } - ret = 0; - cleanup: - return ret; + return 0; } @@ -1921,7 +1886,6 @@ virNodeDevCapSystemParseXML(xmlXPathContextPtr ctxt, virNodeDevCapSystemHardwarePtr hardware = &syscap->hardware; virNodeDevCapSystemFirmwarePtr firmware = &syscap->firmware; VIR_XPATH_NODE_AUTORESTORE(ctxt) - int ret = -1; g_autofree char *tmp = NULL; ctxt->node = node; @@ -1936,22 +1900,20 @@ virNodeDevCapSystemParseXML(xmlXPathContextPtr ctxt, if (!tmp) { virReportError(VIR_ERR_INTERNAL_ERROR, _("no system UUID supplied for '%s'"), def->name); - goto out; + return -1; } if (virUUIDParse(tmp, hardware->uuid) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("malformed uuid element for '%s'"), def->name); - goto out; + return -1; } firmware->vendor_name = virXPathString("string(./firmware/vendor[1])", ctxt); firmware->version = virXPathString("string(./firmware/version[1])", ctxt); firmware->release_date = virXPathString("string(./firmware/release_date[1])", ctxt); - ret = 0; - out: - return ret; + return 0; } static int @@ -1983,7 +1945,6 @@ virNodeDevCapMdevParseXML(xmlXPathContextPtr ctxt, virNodeDevCapMdevPtr mdev) { VIR_XPATH_NODE_AUTORESTORE(ctxt) - int ret = -1; int nattrs = 0; g_autofree xmlNodePtr *attrs = NULL; size_t i; @@ -1993,7 +1954,7 @@ virNodeDevCapMdevParseXML(xmlXPathContextPtr ctxt, if (!(mdev->type = virXPathString("string(./type[1]/@id)", ctxt))) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("missing type id attribute for '%s'"), def->name); - goto out; + return -1; } /* 'iommuGroup' is optional, only report an error if the supplied value is @@ -2003,18 +1964,16 @@ virNodeDevCapMdevParseXML(xmlXPathContextPtr ctxt, virReportError(VIR_ERR_INTERNAL_ERROR, _("invalid iommuGroup number attribute for '%s'"), def->name); - goto out; + return -1; } if ((nattrs = virXPathNodeSet("./attr", ctxt, &attrs)) < 0) - goto out; + return -1; for (i = 0; i < nattrs; i++) virNodeDevCapMdevAttributeParseXML(ctxt, attrs[i], mdev); - ret = 0; - out: - return ret; + return 0; } @@ -2529,7 +2488,6 @@ virNodeDeviceCapsListExport(virNodeDeviceDefPtr def, g_autofree virNodeDevCapType *tmp = NULL; bool want_list = !!list; int ncaps = 0; - int ret = -1; #define MAYBE_ADD_CAP(cap) \ do { \ @@ -2538,7 +2496,7 @@ virNodeDeviceCapsListExport(virNodeDeviceDefPtr def, } while (0) if (virNodeDeviceUpdateCaps(def) < 0) - goto cleanup; + return -1; if (want_list) tmp = g_new0(virNodeDevCapType, VIR_NODE_DEV_CAP_LAST - 1); @@ -2596,9 +2554,8 @@ virNodeDeviceCapsListExport(virNodeDeviceDefPtr def, if (want_list) *list = g_steal_pointer(&tmp); - ret = ncaps; - cleanup: - return ret; + + return ncaps; } -- 2.29.2

On 3/3/21 5:17 PM, Kristina Hanicova wrote:
This refactors file node_device_conf in order to remove VIR_FREE (if possible) and removes redundant variables and labels.
Kristina Hanicova (4): node_device_conf: Use g_autofree where possible node_device_conf: Refactoring functions in order to remove VIR_FREE node_device_conf: use g_autoptr in virNodeDevCapPCIDevIommuGroupParseXML() node_device_conf: Remove redundant variables/labels
src/conf/node_device_conf.c | 324 +++++++++++++----------------------- 1 file changed, 118 insertions(+), 206 deletions(-)
I'm fixing the small nit in 3/4 and pushing. Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Michal
participants (2)
-
Kristina Hanicova
-
Michal Privoznik