[glib: 1/3] gobject: Change assertions to read values via atomics



commit 4631cd892da2ad7ea54912b7f20af33ee2c72744
Author: Colin Walters <walters verbum org>
Date:   Thu Nov 17 15:43:26 2016 -0500

    gobject: Change assertions to read values via atomics
    
    I'm trying to use `-fsanitize=thread` for OSTree, and some of
    these issues seem to go into GLib.  Also, the sanitizers work better if
    the userspace libraries are built with them too.
    
    This fix is similar to
    https://github.com/ostreedev/ostree/pull/582/commits/b6814bb37cacd7a36715cf91766eb760b1b33c66
    
    Mixing atomic and non-atomic reads trips TSAN, so let's change the
    assertions to operate on the local values returned from atomic
    read/writes.
    
    Without this change I couldn't even *build* GLib with TSAN, since we
    use gresources during compilation, which uses GSubprocess, which hits
    this code.
    
    (Minor review fixes made by Philip Withnall <withnall endlessm com>.)
    
    https://gitlab.gnome.org/GNOME/glib/issues/1224

 gobject/gobject.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
---
diff --git a/gobject/gobject.c b/gobject/gobject.c
index 3f8ad9273..de61a0481 100644
--- a/gobject/gobject.c
+++ b/gobject/gobject.c
@@ -3210,9 +3210,9 @@ gpointer
   gint old_val;
 
   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
-  g_return_val_if_fail (object->ref_count > 0, NULL);
   
   old_val = g_atomic_int_add (&object->ref_count, 1);
+  g_return_val_if_fail (old_val > 0, NULL);
 
   if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
     toggle_refs_notify (object, FALSE);
@@ -3241,7 +3241,6 @@ g_object_unref (gpointer _object)
   gint old_ref;
   
   g_return_if_fail (G_IS_OBJECT (object));
-  g_return_if_fail (object->ref_count > 0);
   
   /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
  retry_atomic_decrement1:
@@ -3336,6 +3335,7 @@ g_object_unref (gpointer _object)
       
       /* decrement the last reference */
       old_ref = g_atomic_int_add (&object->ref_count, -1);
+      g_return_if_fail (old_ref > 0);
 
       TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
 


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