viridentity: Fix ref/unref imbalance in VIR_IDENTITY_AUTORESTORE

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>
This commit is contained in:
Michal Privoznik 2021-05-17 18:01:11 +02:00
parent b24a45827a
commit 530715bd0b

View 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