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@gmail.com>
---
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@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
On Wed, May 30, 2018 at 02:30:07AM +0530, Sukrit Bhatnagar wrote:
> Define a new cleanup function for virAuthConfigPtr in
> src/util/virauthconfig.h.
> Modifiy code to use cleanup macros where required.
s/fiy/fy
>
> Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
> ---
> 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;
As I said in patch 1, changes related to VIR_AUTOPTR should be a separate patch
series, IOW the patches shouldn't combine changing both VIR_AUTOFREE and
VIR_AUTOPTR.
Otherwise, the changes look good, looking forward to more files. As we
discussed privately, having a series addressing src/util then e.g. src/conf as
separate patch series which can be merged gradually is IMHO better than trying
to make all the VIR_FREE changes everywhere, then sending a massive series
consisting of tens of patches and then doing the same for VIR_AUTOPTR.
Erik
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
© 2016 - 2025 Red Hat, Inc.