fix: bound plain-text runs in parse_prompt_attention regex - #1919
Open
fszontagh wants to merge 2 commits into
Open
fix: bound plain-text runs in parse_prompt_attention regex#1919fszontagh wants to merge 2 commits into
fszontagh wants to merge 2 commits into
Conversation
leejet
requested changes
Aug 30, 2026
leejet
left a comment
Owner
There was a problem hiding this comment.
The plain-text repetition is bounded now, but the same stack-overflow path still exists in the weight alternative: :([+-]?[.\d]+)\) contains another unbounded +.
With libstdc++ (g++ 14.2, default 8 MiB stack), the PR still segfaults for e.g.:
":" + std::string(30000, '1')A closing ) is not required: the regex recursively consumes the digit run while attempting the weight alternative before falling back to the literal : alternative.
Could we bound the numeric run as well (to a realistic float length), or avoid unbounded std::regex repetitions in this lexer entirely? We should also make sure oversized/invalid weights cannot terminate through std::stof.
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.
Summary
parse_prompt_attentionlexes the prompt with an unbounded plain-text alternative. libstdc++'sstd::regexrecurses once per matched character, so a prompt of a few tens of kilobytes overflows the stack and segfaults before tokenization.Bound the run length. Splitting a long run is safe because the function's final pass merges adjacent segments of equal weight, and
Bis excluded from the character class, so a chunk boundary can never fall inside aBREAK.Related Issue / Discussion
None.
Additional Information
A 45KB prompt exits 139 (SIGSEGV) during tokenization before the change and completes normally after it.
Parsing results are unchanged: comparing old and new output across weighted parentheses, nested
((...)),[...], escaped\(, multipleBREAKs,BREAKINGand bareB, bare colons, the empty string, multi-byte UTF-8, and a 21KB run both bare and inside(...:1.3), every case is byte-identical.Checklist