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

@ -85,10 +85,7 @@ Node *VisualScriptFunctionCall::_get_base_node() const {
return NULL;
MainLoop *main_loop = OS::get_singleton()->get_main_loop();
if (!main_loop)
return NULL;
SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
if (!scene_tree)
return NULL;
@ -776,7 +773,7 @@ public:
if (!p_base)
return false;
Node *node = p_base->cast_to<Node>();
Node *node = Object::cast_to<Node>(p_base);
if (!node)
return false;
@ -817,7 +814,7 @@ public:
} break;
case VisualScriptFunctionCall::CALL_MODE_NODE_PATH: {
Node *node = instance->get_owner_ptr()->cast_to<Node>();
Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
if (!node) {
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
r_error_str = "Base object is not a Node!";
@ -961,10 +958,8 @@ Node *VisualScriptPropertySet::_get_base_node() const {
return NULL;
MainLoop *main_loop = OS::get_singleton()->get_main_loop();
if (!main_loop)
return NULL;
SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
if (!scene_tree)
return NULL;
@ -1159,9 +1154,7 @@ String VisualScriptPropertySet::get_base_script() const {
void VisualScriptPropertySet::_update_cache() {
if (!OS::get_singleton()->get_main_loop())
return;
if (!OS::get_singleton()->get_main_loop()->cast_to<SceneTree>())
if (!Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop()))
return;
if (!Engine::get_singleton()->is_editor_hint()) //only update cache if editor exists, it's pointless otherwise
@ -1595,7 +1588,7 @@ public:
} break;
case VisualScriptPropertySet::CALL_MODE_NODE_PATH: {
Node *node = instance->get_owner_ptr()->cast_to<Node>();
Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
if (!node) {
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
r_error_str = "Base object is not a Node!";
@ -1730,10 +1723,8 @@ Node *VisualScriptPropertyGet::_get_base_node() const {
return NULL;
MainLoop *main_loop = OS::get_singleton()->get_main_loop();
if (!main_loop)
return NULL;
SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
if (!scene_tree)
return NULL;
@ -2242,7 +2233,7 @@ public:
} break;
case VisualScriptPropertyGet::CALL_MODE_NODE_PATH: {
Node *node = instance->get_owner_ptr()->cast_to<Node>();
Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
if (!node) {
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
r_error_str = RTR("Base object is not a Node!");