Define a new cleanup function for virAuthConfigPtr in src/util/virauthconfig.h. Modifiy code to use cleanup macros where required. Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@xxxxxxxxx> --- src/util/virauth.c | 66 ++++++++++++++++++------------------------------ src/util/virauthconfig.h | 3 +++ 2 files changed, 27 insertions(+), 42 deletions(-) diff --git a/src/util/virauth.c b/src/util/virauth.c index adb093e..aa7593c 100644 --- a/src/util/virauth.c +++ b/src/util/virauth.c @@ -26,7 +26,6 @@ #include "virauth.h" #include "virutil.h" -#include "viralloc.h" #include "virlog.h" #include "datatypes.h" #include "virerror.h" @@ -42,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; @@ -54,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; } @@ -64,41 +62,38 @@ 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) - goto done; + if (access(*path, R_OK) == 0) { + VIR_DEBUG("Using auth file '%s'", NULLSTR(*path)); + return 0; + } 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) - goto done; + if (access(*path, R_OK) == 0) { + VIR_DEBUG("Using auth file '%s'", NULLSTR(*path)); + return 0; + } VIR_FREE(*path); - done: - ret = 0; - - VIR_DEBUG("Using auth file '%s'", NULLSTR(*path)); - cleanup: - VIR_FREE(userdir); - - return ret; + return 0; } @@ -117,8 +112,7 @@ virAuthGetCredential(const char *servicename, const char *path, char **value) { - int ret = -1; - virAuthConfigPtr config = NULL; + VIR_AUTOPTR(virAuthConfigPtr) config = NULL; const char *tmp; *value = NULL; @@ -127,23 +121,19 @@ virAuthGetCredential(const char *servicename, return 0; if (!(config = virAuthConfigNew(path))) - goto cleanup; + return -1; if (virAuthConfigLookup(config, servicename, hostname, credname, &tmp) < 0) - goto cleanup; + return -1; if (VIR_STRDUP(*value, tmp) < 0) - goto cleanup; + return -1; - ret = 0; - - cleanup: - virAuthConfigFree(config); - return ret; + return 0; } @@ -156,7 +146,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) @@ -193,8 +183,6 @@ virAuthGetUsernamePath(const char *path, break; } - VIR_FREE(prompt); - return cred.result; } @@ -207,7 +195,7 @@ virAuthGetUsername(virConnectPtr conn, const char *hostname) { char *ret; - char *path; + VIR_AUTOFREE(char *) path = NULL; if (virAuthGetConfigFilePath(conn, &path) < 0) return NULL; @@ -215,8 +203,6 @@ virAuthGetUsername(virConnectPtr conn, ret = virAuthGetUsernamePath(path, auth, servicename, defaultUsername, hostname); - VIR_FREE(path); - return ret; } @@ -230,7 +216,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) @@ -264,8 +250,6 @@ virAuthGetPasswordPath(const char *path, break; } - VIR_FREE(prompt); - return cred.result; } @@ -278,14 +262,12 @@ virAuthGetPassword(virConnectPtr conn, 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; } diff --git a/src/util/virauthconfig.h b/src/util/virauthconfig.h index ac0ceeb..375ccad 100644 --- a/src/util/virauthconfig.h +++ b/src/util/virauthconfig.h @@ -24,6 +24,7 @@ # define __VIR_AUTHCONFIG_H__ # include "internal.h" +# include "viralloc.h" typedef struct _virAuthConfig virAuthConfig; typedef virAuthConfig *virAuthConfigPtr; @@ -42,4 +43,6 @@ int virAuthConfigLookup(virAuthConfigPtr auth, const char *credname, const char **value); +VIR_DEFINE_AUTOPTR_FUNC(virAuthConfigPtr, virAuthConfigFree) + #endif /* __VIR_AUTHCONFIG_H__ */ -- 1.8.3.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list