Rewrite this==NULL checks to avoid undefined behavior

Fixes https://code.google.com/p/chromium/issues/detail?id=403594
This commit is contained in:
Behdad Esfahbod 2014-08-14 13:05:36 -04:00
parent cc3b2d432b
commit a5a27073cf
1 changed files with 8 additions and 10 deletions

View File

@ -129,15 +129,11 @@ struct hb_object_header_t
template <typename Type> friend Type *hb_object_reference (Type *obj); template <typename Type> friend Type *hb_object_reference (Type *obj);
inline void reference (void) { inline void reference (void) {
if (unlikely (!this || this->is_inert ()))
return;
ref_count.inc (); ref_count.inc ();
} }
template <typename Type> friend bool hb_object_destroy (Type *obj); template <typename Type> friend bool hb_object_destroy (Type *obj);
inline bool destroy (void) { inline bool destroy (void) {
if (unlikely (!this || this->is_inert ()))
return false;
if (ref_count.dec () != 1) if (ref_count.dec () != 1)
return false; return false;
@ -156,18 +152,12 @@ struct hb_object_header_t
void * data, void * data,
hb_destroy_func_t destroy_func, hb_destroy_func_t destroy_func,
hb_bool_t replace) { hb_bool_t replace) {
if (unlikely (!this || this->is_inert ()))
return false;
return user_data.set (key, data, destroy_func, replace); return user_data.set (key, data, destroy_func, replace);
} }
template <typename type> friend void *hb_object_get_user_data (type *obj, template <typename type> friend void *hb_object_get_user_data (type *obj,
hb_user_data_key_t *key); hb_user_data_key_t *key);
inline void *get_user_data (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); return user_data.get (key);
} }
@ -212,6 +202,8 @@ static inline bool hb_object_is_inert (const Type *obj)
template <typename Type> template <typename Type>
static inline Type *hb_object_reference (Type *obj) static inline Type *hb_object_reference (Type *obj)
{ {
if (unlikely (!obj || obj->header.is_inert ()))
return obj;
hb_object_trace (obj, HB_FUNC); hb_object_trace (obj, HB_FUNC);
obj->header.reference (); obj->header.reference ();
return obj; return obj;
@ -219,6 +211,8 @@ static inline Type *hb_object_reference (Type *obj)
template <typename Type> template <typename Type>
static inline bool hb_object_destroy (Type *obj) static inline bool hb_object_destroy (Type *obj)
{ {
if (unlikely (!obj || obj->header.is_inert ()))
return false;
hb_object_trace (obj, HB_FUNC); hb_object_trace (obj, HB_FUNC);
return obj->header.destroy (); return obj->header.destroy ();
} }
@ -229,6 +223,8 @@ static inline bool hb_object_set_user_data (Type *obj,
hb_destroy_func_t destroy, hb_destroy_func_t destroy,
hb_bool_t replace) hb_bool_t replace)
{ {
if (unlikely (!obj || obj->header.is_inert ()))
return false;
return obj->header.set_user_data (key, data, destroy, replace); return obj->header.set_user_data (key, data, destroy, replace);
} }
@ -236,6 +232,8 @@ template <typename Type>
static inline void *hb_object_get_user_data (Type *obj, static inline void *hb_object_get_user_data (Type *obj,
hb_user_data_key_t *key) hb_user_data_key_t *key)
{ {
if (unlikely (!obj || obj->header.is_inert ()))
return NULL;
return obj->header.get_user_data (key); return obj->header.get_user_data (key);
} }