Repository navigation
Conversation
This branch has not been deployed
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
By experimenting with structured outputs, I noticed that
llama-servercrashes via Segfault if it receives a malformed regex pattern in a JSON schema (e.g.,pattern: "^+[a-z]$"orpattern: "^{2}times$").The crash happens in
common_schema_converter::_visit_patternbecause the quantifier branches evaluate before ensuring theseqvector has elements, leading toseq.back()accessing unallocated memory. Additionally, deeply nested parentheses can cause the recursivetransformlambda to exhaust the call stack.Changes in this PR:
Added
if (seq.empty())bounds checks for*,+,?, and{branches, returning a descriptive error.Added a
depthparameter to the recursivetransform()lambda, capping nesting at 100 to gracefully return an error instead of overflowing the stack.Additional information
Reproduction Script
This Python script consistently crashes the unpatched server by passing a malformed regex in the
response_formatJSON schema. With this patch, the server gracefully returns a 400 error indicating the pattern is invalid.After patching, the server returns error messages for wrong payloads:
"^+[a-zA-Z0-9 ]$"
"^{2}times$"
Tested on Windows 10 x64, Gemma-4 model.
Requirements
I have read and agree with the contributing guidelines
AI usage disclosure: NO - The core fix was authored by a human contributor. AI was used to double-check that everything was fixed, and for grammar suggestions on the PR description.