From: Michal Privoznik Date: Mon, 17 May 2021 16:01:11 +0000 (+0200) Subject: viridentity: Fix ref/unref imbalance in VIR_IDENTITY_AUTORESTORE X-Git-Tag: v7.4.0-rc1~120 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=530715bd0b619b87c969571c0c5a10ebda514218;p=thirdparty%2Flibvirt.git 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 Reviewed-by: Daniel P. Berrangé --- diff --git a/src/util/viridentity.c b/src/util/viridentity.c index e7e5c31241..eb77f69e2e 100644 --- a/src/util/viridentity.c +++ b/src/util/viridentity.c @@ -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