Repository navigation
grammar: bound recursion depth to prevent parser stack overflow (#29495) - #29755
Merged
ngxson merged 1 commit intoOct 25, 2026
Merged
Conversation
ServeurpersoCom
approved these changes
Oct 25, 2026
ServeurpersoCom
left a comment
Contributor
There was a problem hiding this comment.
LGTM, tested on Linux against llama-server: a grammar with 50,000 nested groups segfaults on master and returns a 400 with this PR, 300 nested groups are rejected cleanly, and long chains of named rules still work. Bounding the recursion where it happens is the pattern ngxson set in #30242.
ngxson
approved these changes
Oct 25, 2026
auRose94
pushed a commit
to auRose94/llama.cpp
that referenced
this pull request
Oct 28, 2026
edwardyoon
pushed a commit
to edwardyoon/focus-llama
that referenced
this pull request
Oct 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Fixes #29495.
In
src/llama-grammar.cpp,llama_grammar_parser::parse_alternatesandparse_sequencewere mutually recursive when parsing nested groupings(with no recursion depth bound. A client-supplied grammar payload containing deeply nested parenthesized groups or nested alternations (e.g.root ::= (((... "x" ...)))) caused unbounded call-stack consumption, leading to a stack overflow and terminatingllama-serverviaSIGSEGV(Denial of Service).Changes:
MAX_GRAMMAR_RECURSION_DEPTH 256insrc/llama-grammar.cpp.depthtracking acrossparse_alternatesandparse_sequenceinsrc/llama-grammar.handsrc/llama-grammar.cpp(defaulting to0for complete backward compatibility).(: throwsstd::runtime_errorwhen depth exceedsMAX_GRAMMAR_RECURSION_DEPTH, enablingllama_grammar_parser::parse()to catch the exception, clear rules, and returnfalsecleanly instead of crashing the process.tests/test-grammar-parser.cppverifying that deeply-nested groupings (> 256) are safely rejected.Additional information
test-grammar-parser: all 20+ grammar parser test cases pass without regressions.Requirements