Merge pull request #35524 from neikeq/issue-35496

Fix C# preprocessor infinite loop and incorrect parsing of `#if!`
This commit is contained in:
Rémi Verschelde 2020-01-24 21:51:38 +01:00 committed by GitHub
commit 75ab07546b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -635,13 +635,14 @@ static String get_preprocessor_directive(const String &p_line, int p_from) {
CRASH_COND(p_line[p_from] != '#'); CRASH_COND(p_line[p_from] != '#');
p_from++; p_from++;
int i = p_from; int i = p_from;
while (i < p_line.length() && p_line[i] != ' ' && p_line[i] != '\t') { while (i < p_line.length() && (p_line[i] == '_' || (p_line[i] >= 'A' && p_line[i] <= 'Z') ||
(p_line[i] >= 'a' && p_line[i] <= 'z') || p_line[i] > 127)) {
i++; i++;
} }
return p_line.substr(p_from, i - p_from); return p_line.substr(p_from, i - p_from);
} }
static void run_dummy_preprocessor(String &r_source) { static void run_dummy_preprocessor(String &r_source, const String &p_filepath) {
Vector<String> lines = r_source.split("\n", /* p_allow_empty: */ true); Vector<String> lines = r_source.split("\n", /* p_allow_empty: */ true);
@ -655,8 +656,8 @@ static void run_dummy_preprocessor(String &r_source) {
const int line_len = line.length(); const int line_len = line.length();
int j = 0; int j;
while (j < line_len) { for (j = 0; j < line_len; j++) {
if (line[j] != ' ' && line[j] != '\t') { if (line[j] != ' ' && line[j] != '\t') {
if (line[j] == '#') { if (line[j] == '#') {
// First non-whitespace char of the line is '#' // First non-whitespace char of the line is '#'
@ -668,13 +669,13 @@ static void run_dummy_preprocessor(String &r_source) {
if_level++; if_level++;
is_branch_being_compiled.push_back(if_level == 0 || is_branch_being_compiled[if_level - 1]); is_branch_being_compiled.push_back(if_level == 0 || is_branch_being_compiled[if_level - 1]);
} else if (directive == "elif") { } else if (directive == "elif") {
ERR_CONTINUE_MSG(if_level == -1, "Found unexpected '#elif' directive."); ERR_CONTINUE_MSG(if_level == -1, "Found unexpected '#elif' directive. File: '" + p_filepath + "'.");
is_branch_being_compiled.write[if_level] = false; is_branch_being_compiled.write[if_level] = false;
} else if (directive == "else") { } else if (directive == "else") {
ERR_CONTINUE_MSG(if_level == -1, "Found unexpected '#else' directive."); ERR_CONTINUE_MSG(if_level == -1, "Found unexpected '#else' directive. File: '" + p_filepath + "'.");
is_branch_being_compiled.write[if_level] = false; is_branch_being_compiled.write[if_level] = false;
} else if (directive == "endif") { } else if (directive == "endif") {
ERR_CONTINUE_MSG(if_level == -1, "Found unexpected '#endif' directive."); ERR_CONTINUE_MSG(if_level == -1, "Found unexpected '#endif' directive. File: '" + p_filepath + "'.");
is_branch_being_compiled.remove(if_level); is_branch_being_compiled.remove(if_level);
if_level--; if_level--;
} }
@ -686,8 +687,6 @@ static void run_dummy_preprocessor(String &r_source) {
break; break;
} }
} }
j++;
} }
if (j == line_len) { if (j == line_len) {
@ -722,7 +721,7 @@ Error ScriptClassParser::parse_file(const String &p_filepath) {
" Please ensure that scripts are saved in valid UTF-8 unicode." : " Please ensure that scripts are saved in valid UTF-8 unicode." :
"Failed to read file: '" + p_filepath + "'."); "Failed to read file: '" + p_filepath + "'.");
run_dummy_preprocessor(source); run_dummy_preprocessor(source, p_filepath);
return parse(source); return parse(source);
} }