[PATCH 1/9] util: Fix memory leak in virAuthGetCredential

John Ferlan jferlan at redhat.com
Tue Jun 16 12:07:02 UTC 2020


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 at redhat.com>
---
 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 0aeab9db27..653c68472a 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 1c007757c7..8289b28d34 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.25.4




More information about the libvir-list mailing list