]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
esx: switch esxUtil_ResolveHostname to return a new string
authorPino Toscano <ptoscano@redhat.com>
Mon, 5 Oct 2020 11:34:25 +0000 (13:34 +0200)
committerPino Toscano <ptoscano@redhat.com>
Mon, 5 Oct 2020 14:23:41 +0000 (16:23 +0200)
Change the interface of esxUtil_ResolveHostname() to return a newly
allocated string with the result address, instead of forcing the callers
to provide a buffer and its size. This way we can simply (auto)free the
string, and make the function stacks smaller.

Signed-off-by: Pino Toscano <ptoscano@redhat.com>
Reviewed-by: Laine Stump <laine@redhat.com>
src/esx/esx_driver.c
src/esx/esx_util.c
src/esx/esx_util.h

index e82e5ed8359d7d769e51da58943d1c90a591674d..a17bf58a512469fc858be57557b3cc0a3c33609c 100644 (file)
@@ -602,7 +602,7 @@ esxConnectToHost(esxPrivate *priv,
                  char **vCenterIPAddress)
 {
     int result = -1;
-    char ipAddress[NI_MAXHOST] = "";
+    g_autofree char *ipAddress = NULL;
     char *username = NULL;
     char *password = NULL;
     char *url = NULL;
@@ -615,7 +615,7 @@ esxConnectToHost(esxPrivate *priv,
 
     ESX_VI_CHECK_ARG_LIST(vCenterIPAddress);
 
-    if (esxUtil_ResolveHostname(conn->uri->server, ipAddress, NI_MAXHOST) < 0)
+    if (esxUtil_ResolveHostname(conn->uri->server, &ipAddress) < 0)
         return -1;
 
     if (conn->uri->user) {
@@ -692,7 +692,7 @@ esxConnectToVCenter(esxPrivate *priv,
                     const char *hostSystemIPAddress)
 {
     int result = -1;
-    char ipAddress[NI_MAXHOST] = "";
+    g_autofree char *ipAddress = NULL;
     char *username = NULL;
     char *password = NULL;
     char *url = NULL;
@@ -704,7 +704,7 @@ esxConnectToVCenter(esxPrivate *priv,
         return -1;
     }
 
-    if (esxUtil_ResolveHostname(hostname, ipAddress, NI_MAXHOST) < 0)
+    if (esxUtil_ResolveHostname(hostname, &ipAddress) < 0)
         return -1;
 
     if (conn->uri->user) {
@@ -813,7 +813,7 @@ esxConnectOpen(virConnectPtr conn, virConnectAuthPtr auth,
     virDrvOpenStatus result = VIR_DRV_OPEN_ERROR;
     esxPrivate *priv = NULL;
     char *potentialVCenterIPAddress = NULL;
-    char vCenterIPAddress[NI_MAXHOST] = "";
+    g_autofree char *vCenterIPAddress = NULL;
 
     virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
 
@@ -875,16 +875,10 @@ esxConnectOpen(virConnectPtr conn, virConnectAuthPtr auth,
                     goto cleanup;
                 }
 
-                if (virStrcpyStatic(vCenterIPAddress,
-                                    potentialVCenterIPAddress) < 0) {
-                    virReportError(VIR_ERR_INTERNAL_ERROR,
-                                   _("vCenter IP address %s too big for destination"),
-                                   potentialVCenterIPAddress);
-                    goto cleanup;
-                }
+                vCenterIPAddress = g_strdup(potentialVCenterIPAddress);
             } else {
                 if (esxUtil_ResolveHostname(priv->parsedUri->vCenter,
-                                            vCenterIPAddress, NI_MAXHOST) < 0) {
+                                            &vCenterIPAddress) < 0) {
                     goto cleanup;
                 }
 
index 555158f953d54161426ac8a806a1ac647ac13603..d9e7641d679859dd99fe1adb301ecffaf963ac8f 100644 (file)
@@ -278,12 +278,12 @@ esxUtil_ParseDatastorePath(const char *datastorePath, char **datastoreName,
 
 
 int
-esxUtil_ResolveHostname(const char *hostname,
-                        char *ipAddress, size_t ipAddress_length)
+esxUtil_ResolveHostname(const char *hostname, char **ipAddress)
 {
     struct addrinfo hints;
     struct addrinfo *result = NULL;
     int errcode;
+    g_autofree char *address = NULL;
 
     memset(&hints, 0, sizeof(hints));
 
@@ -308,8 +308,9 @@ esxUtil_ResolveHostname(const char *hostname,
         return -1;
     }
 
-    errcode = getnameinfo(result->ai_addr, result->ai_addrlen, ipAddress,
-                          ipAddress_length, NULL, 0, NI_NUMERICHOST);
+    address = g_new0(char, NI_MAXHOST);
+    errcode = getnameinfo(result->ai_addr, result->ai_addrlen, address,
+                          NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
     freeaddrinfo(result);
 
     if (errcode != 0) {
@@ -319,6 +320,8 @@ esxUtil_ResolveHostname(const char *hostname,
         return -1;
     }
 
+    *ipAddress = g_strdup(address);
+
     return 0;
 }
 
index 97b6d82a2be125f5714a5f16aef7c2b9fa7bb693..9bfbff1d42e14fb276661bd4f1001d57636cbe9a 100644 (file)
@@ -55,8 +55,7 @@ int esxUtil_ParseVirtualMachineIDString(const char *id_string, int *id);
 int esxUtil_ParseDatastorePath(const char *datastorePath, char **datastoreName,
                                char **directoryName, char **directoryAndFileName);
 
-int esxUtil_ResolveHostname(const char *hostname,
-                            char *ipAddress, size_t ipAddress_length);
+int esxUtil_ResolveHostname(const char *hostname, char **ipAddress);
 
 int esxUtil_ReformatUuid(const char *input, char *output);