summaryrefslogtreecommitdiffstats
path: root/modules/gdscript/gdscript_tokenizer.cpp
diff options
context:
space:
mode:
authorA Thousand Ships <96648715+AThousandShips@users.noreply.github.com>2024-02-04 13:06:34 +0100
committerA Thousand Ships <96648715+AThousandShips@users.noreply.github.com>2024-02-04 14:35:22 +0100
commit66d55e62f009876b4e71d988a7a1bfb16c8230fa (patch)
tree9fbcacd97952d901d958b44e0bb6217c826439e3 /modules/gdscript/gdscript_tokenizer.cpp
parentb4e2a24c1f62088b3f7ce0197afc90832fc25009 (diff)
downloadredot-engine-66d55e62f009876b4e71d988a7a1bfb16c8230fa.tar.gz
[GDScript] Prevent running `String` number functions on invalid literal
Prevents printing excessive errors.
Diffstat (limited to 'modules/gdscript/gdscript_tokenizer.cpp')
-rw-r--r--modules/gdscript/gdscript_tokenizer.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/modules/gdscript/gdscript_tokenizer.cpp b/modules/gdscript/gdscript_tokenizer.cpp
index 98a3a1268f..29cf7bc6ca 100644
--- a/modules/gdscript/gdscript_tokenizer.cpp
+++ b/modules/gdscript/gdscript_tokenizer.cpp
@@ -672,6 +672,7 @@ GDScriptTokenizer::Token GDScriptTokenizer::number() {
bool has_decimal = false;
bool has_exponent = false;
bool has_error = false;
+ bool need_digits = false;
bool (*digit_check_func)(char32_t) = is_digit;
// Sign before hexadecimal or binary.
@@ -686,11 +687,13 @@ GDScriptTokenizer::Token GDScriptTokenizer::number() {
// Hexadecimal.
base = 16;
digit_check_func = is_hex_digit;
+ need_digits = true;
_advance();
} else if (_peek() == 'b') {
// Binary.
base = 2;
digit_check_func = is_binary_digit;
+ need_digits = true;
_advance();
}
}
@@ -717,6 +720,7 @@ GDScriptTokenizer::Token GDScriptTokenizer::number() {
}
previous_was_underscore = true;
} else {
+ need_digits = false;
previous_was_underscore = false;
}
_advance();
@@ -820,6 +824,16 @@ GDScriptTokenizer::Token GDScriptTokenizer::number() {
}
}
+ if (need_digits) {
+ // No digits in hex or bin literal.
+ Token error = make_error(vformat(R"(Expected %s digit after "0%c".)", (base == 16 ? "hexadecimal" : "binary"), (base == 16 ? 'x' : 'b')));
+ error.start_column = column;
+ error.leftmost_column = column;
+ error.end_column = column + 1;
+ error.rightmost_column = column + 1;
+ return error;
+ }
+
// Detect extra decimal point.
if (!has_error && has_decimal && _peek() == '.' && _peek(1) != '.') {
Token error = make_error("Cannot use a decimal point twice in a number.");