feat(parserstmt): add shallow recursion guard to ParserStmtBox.parse

This commit is contained in:
nyash-codex
2025-11-17 18:20:29 +09:00
parent 04524f5894
commit 978890e7f6

View File

@ -9,6 +9,17 @@ using lang.compiler.parser.stmt.parser_exception_box
static box ParserStmtBox {
parse(src, i, ctx) {
// Shallow recursion guard: prevent ParserStmtBox.parse from recursively
// re-entering itself on the same VM stackStageB/selfhost 安全弁)。
{
local depth = env.get("HAKO_STAGEB_STMT_DEPTH")
if depth != null && ("" + depth) != "0" {
print("[stageb/recursion] ParserStmtBox.parse recursion detected")
// Fail-fast: return empty stmt and keep position unchanged
return ""
}
env.set("HAKO_STAGEB_STMT_DEPTH", "1")
}
local j = ctx.skip_ws(src, i)
local stmt_start = j
@ -182,6 +193,8 @@ static box ParserStmtBox {
if j < src.length() { j = j + 1 } else { j = src.length() }
}
ctx.gpos_set(j)
// Clear depth guard before returning
env.set("HAKO_STAGEB_STMT_DEPTH", "0")
return "{\"type\":\"Expr\",\"expr\":" + e + "}"
}