tests: Fix use-after-free in reference tests

Switch the check which tests whether the object has been finalised from
being a use-after-free, to using a weak pointer which is nullified on
finalisation.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2018-06-27 09:59:57 +01:00
parent ffb1c3cb74
commit e818089b70

View File

@ -156,11 +156,14 @@ test_set (void)
{
GObject *o = NULL;
GObject *tmp;
gpointer tmp_weak = NULL;
g_assert (!g_set_object (&o, NULL));
g_assert (o == NULL);
tmp = g_object_new (G_TYPE_OBJECT, NULL);
tmp_weak = tmp;
g_object_add_weak_pointer (tmp, &tmp_weak);
g_assert_cmpint (tmp->ref_count, ==, 1);
g_assert (g_set_object (&o, tmp));
@ -174,10 +177,11 @@ test_set (void)
g_assert (!g_set_object (&o, tmp));
g_assert (o == tmp);
g_assert_cmpint (tmp->ref_count, ==, 1);
g_assert_nonnull (tmp_weak);
g_assert (g_set_object (&o, NULL));
g_assert (o == NULL);
g_assert (!G_IS_OBJECT (tmp)); /* finalised */
g_assert_null (tmp_weak);
}
static void
@ -185,11 +189,14 @@ test_set_function (void)
{
GObject *o = NULL;
GObject *tmp;
gpointer tmp_weak = NULL;
g_assert (!(g_set_object) (&o, NULL));
g_assert (o == NULL);
tmp = g_object_new (G_TYPE_OBJECT, NULL);
tmp_weak = tmp;
g_object_add_weak_pointer (tmp, &tmp_weak);
g_assert_cmpint (tmp->ref_count, ==, 1);
g_assert ((g_set_object) (&o, tmp));
@ -203,10 +210,11 @@ test_set_function (void)
g_assert (!(g_set_object) (&o, tmp));
g_assert (o == tmp);
g_assert_cmpint (tmp->ref_count, ==, 1);
g_assert_nonnull (tmp_weak);
g_assert ((g_set_object) (&o, NULL));
g_assert (o == NULL);
g_assert (!G_IS_OBJECT (tmp)); /* finalised */
g_assert_null (tmp_weak);
}
static void