[HarfBuzz] harfbuzz: Branch 'master'

Behdad Esfahbod behdad at kemper.freedesktop.org
Fri Oct 27 22:37:41 UTC 2017


 src/hb-ft.cc             |    3 +++
 src/hb-glib.cc           |    3 +++
 src/hb-icu.cc            |    3 +++
 src/hb-object-private.hh |   37 +++++++++++++++++++++++++++++++------
 src/hb-ot-font.cc        |    3 +++
 src/hb-ucdn.cc           |    3 +++
 6 files changed, 46 insertions(+), 6 deletions(-)

New commits:
commit 5daf3bd4494cce6b4fb074533be9e99c4d9e0edd
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Fri Oct 27 16:34:01 2017 -0600

    [glib/ucdn/icu/ft/ot] Make returned funcs inert
    
    Such that client cannot accidentally destroy them, even though that
    will be a bug in their code...

diff --git a/src/hb-ft.cc b/src/hb-ft.cc
index f578e9dc..13f32142 100644
--- a/src/hb-ft.cc
+++ b/src/hb-ft.cc
@@ -428,6 +428,7 @@ static hb_font_funcs_t *static_ft_funcs = nullptr;
 static
 void free_static_ft_funcs (void)
 {
+  hb_object_undo_inert (static_ft_funcs);
   hb_font_funcs_destroy (static_ft_funcs);
 }
 #endif
@@ -458,8 +459,10 @@ retry:
     hb_font_funcs_set_glyph_from_name_func (funcs, hb_ft_get_glyph_from_name, nullptr, nullptr);
 
     hb_font_funcs_make_immutable (funcs);
+    hb_object_make_inert (funcs);
 
     if (!hb_atomic_ptr_cmpexch (&static_ft_funcs, nullptr, funcs)) {
+      hb_object_undo_inert (funcs);
       hb_font_funcs_destroy (funcs);
       goto retry;
     }
diff --git a/src/hb-glib.cc b/src/hb-glib.cc
index 50c30e9c..31de68e8 100644
--- a/src/hb-glib.cc
+++ b/src/hb-glib.cc
@@ -370,6 +370,7 @@ static hb_unicode_funcs_t *static_glib_funcs = nullptr;
 static
 void free_static_glib_funcs (void)
 {
+  hb_object_undo_inert (static_glib_funcs);
   hb_unicode_funcs_destroy (static_glib_funcs);
 }
 #endif
@@ -390,8 +391,10 @@ retry:
 #undef HB_UNICODE_FUNC_IMPLEMENT
 
     hb_unicode_funcs_make_immutable (funcs);
+    hb_object_make_inert (funcs);
 
     if (!hb_atomic_ptr_cmpexch (&static_glib_funcs, nullptr, funcs)) {
+      hb_object_undo_inert (funcs);
       hb_unicode_funcs_destroy (funcs);
       goto retry;
     }
diff --git a/src/hb-icu.cc b/src/hb-icu.cc
index 552eaeca..44a19893 100644
--- a/src/hb-icu.cc
+++ b/src/hb-icu.cc
@@ -351,6 +351,7 @@ static hb_unicode_funcs_t *static_icu_funcs = nullptr;
 static
 void free_static_icu_funcs (void)
 {
+  hb_object_undo_inert (static_icu_funcs);
   hb_unicode_funcs_destroy (static_icu_funcs);
 }
 #endif
@@ -379,8 +380,10 @@ retry:
 #undef HB_UNICODE_FUNC_IMPLEMENT
 
     hb_unicode_funcs_make_immutable (funcs);
+    hb_object_make_inert (funcs);
 
     if (!hb_atomic_ptr_cmpexch (&static_icu_funcs, nullptr, funcs)) {
+      hb_object_undo_inert (funcs);
       hb_unicode_funcs_destroy (funcs);
       goto retry;
     }
diff --git a/src/hb-object-private.hh b/src/hb-object-private.hh
index d678ae36..99fe646b 100644
--- a/src/hb-object-private.hh
+++ b/src/hb-object-private.hh
@@ -47,9 +47,10 @@
 
 /* reference_count */
 
