Convert Object::cast_to() to the static version

Currently we rely on some undefined behavior when Object->cast_to() gets
called with a Null pointer. This used to work fine with GCC < 6 but
newer versions of GCC remove all codepaths in which the this pointer is
Null. However, the non-static cast_to() was supposed to be null safe.

This patch makes cast_to() Null safe and removes the now redundant Null
checks where they existed.

It is explained in this article: https://www.viva64.com/en/b/0226/
This commit is contained in:
Hein-Pieter van Braam
2017-08-24 22:58:51 +02:00
parent 4aa2c18cb4
commit cacced7e50
185 changed files with 1314 additions and 1508 deletions

View File

@ -598,46 +598,6 @@ public:
#endif
}
// TODO: ensure 'this' is never NULL since it's UB, but by now, avoid warning flood
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundefined-bool-conversion"
#endif
template <class T>
T *cast_to() {
#ifndef NO_SAFE_CAST
return SAFE_CAST<T *>(this);
#else
if (!this)
return NULL;
if (is_class_ptr(T::get_class_ptr_static()))
return static_cast<T *>(this);
else
return NULL;
#endif
}
template <class T>
const T *cast_to() const {
#ifndef NO_SAFE_CAST
return SAFE_CAST<const T *>(this);
#else
if (!this)
return NULL;
if (is_class_ptr(T::get_class_ptr_static()))
return static_cast<const T *>(this);
else
return NULL;
#endif
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
enum {
NOTIFICATION_POSTINITIALIZE = 0,