Skip to content

bound the AST height of flat script expressions - #1259

Merged
facontidavide merged 3 commits into
BehaviorTree:masterfrom
aysha-afrah26:script-expression-height
Nov 7, 2026
Merged

facontidavide merged 3 commits into
BehaviorTree:masterfrom
aysha-afrah26:script-expression-height

Conversation

@aysha-afrah26

Copy link
Copy Markdown
Contributor

The nesting cap from #1174 limits how deep ScriptParser::parseExpr recurses, but not how tall the resulting AST gets. A left-associative chain like 1+1+1+... is consumed by the while loop in parseExpr at depth 1, yet each operator wraps the previous result as its lhs, so the tree is as tall as the expression is long. ExprBinaryArithmetic::evaluate() then recurses once per level, and so does the shared_ptr destructor chain when the AST is freed.

So a long Script, _skipIf, _while or postcondition attribute passes createTreeFromText and then takes the process down on the first tick:

<Script code="x := 1+1+1+ ... +1"/>     50,000 terms, about 100 KB
-> SIGSEGV, stack overflow in ExprBinaryArithmetic::evaluate

How long the script has to be depends on the stack of the thread that ticks the tree. In a Release build on macOS I measured about 256 bytes per level:

  • 512 KB stack (the macOS default for std::thread): crashes at 2,000 terms, roughly 4 KB of script
  • 1 MB stack: crashes at 4,000 terms
  • 8 MB main thread: crashes at 32,000 terms
  • around 1M terms crash ValidateScript itself, while the AST is destroyed

The parser now tracks the height of the expression it builds and throws a regular parse error past 1024 levels. That limit is deliberately higher than kMaxNestingDepth, so the 1,000-term flat expression that DeeplyNestedScriptIsRejected expects to be valid still parses and evaluates (I also checked it under ASan+UBSan and on a 512 KB thread). Heights add up across parentheses, so ((1+1+...)+1+...) can't sidestep the limit by splitting the chain.

The change is private to ScriptParser in script_parser.cpp, so there is no API or ABI change. The only behavior change is that a single expression taller than 1024 levels is rejected when the tree is created, instead of crashing when it runs.

ParserTest.LongFlatScriptIsRejected crashes with SIGSEGV on master and passes with the fix. The full ctest suite passes in Release and with ASan+UBSan; the one Release failure, LoggerTest.SqliteLogger_ExecSqlStatement, fails the same way on untouched master. pre-commit (clang-format, codespell) passes. clang-tidy did not run locally because clangd-21 isn't installed here.

aysha-afrah26 and others added 3 commits October 25, 2026 23:01
The nesting cap in ScriptParser::parseExpr counts parser recursion only.
A flat chain such as 1+1+1+... is parsed by the loop without recursing,
but it builds a left-deep AST that evaluate() and the node destructors
walk recursively, one frame per operator, so a long Script attribute
passes tree creation and overflows the stack on the first tick.

Track the height of the expression being built and reject it past 1024
levels, so the evaluation depth no longer grows with the script length.
The height limit of 1024 still let accepted scripts overflow the 1 MB default
stack of an unoptimized Windows build when evaluated: each level of
ExprBinaryArithmetic::evaluate() can take about 1 KB there, which is why
ParserTest.LongFlatScriptIsRejected crashed in the windows-2022 Debug job.
Use kMaxNestingDepth (256) for both limits, shorten the accepted 'wide'
scripts to 200 terms and check that 300 terms are rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@facontidavide

Copy link
Copy Markdown
Collaborator

Thanks @aysha-afrah26. The height accounting is correct, and it sits in the one place every script goes through. The limit was too high, though. Evaluating (and destroying) a flat chain recurses once per operator, and in an unoptimized MSVC build each ExprBinaryArithmetic::evaluate() level can take about 1 KB of stack. That makes a 1000-term script, which the PR accepts, overflow the 1 MB default stack when it is ticked. That's the windows-2022, Debug segfault in ParserTest.LongFlatScriptIsRejected.

I pushed f7cc673 on top of a merge of master: the height limit is now kMaxNestingDepth (256), the accepted "wide" scripts are shortened to 200 terms, and a check confirms 300 terms are rejected. Locally all tests pass, the parser tests also pass under ulimit -s 1024, and I'll merge once Windows Debug is green. Behavior change for the release notes: a single expression with more than about 255 chained operators is now rejected when the tree is created.

@facontidavide
facontidavide merged commit 180324d into BehaviorTree:master Nov 7, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants