浏览代码

PackedScene: Prevent crash when root node has `parent` attribute

The crash happens further down when setting an invalid owner in
`Node::_set_owner_nocheck` but I couldn't figure out how to fix it.

But here the proper fix is to catch the invalid scene file early on
and fail loading it.

Part of #17372.

(cherry picked from commit c080ec5da220474a80789afa33cc4f5612cddb50)
Rémi Verschelde 5 年之前
父节点
当前提交
f3fcdfbdd0
共有 2 个文件被更改,包括 5 次插入2 次删除
  1. 2 2
      editor/editor_node.cpp
  2. 3 0
      scene/resources/packed_scene.cpp

+ 2 - 2
editor/editor_node.cpp

@@ -3426,13 +3426,13 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
 	if (!new_scene) {
 
 		sdata.unref();
-		_dialog_display_load_error(lpath, ERR_FILE_NOT_FOUND);
+		_dialog_display_load_error(lpath, ERR_FILE_CORRUPT);
 		opening_prev = false;
 		if (prev != -1) {
 			set_current_scene(prev);
 			editor_data.remove_scene(idx);
 		}
-		return ERR_FILE_NOT_FOUND;
+		return ERR_FILE_CORRUPT;
 	}
 
 	if (p_set_inherited) {

+ 3 - 0
scene/resources/packed_scene.cpp

@@ -101,6 +101,9 @@ Node *SceneState::instance(GenEditState p_edit_state) const {
 			}
 #endif
 			parent = nparent;
+		} else {
+			// i == 0 is root node. Confirm that it doesn't have a parent defined.
+			ERR_FAIL_COND_V_MSG(n.parent != -1, nullptr, vformat("Invalid scene: root node %s cannot specify a parent node.", snames[n.name]));
 		}
 
 		Node *node = NULL;