Before adding new code to this function that will be made easier by using auto-cleaning pointers, update it to use auto-cleaning pointers (and the more modern virErrorPreserveLast()). Signed-off-by: Laine Stump <laine@xxxxxxxxxx> --- src/conf/domain_conf.c | 53 ++++++++++++++---------------------- src/conf/virnetworkportdef.h | 1 + 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 76aaa63f57..b6fa802523 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -30873,60 +30873,47 @@ virDomainNetCreatePort(virConnectPtr conn, virDomainNetDefPtr iface, unsigned int flags) { - virNetworkPtr net = NULL; - int ret = -1; - virNetworkPortDefPtr portdef = NULL; - virNetworkPortPtr port = NULL; - char *portxml = NULL; - virErrorPtr saved; + virErrorPtr save_err; + VIR_AUTOUNREF(virNetworkPtr) net = NULL; + VIR_AUTOPTR(virNetworkPortDef) portdef = NULL; + VIR_AUTOUNREF(virNetworkPortPtr) port = NULL; + VIR_AUTOFREE(char *) portxml = NULL; if (!(net = virNetworkLookupByName(conn, iface->data.network.name))) return -1; if (flags & VIR_NETWORK_PORT_CREATE_RECLAIM) { if (!(portdef = virDomainNetDefActualToNetworkPort(dom, iface))) - goto cleanup; + return -1; } else { if (!(portdef = virDomainNetDefToNetworkPort(dom, iface))) - goto cleanup; + return -1; } if (!(portxml = virNetworkPortDefFormat(portdef))) - goto cleanup; + return -1; + /* prepare to re-use portdef */ virNetworkPortDefFree(portdef); portdef = NULL; if (!(port = virNetworkPortCreateXML(net, portxml, flags))) - goto cleanup; + return -1; + /* prepare to re-use portxml */ VIR_FREE(portxml); - if (!(portxml = virNetworkPortGetXMLDesc(port, 0))) - goto deleteport; - - if (!(portdef = virNetworkPortDefParseString(portxml))) - goto deleteport; - - if (virDomainNetDefActualFromNetworkPort(iface, portdef) < 0) - goto deleteport; + if (!(portxml = virNetworkPortGetXMLDesc(port, 0)) || + !(portdef = virNetworkPortDefParseString(portxml)) || + virDomainNetDefActualFromNetworkPort(iface, portdef) < 0) { + virErrorPreserveLast(&save_err); + virNetworkPortDelete(port, 0); + virErrorRestore(&save_err); + return -1; + } virNetworkPortGetUUID(port, iface->data.network.portid); - - ret = 0; - cleanup: - virNetworkPortDefFree(portdef); - VIR_FREE(portxml); - virObjectUnref(port); - virObjectUnref(net); - return ret; - - deleteport: - saved = virSaveLastError(); - virNetworkPortDelete(port, 0); - virSetError(saved); - virFreeError(saved); - goto cleanup; + return 0; } int diff --git a/src/conf/virnetworkportdef.h b/src/conf/virnetworkportdef.h index 3d42b9b6a2..796e269fe0 100644 --- a/src/conf/virnetworkportdef.h +++ b/src/conf/virnetworkportdef.h @@ -82,6 +82,7 @@ struct _virNetworkPortDef { void virNetworkPortDefFree(virNetworkPortDefPtr port); +VIR_DEFINE_AUTOPTR_FUNC(virNetworkPortDef, virNetworkPortDefFree); virNetworkPortDefPtr virNetworkPortDefParseNode(xmlDocPtr xml, -- 2.21.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list