From cacced7e507f7603bacc03ae2616e58f0ede122a Mon Sep 17 00:00:00 2001 From: Hein-Pieter van Braam Date: Thu, 24 Aug 2017 22:58:51 +0200 Subject: 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/ --- modules/visual_script/visual_script.cpp | 22 ++++---- modules/visual_script/visual_script_editor.cpp | 61 ++++++++++------------ modules/visual_script/visual_script_func_nodes.cpp | 25 +++------ modules/visual_script/visual_script_nodes.cpp | 14 ++--- .../visual_script/visual_script_yield_nodes.cpp | 9 ++-- 5 files changed, 54 insertions(+), 77 deletions(-) (limited to 'modules/visual_script') diff --git a/modules/visual_script/visual_script.cpp b/modules/visual_script/visual_script.cpp index b4bdbe16b4..6b69ffdf8d 100644 --- a/modules/visual_script/visual_script.cpp +++ b/modules/visual_script/visual_script.cpp @@ -273,7 +273,9 @@ void VisualScript::_node_ports_changed(int p_id) { Function &func = functions[function]; Ref vsn = func.nodes[p_id].node; - if (OS::get_singleton()->get_main_loop() && OS::get_singleton()->get_main_loop()->cast_to() && Engine::get_singleton()->is_editor_hint()) { + if (OS::get_singleton()->get_main_loop() && + Object::cast_to(OS::get_singleton()->get_main_loop()) && + Engine::get_singleton()->is_editor_hint()) { vsn->validate_input_default_values(); //force validate default values when editing on editor } @@ -336,7 +338,7 @@ void VisualScript::add_node(const StringName &p_func, int p_id, const Refcast_to()) { + if (Object::cast_to(*p_node)) { //the function indeed ERR_EXPLAIN("A function node already has been set here."); ERR_FAIL_COND(func.function_id >= 0); @@ -393,7 +395,7 @@ void VisualScript::remove_node(const StringName &p_func, int p_id) { } } - if (func.nodes[p_id].node->cast_to()) { + if (Object::cast_to(func.nodes[p_id].node.ptr())) { func.function_id = -1; //revert to invalid } @@ -1086,7 +1088,7 @@ int VisualScript::get_member_line(const StringName &p_member) const { #ifdef TOOLS_ENABLED if (has_function(p_member)) { for (Map::Element *E = functions[p_member].nodes.front(); E; E = E->next()) { - if (E->get().node->cast_to()) + if (Object::cast_to(E->get().node.ptr())) return E->key(); } } @@ -2001,9 +2003,9 @@ void VisualScriptInstance::create(const Ref &p_script, Object *p_o max_input_args = 0; max_output_args = 0; - if (p_owner->cast_to()) { + if (Object::cast_to(p_owner)) { //turn on these if they exist and base is a node - Node *node = p_owner->cast_to(); + Node *node = Object::cast_to(p_owner); if (p_script->functions.has("_process")) node->set_process(true); if (p_script->functions.has("_fixed_process")) @@ -2094,16 +2096,16 @@ void VisualScriptInstance::create(const Ref &p_script, Object *p_o } } - if (node->cast_to() || node->cast_to()) { + if (Object::cast_to(node.ptr()) || Object::cast_to(*node)) { //working memory is shared only for this node, for the same variables Ref vslv = node; StringName var_name; - if (node->cast_to()) - var_name = String(node->cast_to()->get_var_name()).strip_edges(); + if (Object::cast_to(*node)) + var_name = String(Object::cast_to(*node)->get_var_name()).strip_edges(); else - var_name = String(node->cast_to()->get_var_name()).strip_edges(); + var_name = String(Object::cast_to(*node)->get_var_name()).strip_edges(); if (!local_var_indices.has(var_name)) { local_var_indices[var_name] = function.max_stack; diff --git a/modules/visual_script/visual_script_editor.cpp b/modules/visual_script/visual_script_editor.cpp index e7aaf0aa4a..61c21227fa 100644 --- a/modules/visual_script/visual_script_editor.cpp +++ b/modules/visual_script/visual_script_editor.cpp @@ -422,7 +422,7 @@ void VisualScriptEditor::_update_graph(int p_only_id) { for (int i = 0; i < graph->get_child_count(); i++) { - if (graph->get_child(i)->cast_to()) { + if (Object::cast_to(graph->get_child(i))) { memdelete(graph->get_child(i)); i--; } @@ -506,7 +506,7 @@ void VisualScriptEditor::_update_graph(int p_only_id) { gnode->set_show_close_button(true); } - if (node->cast_to()) { + if (Object::cast_to(*node)) { LineEdit *line_edit = memnew(LineEdit); line_edit->set_text(node->get_text()); @@ -520,7 +520,7 @@ void VisualScriptEditor::_update_graph(int p_only_id) { gnode->add_child(text); } - if (node->cast_to()) { + if (Object::cast_to(*node)) { Ref vsc = node; gnode->set_comment(true); gnode->set_resizeable(true); @@ -970,7 +970,7 @@ void VisualScriptEditor::_override_pressed(int p_id) { void VisualScriptEditor::_member_button(Object *p_item, int p_column, int p_button) { - TreeItem *ti = p_item->cast_to(); + TreeItem *ti = Object::cast_to(p_item); TreeItem *root = members->get_root(); @@ -1117,8 +1117,8 @@ void VisualScriptEditor::_expression_text_changed(const String &p_text, int p_id undo_redo->commit_action(); Node *node = graph->get_node(itos(p_id)); - if (node->cast_to()) - node->cast_to()->set_size(Vector2(1, 1)); //shrink if text is smaller + if (Object::cast_to(node)) + Object::cast_to(node)->set_size(Vector2(1, 1)); //shrink if text is smaller updating_graph = false; } @@ -1253,7 +1253,7 @@ void VisualScriptEditor::_on_nodes_delete() { List to_erase; for (int i = 0; i < graph->get_child_count(); i++) { - GraphNode *gn = graph->get_child(i)->cast_to(); + GraphNode *gn = Object::cast_to(graph->get_child(i)); if (gn) { if (gn->is_selected() && gn->is_close_button_visible()) { to_erase.push_back(gn->get_name().operator String().to_int()); @@ -1302,7 +1302,7 @@ void VisualScriptEditor::_on_nodes_duplicate() { List to_duplicate; for (int i = 0; i < graph->get_child_count(); i++) { - GraphNode *gn = graph->get_child(i)->cast_to(); + GraphNode *gn = Object::cast_to(graph->get_child(i)); if (gn) { if (gn->is_selected() && gn->is_close_button_visible()) { to_duplicate.push_back(gn->get_name().operator String().to_int()); @@ -1335,7 +1335,7 @@ void VisualScriptEditor::_on_nodes_duplicate() { undo_redo->commit_action(); for (int i = 0; i < graph->get_child_count(); i++) { - GraphNode *gn = graph->get_child(i)->cast_to(); + GraphNode *gn = Object::cast_to(graph->get_child(i)); if (gn) { int id = gn->get_name().operator String().to_int(); gn->set_selected(to_select.has(id)); @@ -1799,7 +1799,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da if (!obj) return; - Node *node = obj->cast_to(); + Node *node = Object::cast_to(obj); Vector2 ofs = graph->get_scroll_ofs() + p_point; if (graph->is_using_snap()) { @@ -1918,7 +1918,7 @@ void VisualScriptEditor::_selected_method(const String &p_method) { void VisualScriptEditor::_draw_color_over_button(Object *obj, Color p_color) { - Button *button = obj->cast_to