1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

bhyve: cleanup bhyveBuildNetArgStr error handling

Use 'goto cleanup'-style error handling instead of explicitly
freeing variables in every error path.
This commit is contained in:
Roman Bogorodskiy 2016-11-21 18:43:20 +03:00
parent 0df2524acb
commit 0b4c3bd307

View File

@ -52,26 +52,25 @@ bhyveBuildNetArgStr(const virDomainDef *def,
char macaddr[VIR_MAC_STRING_BUFLEN]; char macaddr[VIR_MAC_STRING_BUFLEN];
char *realifname = NULL; char *realifname = NULL;
char *brname = NULL; char *brname = NULL;
int ret = -1;
virDomainNetType actualType = virDomainNetGetActualType(net); virDomainNetType actualType = virDomainNetGetActualType(net);
if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE) { if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE) {
if (VIR_STRDUP(brname, virDomainNetGetActualBridgeName(net)) < 0) if (VIR_STRDUP(brname, virDomainNetGetActualBridgeName(net)) < 0)
return -1; goto cleanup;
} else { } else {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Network type %d is not supported"), _("Network type %d is not supported"),
virDomainNetGetActualType(net)); virDomainNetGetActualType(net));
return -1; goto cleanup;
} }
if (!net->ifname || if (!net->ifname ||
STRPREFIX(net->ifname, VIR_NET_GENERATED_PREFIX) || STRPREFIX(net->ifname, VIR_NET_GENERATED_PREFIX) ||
strchr(net->ifname, '%')) { strchr(net->ifname, '%')) {
VIR_FREE(net->ifname); VIR_FREE(net->ifname);
if (VIR_STRDUP(net->ifname, VIR_NET_GENERATED_PREFIX "%d") < 0) { if (VIR_STRDUP(net->ifname, VIR_NET_GENERATED_PREFIX "%d") < 0)
VIR_FREE(brname); goto cleanup;
return -1;
}
} }
if (!dryRun) { if (!dryRun) {
@ -80,33 +79,24 @@ bhyveBuildNetArgStr(const virDomainDef *def,
virDomainNetGetActualVirtPortProfile(net), virDomainNetGetActualVirtPortProfile(net),
virDomainNetGetActualVlan(net), virDomainNetGetActualVlan(net),
VIR_NETDEV_TAP_CREATE_IFUP | VIR_NETDEV_TAP_CREATE_PERSIST) < 0) { VIR_NETDEV_TAP_CREATE_IFUP | VIR_NETDEV_TAP_CREATE_PERSIST) < 0) {
VIR_FREE(net->ifname); goto cleanup;
VIR_FREE(brname);
return -1;
} }
realifname = virNetDevTapGetRealDeviceName(net->ifname); realifname = virNetDevTapGetRealDeviceName(net->ifname);
if (realifname == NULL) { if (realifname == NULL)
VIR_FREE(net->ifname); goto cleanup;
VIR_FREE(brname);
return -1;
}
VIR_DEBUG("%s -> %s", net->ifname, realifname); VIR_DEBUG("%s -> %s", net->ifname, realifname);
/* hack on top of other hack: we need to set /* hack on top of other hack: we need to set
* interface to 'UP' again after re-opening to find its * interface to 'UP' again after re-opening to find its
* name * name
*/ */
if (virNetDevSetOnline(net->ifname, true) != 0) { if (virNetDevSetOnline(net->ifname, true) != 0)
VIR_FREE(realifname); goto cleanup;
VIR_FREE(net->ifname);
VIR_FREE(brname);
return -1;
}
} else { } else {
if (VIR_STRDUP(realifname, "tap0") < 0) if (VIR_STRDUP(realifname, "tap0") < 0)
return -1; goto cleanup;
} }
@ -114,9 +104,15 @@ bhyveBuildNetArgStr(const virDomainDef *def,
virCommandAddArgFormat(cmd, "%d:0,virtio-net,%s,mac=%s", virCommandAddArgFormat(cmd, "%d:0,virtio-net,%s,mac=%s",
net->info.addr.pci.slot, net->info.addr.pci.slot,
realifname, virMacAddrFormat(&net->mac, macaddr)); realifname, virMacAddrFormat(&net->mac, macaddr));
ret = 0;
cleanup:
if (ret < 0)
VIR_FREE(net->ifname);
VIR_FREE(brname);
VIR_FREE(realifname); VIR_FREE(realifname);
return 0; return ret;
} }
static int static int