[libvirt] [PATCH v1 12/21] util: auth: use VIR_AUTOFREE instead of VIR_FREE for scalar types

Sukrit Bhatnagar skrtbhtngr at gmail.com
Thu Jun 7 19:34:34 UTC 2018


By making use of the GCC's __attribute__((cleanup)) handled by VIR_AUTOFREE
macro, majority of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.
---
 src/util/virauth.c | 45 +++++++++++++--------------------------------
 1 file changed, 13 insertions(+), 32 deletions(-)

diff --git a/src/util/virauth.c b/src/util/virauth.c
index c6a2ce7..d3a5cc7 100644
--- a/src/util/virauth.c
+++ b/src/util/virauth.c
@@ -41,10 +41,9 @@ int
 virAuthGetConfigFilePathURI(virURIPtr uri,
                             char **path)
 {
-    int ret = -1;
     size_t i;
     const char *authenv = virGetEnvBlockSUID("LIBVIRT_AUTH_FILE");
-    char *userdir = NULL;
+    VIR_AUTOFREE(char *) userdir = NULL;
 
     *path = NULL;
 
@@ -53,7 +52,7 @@ virAuthGetConfigFilePathURI(virURIPtr uri,
     if (authenv) {
         VIR_DEBUG("Using path from env '%s'", authenv);
         if (VIR_STRDUP(*path, authenv) < 0)
-            goto cleanup;
+            return -1;
         return 0;
     }
 
@@ -63,17 +62,17 @@ virAuthGetConfigFilePathURI(virURIPtr uri,
                 uri->params[i].value) {
                 VIR_DEBUG("Using path from URI '%s'", uri->params[i].value);
                 if (VIR_STRDUP(*path, uri->params[i].value) < 0)
-                    goto cleanup;
+                    return -1;
                 return 0;
             }
         }
     }
 
     if (!(userdir = virGetUserConfigDirectory()))
-        goto cleanup;
+        return -1;
 
     if (virAsprintf(path, "%s/auth.conf", userdir) < 0)
-        goto cleanup;
+        return -1;
 
     VIR_DEBUG("Checking for readability of '%s'", *path);
     if (access(*path, R_OK) == 0)
@@ -82,7 +81,7 @@ virAuthGetConfigFilePathURI(virURIPtr uri,
     VIR_FREE(*path);
 
     if (VIR_STRDUP(*path, SYSCONFDIR "/libvirt/auth.conf") < 0)
-        goto cleanup;
+        return -1;
 
     VIR_DEBUG("Checking for readability of '%s'", *path);
     if (access(*path, R_OK) == 0)
@@ -91,13 +90,9 @@ virAuthGetConfigFilePathURI(virURIPtr uri,
     VIR_FREE(*path);
 
  done:
-    ret = 0;
-
     VIR_DEBUG("Using auth file '%s'", NULLSTR(*path));
- cleanup:
-    VIR_FREE(userdir);
 
-    return ret;
+    return 0;
 }
 
 
@@ -155,7 +150,7 @@ virAuthGetUsernamePath(const char *path,
 {
     unsigned int ncred;
     virConnectCredential cred;
-    char *prompt;
+    VIR_AUTOFREE(char *) prompt = NULL;
     char *ret = NULL;
 
     if (virAuthGetCredential(servicename, hostname, "username", path, &ret) < 0)
@@ -192,8 +187,6 @@ virAuthGetUsernamePath(const char *path,
         break;
     }
 
-    VIR_FREE(prompt);
-
     return cred.result;
 }
 
@@ -205,18 +198,13 @@ virAuthGetUsername(virConnectPtr conn,
                    const char *defaultUsername,
                    const char *hostname)
 {
-    char *ret;
-    char *path;
+    VIR_AUTOFREE(char *) path = NULL;
 
     if (virAuthGetConfigFilePath(conn, &path) < 0)
         return NULL;
 
-    ret = virAuthGetUsernamePath(path, auth, servicename,
+    return virAuthGetUsernamePath(path, auth, servicename,
                                  defaultUsername, hostname);
-
-    VIR_FREE(path);
-
-    return ret;
 }
 
 
@@ -229,7 +217,7 @@ virAuthGetPasswordPath(const char *path,
 {
     unsigned int ncred;
     virConnectCredential cred;
-    char *prompt;
+    VIR_AUTOFREE(char *) prompt = NULL;
     char *ret = NULL;
 
     if (virAuthGetCredential(servicename, hostname, "password", path, &ret) < 0)
@@ -263,8 +251,6 @@ virAuthGetPasswordPath(const char *path,
         break;
     }
 
-    VIR_FREE(prompt);
-
     return cred.result;
 }
 
@@ -276,15 +262,10 @@ virAuthGetPassword(virConnectPtr conn,
                    const char *username,
                    const char *hostname)
 {
-    char *ret;
-    char *path;
+    VIR_AUTOFREE(char *) path = NULL;
 
     if (virAuthGetConfigFilePath(conn, &path) < 0)
         return NULL;
 
-    ret = virAuthGetPasswordPath(path, auth, servicename, username, hostname);
-
-    VIR_FREE(path);
-
-    return ret;
+    return virAuthGetPasswordPath(path, auth, servicename, username, hostname);
 }
-- 
1.8.3.1




More information about the libvir-list mailing list