[glib: 9/11] tests: Fix use-after-free in reference tests



commit e818089b70245c276295a7684100605f93899461
Author: Philip Withnall <withnall endlessm com>
Date:   Wed Jun 27 09:59:57 2018 +0100

    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>

 gobject/tests/reference.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
---
diff --git a/gobject/tests/reference.c b/gobject/tests/reference.c
index e3f86315c..b05150e96 100644
--- a/gobject/tests/reference.c
+++ b/gobject/tests/reference.c
@@ -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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]