]> xenbits.xensource.com Git - libvirt.git/commitdiff
viridentity: Fix ref/unref imbalance in VIR_IDENTITY_AUTORESTORE
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 17 May 2021 16:01:11 +0000 (18:01 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 17 May 2021 19:06:15 +0000 (21:06 +0200)
The basic use case of VIR_IDENTITY_AUTORESTORE() is in
conjunction with virIdentityElevateCurrent(). What happens is
that virIdentityElevateCurrent() gets current identity (which
increases the refcounter of thread local virIdentity object) and
returns a pointer to it. Later, when the variable goes out of
scope the virIdentityRestoreHelper() is called which calls
virIdentitySetCurrent() over the old identity. But this means
that the refcounter is increased again.

Therefore, we have to explicitly decrease the refcounter by
calling g_object_unref().

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
src/util/viridentity.c

index e7e5c31241a78123c06ef0132bac11621b147ff2..eb77f69e2ee3b218b57edb9f49963758b6f2c4ac 100644 (file)
@@ -197,8 +197,12 @@ void virIdentityRestoreHelper(virIdentity **identptr)
 {
     virIdentity *ident = *identptr;
 
-    if (ident != NULL)
+    if (ident != NULL) {
         virIdentitySetCurrent(ident);
+        /* virIdentitySetCurrent() grabs its own reference.
+         * We don't need ours anymore. */
+        g_object_unref(ident);
+    }
 }
 
 #define TOKEN_BYTES 16