From 1d0a80317f2bb3bc5e44c54cc7d49e44ac80b66f Mon Sep 17 00:00:00 2001
From: John Ferlan <jferlan@redhat.com>
Date: Tue, 16 Jun 2020 08:07:02 -0400
Subject: [PATCH 005/108] util: Fix memory leak in virAuthGetCredential

Since 5084091a, @tmp is filled by a g_key_file_get_string which is
now an allocated string as opposed to some hash table lookup value,
so we need to treat it as so.

Found by Coverity

Signed-off-by: John Ferlan <jferlan@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
(cherry picked from commit d585847d2e9c98ccb765027999824568c355ddc6)
---
 src/remote/remote_driver.c | 2 +-
 src/util/virauth.c         | 5 +----
 src/util/virauthconfig.c   | 2 +-
 src/util/virauthconfig.h   | 2 +-
 tests/virauthconfigtest.c  | 2 +-
 5 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 1202d44017..7591263f6d 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -4097,7 +4097,7 @@ static int remoteAuthFillFromConfig(virConnectPtr conn,
     }
 
     for (ninteract = 0; state->interact[ninteract].id != 0; ninteract++) {
-        const char *value = NULL;
+        char *value = NULL;
 
         switch (state->interact[ninteract].id) {
         case SASL_CB_USER:
diff --git a/src/util/virauth.c b/src/util/virauth.c
index f75e674586..105fca16eb 100644
--- a/src/util/virauth.c
+++ b/src/util/virauth.c
@@ -107,7 +107,6 @@ virAuthGetCredential(const char *servicename,
                      char **value)
 {
     g_autoptr(virAuthConfig) config = NULL;
-    const char *tmp;
 
     *value = NULL;
 
@@ -121,11 +120,9 @@ virAuthGetCredential(const char *servicename,
                             servicename,
                             hostname,
                             credname,
-                            &tmp) < 0)
+                            value) < 0)
         return -1;
 
-    *value = g_strdup(tmp);
-
     return 0;
 }
 
diff --git a/src/util/virauthconfig.c b/src/util/virauthconfig.c
index 0da9c2461f..2e50609531 100644
--- a/src/util/virauthconfig.c
+++ b/src/util/virauthconfig.c
@@ -99,7 +99,7 @@ int virAuthConfigLookup(virAuthConfigPtr auth,
                         const char *service,
                         const char *hostname,
                         const char *credname,
-                        const char **value)
+                        char **value)
 {
     g_autofree char *authgroup = NULL;
     g_autofree char *credgroup = NULL;
diff --git a/src/util/virauthconfig.h b/src/util/virauthconfig.h
index de28b1ff28..b6f5b5c110 100644
--- a/src/util/virauthconfig.h
+++ b/src/util/virauthconfig.h
@@ -37,6 +37,6 @@ int virAuthConfigLookup(virAuthConfigPtr auth,
                         const char *service,
                         const char *hostname,
                         const char *credname,
-                        const char **value);
+                        char **value);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virAuthConfig, virAuthConfigFree);
diff --git a/tests/virauthconfigtest.c b/tests/virauthconfigtest.c
index 20855f004e..a88b453543 100644
--- a/tests/virauthconfigtest.c
+++ b/tests/virauthconfigtest.c
@@ -42,7 +42,7 @@ struct ConfigLookupData {
 static int testAuthLookup(const void *args)
 {
     const struct ConfigLookupData *data = args;
-    const char *actual = NULL;
+    g_autofree char *actual = NULL;
     int rv;
 
     rv = virAuthConfigLookup(data->config,
-- 
2.33.0

