From 3d98247678b8986f0696973177badaef480c70fb Mon Sep 17 00:00:00 2001 From: Danil Alexeev Date: Thu, 6 Jul 2023 20:32:30 +0300 Subject: [PATCH] [3.x] GDScript: Prevent native class shadowing --- modules/gdscript/gdscript_parser.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 93f9852602a..d97f78b093c 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -3758,7 +3758,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { return; } - if (ClassDB::class_exists(p_class->name)) { + if (ClassDB::class_exists(p_class->name) || ClassDB::class_exists("_" + p_class->name.operator String())) { _set_error("The class \"" + p_class->name + "\" shadows a native class."); return; } @@ -4922,6 +4922,12 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { member.usages = 0; member.rpc_mode = rpc_mode; + // GH-57496 + if (ClassDB::class_exists(member.identifier) || ClassDB::class_exists("_" + member.identifier.operator String())) { + _set_error("Variable \"" + String(member.identifier) + "\" shadows a native class."); + return; + } + if (current_class->constant_expressions.has(member.identifier)) { _set_error("A constant named \"" + String(member.identifier) + "\" already exists in this class (at line: " + itos(current_class->constant_expressions[member.identifier].expression->line) + ")."); @@ -5180,6 +5186,12 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { StringName const_id = tokenizer->get_token_literal(); int line = tokenizer->get_token_line(); + // GH-57496 + if (ClassDB::class_exists(const_id) || ClassDB::class_exists("_" + const_id.operator String())) { + _set_error("Constant \"" + String(const_id) + "\" shadows a native class."); + return; + } + if (current_class->constant_expressions.has(const_id)) { _set_error("Constant \"" + String(const_id) + "\" already exists in this class (at line " + itos(current_class->constant_expressions[const_id].expression->line) + ")."); @@ -5256,6 +5268,12 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->is_token_literal(0, true)) { enum_name = tokenizer->get_token_literal(); + // GH-57496 + if (ClassDB::class_exists(enum_name) || ClassDB::class_exists("_" + enum_name)) { + _set_error("Enumeration \"" + enum_name + "\" shadows a native class."); + return; + } + if (current_class->constant_expressions.has(enum_name)) { _set_error("A constant named \"" + String(enum_name) + "\" already exists in this class (at line " + itos(current_class->constant_expressions[enum_name].expression->line) + ")."); @@ -5348,6 +5366,12 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (enum_name != "") { enum_dict[const_id] = enum_value_expr->value; } else { + // GH-57496 + if (ClassDB::class_exists(const_id) || ClassDB::class_exists("_" + const_id.operator String())) { + _set_error("Constant \"" + String(const_id) + "\" shadows a native class."); + return; + } + if (current_class->constant_expressions.has(const_id)) { _set_error("A constant named \"" + String(const_id) + "\" already exists in this class (at line " + itos(current_class->constant_expressions[const_id].expression->line) + ").");