Skip to content

Commit 94b8e14

Browse files
fix unbounded recursion in the script expression parser (#1174)
1 parent 6790869 commit 94b8e14

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎src/script_parser.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ class ScriptParser
4646
private:
4747
std::vector<Token> tokens_;
4848
size_t current_ = 0;
49+
int depth_ = 0;
50+
51+
// Upper bound on the nesting of the recursive-descent grammar. Each open
52+
// paren / unary prefix / right-hand operand adds one parseExpr frame, so a
53+
// crafted expression like "((((...0...))))" would otherwise recurse until the
54+
// native stack is exhausted. This mirrors the kMaxNestingDepth cap the XML
55+
// parser already applies in VerifyXML and recursivelyCreateSubtree.
56+
static constexpr int kMaxNestingDepth = 256;
4957

5058
// Binding power constants. Higher value = tighter binding.
5159
static constexpr int kAssignmentBP = 2;
@@ -254,6 +262,18 @@ class ScriptParser
254262
/// Main Pratt expression parser
255263
Ast::expr_ptr parseExpr(int minBP)
256264
{
265+
// Bound the recursion so a deeply nested expression can't overflow the
266+
// stack. Every open paren / unary prefix / right-hand operand adds one
267+
// parseExpr frame; depth_ is decremented before the normal return below so
268+
// it tracks the current nesting rather than the total call count. A parse
269+
// failure throws and discards the parser, so the error paths need no reset.
270+
if(++depth_ > kMaxNestingDepth)
271+
{
272+
throw RuntimeError(StrCat("Parse error at position ", std::to_string(peek().pos),
273+
": expression nesting is too deep (limit ",
274+
std::to_string(kMaxNestingDepth), ")"));
275+
}
276+
257277
auto left = parsePrefix();
258278

259279
while(true)
@@ -293,6 +313,7 @@ class ScriptParser
293313
left = makeBinary(std::move(left), opTok.type, std::move(right));
294314
}
295315

316+
--depth_;
296317
return left;
297318
}
298319

‎tests/script_parser_test.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@ TEST(ParserTest, AnyTypes_Failing)
9898
EXPECT_FALSE(BT::ParseScriptAndExecute(env, "foo").has_value());
9999
}
100100

101+
TEST(ParserTest, DeeplyNestedScriptIsRejected)
102+
{
103+
// A script with thousands of nested parentheses or unary prefixes used to add
104+
// one recursion level per character and overflow the stack (SIGSEGV) at parse
105+
// time. The parser now caps the nesting depth and returns an error instead.
106+
const std::string deep_parens = std::string(20000, '(') + "0" + std::string(20000, ')');
107+
EXPECT_FALSE(BT::ValidateScript(deep_parens));
108+
109+
const std::string deep_unary = std::string(20000, '!') + "0";
110+
EXPECT_FALSE(BT::ValidateScript(deep_unary));
111+
112+
// The depth cap must not reject ordinary scripts: a long flat expression is
113+
// parsed iteratively (shallow recursion) and light nesting is well within it.
114+
std::string wide = "1";
115+
for(int i = 0; i < 1000; ++i)
116+
{
117+
wide += "+1";
118+
}
119+
EXPECT_TRUE(BT::ValidateScript(wide));
120+
EXPECT_TRUE(BT::ValidateScript("((((1))))"));
121+
}
122+
101123
TEST(ParserTest, Equations)
102124
{
103125
BT::Ast::Environment environment = { BT::Blackboard::create(), {} };

0 commit comments

Comments
 (0)