From a5a27073cfff91c4f80209ca8462543130af61dd Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 14 Aug 2014 13:05:36 -0400 Subject: [PATCH] Rewrite this==NULL checks to avoid undefined behavior Fixes https://code.google.com/p/chromium/issues/detail?id=403594 --- src/hb-object-private.hh | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/hb-object-private.hh b/src/hb-object-private.hh index 8e3c0062e..1befd6bad 100644 --- a/src/hb-object-private.hh +++ b/src/hb-object-private.hh @@ -129,15 +129,11 @@ struct hb_object_header_t template friend Type *hb_object_reference (Type *obj); inline void reference (void) { - if (unlikely (!this || this->is_inert ())) - return; ref_count.inc (); } template friend bool hb_object_destroy (Type *obj); inline bool destroy (void) { - if (unlikely (!this || this->is_inert ())) - return false; if (ref_count.dec () != 1) return false; @@ -156,18 +152,12 @@ struct hb_object_header_t void * data, hb_destroy_func_t destroy_func, hb_bool_t replace) { - if (unlikely (!this || this->is_inert ())) - return false; - return user_data.set (key, data, destroy_func, replace); } template friend void *hb_object_get_user_data (type *obj, hb_user_data_key_t *key); inline void *get_user_data (hb_user_data_key_t *key) { - if (unlikely (!this || this->is_inert ())) - return NULL; - return user_data.get (key); } @@ -212,6 +202,8 @@ static inline bool hb_object_is_inert (const Type *obj) template static inline Type *hb_object_reference (Type *obj) { + if (unlikely (!obj || obj->header.is_inert ())) + return obj; hb_object_trace (obj, HB_FUNC); obj->header.reference (); return obj; @@ -219,6 +211,8 @@ static inline Type *hb_object_reference (Type *obj) template static inline bool hb_object_destroy (Type *obj) { + if (unlikely (!obj || obj->header.is_inert ())) + return false; hb_object_trace (obj, HB_FUNC); return obj->header.destroy (); } @@ -229,6 +223,8 @@ static inline bool hb_object_set_user_data (Type *obj, hb_destroy_func_t destroy, hb_bool_t replace) { + if (unlikely (!obj || obj->header.is_inert ())) + return false; return obj->header.set_user_data (key, data, destroy, replace); } @@ -236,6 +232,8 @@ template static inline void *hb_object_get_user_data (Type *obj, hb_user_data_key_t *key) { + if (unlikely (!obj || obj->header.is_inert ())) + return NULL; return obj->header.get_user_data (key); }