-#define HB_REFERENCE_COUNT_INERT_VALUE -1
-#define HB_REFERENCE_COUNT_POISON_VALUE -0x0000DEAD
-#define HB_REFERENCE_COUNT_INIT {HB_ATOMIC_INT_INIT(HB_REFERENCE_COUNT_INERT_VALUE)}
+#define HB_REFERENCE_COUNT_UNCHANGABLE_VALUE	-0x53043
+#define HB_REFERENCE_COUNT_UNREFFABLE_VALUE	-0x07734
+#define HB_REFERENCE_COUNT_UNTOUCHABLE_VALUE	-0xBEDAD
+#define HB_REFERENCE_COUNT_INIT {HB_ATOMIC_INT_INIT (HB_REFERENCE_COUNT_UNCHANGABLE_VALUE)}
 
 struct hb_reference_count_t
 {
@@ -59,9 +60,23 @@ struct hb_reference_count_t
   inline int get_unsafe (void) const { return ref_count.get_unsafe (); }
   inline int inc (void) { return ref_count.inc (); }
   inline int dec (void) { return ref_count.dec (); }
-  inline void finish (void) { ref_count.set_unsafe (HB_REFERENCE_COUNT_POISON_VALUE); }
-
-  inline bool is_inert (void) const { return ref_count.get_unsafe () == HB_REFERENCE_COUNT_INERT_VALUE; }
+  inline void finish (void) { ref_count.set_unsafe (HB_REFERENCE_COUNT_UNTOUCHABLE_VALUE); }
+
+  inline void make_inert (void)
+  {
+    if (get_unsafe () == HB_REFERENCE_COUNT_UNCHANGABLE_VALUE)
+      return;
+    ref_count.set_unsafe (HB_REFERENCE_COUNT_UNREFFABLE_VALUE);
+  }
+  inline void undo_inert (void)
+  {
+    if (get_unsafe () == HB_REFERENCE_COUNT_UNCHANGABLE_VALUE)
+      return;
+    assert (get_unsafe () == HB_REFERENCE_COUNT_UNREFFABLE_VALUE);
+    ref_count.set_unsafe (1);
+  }
+
+  inline bool is_inert (void) const { return ref_count.get_unsafe () < 0; }
   inline bool is_valid (void) const { return ref_count.get_unsafe () > 0; }
 };
 
@@ -152,6 +167,16 @@ static inline bool hb_object_is_valid (const Type *obj)
   return likely (obj->header.ref_count.is_valid ());
 }
 template <typename Type>
+static inline void hb_object_make_inert (Type *obj)
+{
+  obj->header.ref_count.make_inert ();
+}
+template <typename Type>
+static inline void hb_object_undo_inert (Type *obj)
+{
+  obj->header.ref_count.undo_inert ();
+}
+template <typename Type>
 static inline Type *hb_object_reference (Type *obj)
 {
   hb_object_trace (obj, HB_FUNC);
diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc
index c0ce89df..10c1cb2c 100644
--- a/src/hb-ot-font.cc
+++ b/src/hb-ot-font.cc
@@ -628,6 +628,7 @@ static hb_font_funcs_t *static_ot_funcs = nullptr;
 static
 void free_static_ot_funcs (void)
 {
+  hb_object_undo_inert (static_ot_funcs);
   hb_font_funcs_destroy (static_ot_funcs);
 }
 #endif
@@ -658,8 +659,10 @@ retry:
     hb_font_funcs_set_glyph_from_name_func (funcs, hb_ot_get_glyph_from_name, nullptr, nullptr);
 
     hb_font_funcs_make_immutable (funcs);
+    hb_object_make_inert (funcs);
 
     if (!hb_atomic_ptr_cmpexch (&static_ot_funcs, nullptr, funcs)) {
+      hb_object_undo_inert (funcs);
       hb_font_funcs_destroy (funcs);
       goto retry;
     }
diff --git a/src/hb-ucdn.cc b/src/hb-ucdn.cc
index 9515bda2..99266724 100644
--- a/src/hb-ucdn.cc
+++ b/src/hb-ucdn.cc
@@ -237,6 +237,7 @@ static hb_unicode_funcs_t *static_ucdn_funcs = nullptr;
 static
 void free_static_ucdn_funcs (void)
 {
+  hb_object_undo_inert (static_ucdn_funcs);
   hb_unicode_funcs_destroy (static_ucdn_funcs);
 }
 #endif
@@ -258,8 +259,10 @@ retry:
 #undef HB_UNICODE_FUNC_IMPLEMENT
 
     hb_unicode_funcs_make_immutable (funcs);
+    hb_object_make_inert (funcs);
 
     if (!hb_atomic_ptr_cmpexch (&static_ucdn_funcs, nullptr, funcs)) {
+      hb_object_undo_inert (funcs);
       hb_unicode_funcs_destroy (funcs);
       goto retry;
     }


More information about the HarfBuzz mailing list