From 530715bd0b619b87c969571c0c5a10ebda514218 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Mon, 17 May 2021 18:01:11 +0200 Subject: [PATCH] viridentity: Fix ref/unref imbalance in VIR_IDENTITY_AUTORESTORE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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é --- src/util/viridentity.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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