gibaseinfo: Fix a double-unref with stack-allocated GIBaseInfos

As documented in the commit, the internal members of `GIBaseInfo` are
not reffed if the `GIBaseInfo` is stack-allocated, as the caller can be
relied on to ensure their lifetime exceeds that of the `GIBaseInfo`.

Make sure that’s actually reflected in the code.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3218
This commit is contained in:
Philip Withnall 2024-01-23 17:55:26 +00:00
parent a3da3a032c
commit c8c1febc7d
2 changed files with 9 additions and 5 deletions

View File

@ -123,12 +123,12 @@ value_base_info_lcopy_value (const GValue *value,
static void
gi_base_info_finalize (GIBaseInfo *self)
{
if (self->container && self->container->ref_count != INVALID_REFCOUNT)
if (self->ref_count != INVALID_REFCOUNT &&
self->container && self->container->ref_count != INVALID_REFCOUNT)
gi_base_info_unref (self->container);
g_clear_object (&self->repository);
g_type_free_instance ((GTypeInstance *) self);
if (self->ref_count != INVALID_REFCOUNT)
g_clear_object (&self->repository);
}
static void
@ -571,7 +571,10 @@ gi_base_info_unref (void *info)
g_assert (rinfo->ref_count > 0 && rinfo->ref_count != INVALID_REFCOUNT);
if (g_atomic_ref_count_dec (&rinfo->ref_count))
GI_BASE_INFO_GET_CLASS (info)->finalize (info);
{
GI_BASE_INFO_GET_CLASS (info)->finalize (info);
g_type_free_instance ((GTypeInstance *) info);
}
}
/**

View File

@ -48,6 +48,7 @@ struct _GIBaseInfo
GTypeInstance parent_instance;
gatomicrefcount ref_count;
/* these are both reffed if the GIBaseInfo is heap-allocated, but not reffed if its stack-allocated */
GIRepository *repository;
GIBaseInfo *container;