From 0b4dec63a912bbc054f18689c73bb0ab9cc0c260 Mon Sep 17 00:00:00 2001
From: Hein-Pieter van Braam-Stewart <hp@tmm.cx>
Date: Fri, 19 Apr 2019 22:03:00 +0200
Subject: [PATCH] Object::script may not be a valid Ref<Script>

It appears that Object::script may be a valid ScriptInstance but not be
castable to Ref<Script>. There were only 5 places in the code that made
this assumption. This commit fixes that.

(cherry picked from commit 20b0046945bfe55591c370e32f4928f7cbeb45e6)
---
 core/object.cpp            |  5 ++++-
 editor/property_editor.cpp | 30 ++++++++++++++++++------------
 scene/main/node.cpp        |  2 +-
 3 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/core/object.cpp b/core/object.cpp
index 58ac5c93b4b..1c5f11d4da2 100644
--- a/core/object.cpp
+++ b/core/object.cpp
@@ -1330,7 +1330,10 @@ Array Object::_get_incoming_connections() const {
 void Object::get_signal_list(List<MethodInfo> *p_signals) const {
 
 	if (!script.is_null()) {
-		Ref<Script>(script)->get_script_signal_list(p_signals);
+		Ref<Script> scr = script;
+		if (scr.is_valid()) {
+			scr->get_script_signal_list(p_signals);
+		}
 	}
 
 	ClassDB::get_signal_list(get_class_name(), p_signals);
diff --git a/editor/property_editor.cpp b/editor/property_editor.cpp
index ac87e2ce193..619af8a4d63 100644
--- a/editor/property_editor.cpp
+++ b/editor/property_editor.cpp
@@ -2397,10 +2397,12 @@ void PropertyEditor::_check_reload_status(const String &p_name, TreeItem *item)
 
 	if (!has_reload && !obj->get_script().is_null()) {
 		Ref<Script> scr = obj->get_script();
-		Variant orig_value;
-		if (scr->get_property_default_value(p_name, orig_value)) {
-			if (orig_value != obj->get(p_name)) {
-				has_reload = true;
+		if (scr.is_valid()) {
+			Variant orig_value;
+			if (scr->get_property_default_value(p_name, orig_value)) {
+				if (orig_value != obj->get(p_name)) {
+					has_reload = true;
+				}
 			}
 		}
 	}
@@ -3558,11 +3560,13 @@ void PropertyEditor::update_tree() {
 
 		if (!has_reload && !obj->get_script().is_null()) {
 			Ref<Script> scr = obj->get_script();
-			Variant orig_value;
-			if (scr->get_property_default_value(p.name, orig_value)) {
-				if (orig_value != obj->get(p.name)) {
-					item->add_button(1, get_icon("ReloadSmall", "EditorIcons"), 3);
-					has_reload = true;
+			if (scr.is_valid()) {
+				Variant orig_value;
+				if (scr->get_property_default_value(p.name, orig_value)) {
+					if (orig_value != obj->get(p.name)) {
+						item->add_button(1, get_icon("ReloadSmall", "EditorIcons"), 3);
+						has_reload = true;
+					}
 				}
 			}
 		}
@@ -3950,9 +3954,11 @@ void PropertyEditor::_edit_button(Object *p_item, int p_column, int p_button) {
 
 		if (!obj->get_script().is_null()) {
 			Ref<Script> scr = obj->get_script();
-			Variant orig_value;
-			if (scr->get_property_default_value(prop, orig_value)) {
-				_edit_set(prop, orig_value);
+			if (scr.is_valid()) {
+				Variant orig_value;
+				if (scr->get_property_default_value(prop, orig_value)) {
+					_edit_set(prop, orig_value);
+				}
 			}
 		}
 
diff --git a/scene/main/node.cpp b/scene/main/node.cpp
index e6de2d78f53..5e85b315350 100644
--- a/scene/main/node.cpp
+++ b/scene/main/node.cpp
@@ -2560,7 +2560,7 @@ void Node::_replace_connections_target(Node *p_new_target) {
 
 		if (c.flags & CONNECT_PERSIST) {
 			c.source->disconnect(c.signal, this, c.method);
-			bool valid = p_new_target->has_method(c.method) || p_new_target->get_script().is_null() || Ref<Script>(p_new_target->get_script())->has_method(c.method);
+			bool valid = p_new_target->has_method(c.method) || Ref<Script>(p_new_target->get_script()).is_null() || Ref<Script>(p_new_target->get_script())->has_method(c.method);
 			ERR_EXPLAIN("Attempt to connect signal \'" + c.source->get_class() + "." + c.signal + "\' to nonexistent method \'" + c.target->get_class() + "." + c.method + "\'");
 			ERR_CONTINUE(!valid);
 			c.source->connect(c.signal, p_new_target, c.method, c.binds, c.flags);