vbox: refactor vboxNetworkGetXMLDesc a bit

Error out on allocation failure to reduce the nesting.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Ján Tomko 2020-10-02 20:02:48 +02:00
parent 5ee7714b57
commit 281ccec246
1 changed files with 49 additions and 53 deletions

View File

@ -821,69 +821,65 @@ static char *vboxNetworkGetXMLDesc(virNetworkPtr network, unsigned int flags)
networkNameUtf16, networkNameUtf16,
&dhcpServer); &dhcpServer);
if (dhcpServer) { if (dhcpServer) {
PRUnichar *ipAddressUtf16 = NULL;
PRUnichar *networkMaskUtf16 = NULL;
PRUnichar *fromIPAddressUtf16 = NULL;
PRUnichar *toIPAddressUtf16 = NULL;
PRUnichar *macAddressUtf16 = NULL;
bool errorOccurred = false;
ipdef->nranges = 1; ipdef->nranges = 1;
if (VIR_ALLOC_N(ipdef->ranges, ipdef->nranges) >= 0) {
PRUnichar *ipAddressUtf16 = NULL;
PRUnichar *networkMaskUtf16 = NULL;
PRUnichar *fromIPAddressUtf16 = NULL;
PRUnichar *toIPAddressUtf16 = NULL;
bool errorOccurred = false;
gVBoxAPI.UIDHCPServer.GetIPAddress(dhcpServer, &ipAddressUtf16); if (VIR_ALLOC_N(ipdef->ranges, ipdef->nranges) < 0)
gVBoxAPI.UIDHCPServer.GetNetworkMask(dhcpServer, &networkMaskUtf16); goto cleanup;
gVBoxAPI.UIDHCPServer.GetLowerIP(dhcpServer, &fromIPAddressUtf16);
gVBoxAPI.UIDHCPServer.GetUpperIP(dhcpServer, &toIPAddressUtf16);
/* Currently virtualbox supports only one dhcp server per network
* with contiguous address space from start to end
*/
addr = ipdef->ranges[0].addr;
if (vboxSocketParseAddrUtf16(data, ipAddressUtf16,
&ipdef->address) < 0 ||
vboxSocketParseAddrUtf16(data, networkMaskUtf16,
&ipdef->netmask) < 0 ||
vboxSocketParseAddrUtf16(data, fromIPAddressUtf16,
&addr.start) < 0 ||
vboxSocketParseAddrUtf16(data, toIPAddressUtf16,
&addr.end) < 0) {
errorOccurred = true;
}
VBOX_UTF16_FREE(ipAddressUtf16); gVBoxAPI.UIDHCPServer.GetIPAddress(dhcpServer, &ipAddressUtf16);
VBOX_UTF16_FREE(networkMaskUtf16); gVBoxAPI.UIDHCPServer.GetNetworkMask(dhcpServer, &networkMaskUtf16);
VBOX_UTF16_FREE(fromIPAddressUtf16); gVBoxAPI.UIDHCPServer.GetLowerIP(dhcpServer, &fromIPAddressUtf16);
VBOX_UTF16_FREE(toIPAddressUtf16); gVBoxAPI.UIDHCPServer.GetUpperIP(dhcpServer, &toIPAddressUtf16);
/* Currently virtualbox supports only one dhcp server per network
if (errorOccurred) * with contiguous address space from start to end
goto cleanup; */
} else { addr = ipdef->ranges[0].addr;
ipdef->nranges = 0; if (vboxSocketParseAddrUtf16(data, ipAddressUtf16,
&ipdef->address) < 0 ||
vboxSocketParseAddrUtf16(data, networkMaskUtf16,
&ipdef->netmask) < 0 ||
vboxSocketParseAddrUtf16(data, fromIPAddressUtf16,
&addr.start) < 0 ||
vboxSocketParseAddrUtf16(data, toIPAddressUtf16,
&addr.end) < 0) {
errorOccurred = true;
} }
VBOX_UTF16_FREE(ipAddressUtf16);
VBOX_UTF16_FREE(networkMaskUtf16);
VBOX_UTF16_FREE(fromIPAddressUtf16);
VBOX_UTF16_FREE(toIPAddressUtf16);
if (errorOccurred)
goto cleanup;
ipdef->nhosts = 1; ipdef->nhosts = 1;
if (VIR_ALLOC_N(ipdef->hosts, ipdef->nhosts) >= 0) { if (VIR_ALLOC_N(ipdef->hosts, ipdef->nhosts) < 0)
PRUnichar *macAddressUtf16 = NULL; goto cleanup;
PRUnichar *ipAddressUtf16 = NULL;
bool errorOccurred = false;
ipdef->hosts[0].name = g_strdup(network->name); ipdef->hosts[0].name = g_strdup(network->name);
gVBoxAPI.UIHNInterface.GetHardwareAddress(networkInterface, &macAddressUtf16); gVBoxAPI.UIHNInterface.GetHardwareAddress(networkInterface, &macAddressUtf16);
gVBoxAPI.UIHNInterface.GetIPAddress(networkInterface, &ipAddressUtf16); gVBoxAPI.UIHNInterface.GetIPAddress(networkInterface, &ipAddressUtf16);
VBOX_UTF16_TO_UTF8(macAddressUtf16, &ipdef->hosts[0].mac); VBOX_UTF16_TO_UTF8(macAddressUtf16, &ipdef->hosts[0].mac);
if (vboxSocketParseAddrUtf16(data, ipAddressUtf16, if (vboxSocketParseAddrUtf16(data, ipAddressUtf16,
&ipdef->hosts[0].ip) < 0) { &ipdef->hosts[0].ip) < 0) {
errorOccurred = true; errorOccurred = true;
}
VBOX_UTF16_FREE(macAddressUtf16);
VBOX_UTF16_FREE(ipAddressUtf16);
if (errorOccurred)
goto cleanup;
} else {
ipdef->nhosts = 0;
} }
VBOX_UTF16_FREE(macAddressUtf16);
VBOX_UTF16_FREE(ipAddressUtf16);
if (errorOccurred)
goto cleanup;
} else { } else {
PRUnichar *networkMaskUtf16 = NULL; PRUnichar *networkMaskUtf16 = NULL;
PRUnichar *ipAddressUtf16 = NULL; PRUnichar *ipAddressUtf16 = NULL;