aboutsummaryrefslogtreecommitdiff
path: root/src/parser.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2024-08-24 12:59:17 +0100
committergingerBill <bill@gingerbill.org>2024-08-24 12:59:17 +0100
commita4cc207022712c238e827bda9e871870970cca25 (patch)
tree0b296d248b4703cb52db1869a30ec69884746628 /src/parser.cpp
parent683dde1fa011056477423ded1bc6098ac2636675 (diff)
Add a recursion depth limit for #3987 with a consideration to use a `switch` statement or refactor the code to not use a large if-else chain
Diffstat (limited to 'src/parser.cpp')
-rw-r--r--src/parser.cpp8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/parser.cpp b/src/parser.cpp
index 5796012d9..6461c9dd0 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -4532,6 +4532,12 @@ gb_internal Ast *parse_if_stmt(AstFile *f) {
return ast_bad_stmt(f, f->curr_token, f->curr_token);
}
+ if (f->recursion_depth_else_if > 256) {
+ syntax_error(f->curr_token, "if-else chain recursion depth limit hit. Consider using a 'switch' statement instead or refactor the code to not require a large if-else chain");
+ f->recursion_depth_else_if = 0;
+ return ast_bad_stmt(f, f->curr_token, f->curr_token);
+ }
+
Token token = expect_token(f, Token_if);
Ast *init = nullptr;
Ast *cond = nullptr;
@@ -4577,7 +4583,9 @@ gb_internal Ast *parse_if_stmt(AstFile *f) {
Token else_token = expect_token(f, Token_else);
switch (f->curr_token.kind) {
case Token_if:
+ f->recursion_depth_else_if += 1;
else_stmt = parse_if_stmt(f);
+ f->recursion_depth_else_if -= 1;
break;
case Token_OpenBrace:
else_stmt = parse_block_stmt(f, false);