Repository navigation
bug: fix grammar builder crash on invalid minItems/maxItems (#29462) - #29497
Merged
Merged
Conversation
Cid-oe
requested review from
a team,
CISC,
IMbackK,
JohannesGaessler,
bartowski1182,
danbev,
ggerganov,
ngxson,
pwilkin and
wine99
as code owners
September 26, 2026 21:03
CISC
removed request for
a team,
CISC,
IMbackK,
JohannesGaessler,
bartowski1182,
danbev,
ggerganov,
ngxson,
pwilkin and
wine99
September 27, 2026 07:53
Member
|
Fix the bad merge please. :) |
…#29462) When parsing JSON schemas with `minItems` greater than `maxItems` (e.g., `{"minItems": 10, "maxItems": 2}`), or direct GBNF like `{10,2}`, `llama-grammar.cpp` previously underflowed the `(max_times - min_times)` subtraction because it was evaluating it in unsigned arithmetic without a `min_times <= max_times` check. This underflow led to allocating quintillions of grammar rules, resulting in an immediate OOM / `std::bad_alloc`. Additionally, the `json-schema-to-grammar.cpp` builder lacked a check to cap `max_items = min_items` when the JSON schema contained logically contradictory inputs, leading to out-of-order bounds being forwarded to the grammar parser. This PR adds: 1. A guard in `json-schema-to-grammar.cpp` that sets `max_items = min_items` if `min_items > max_items`. 2. A strict runtime validation in `llama-grammar.cpp` that explicitly rejects out-of-order repetition bounds with a `std::runtime_error`, preventing catastrophic memory allocation. Closes ggml-org#29462
Contributor
Author
|
@CISC Thanks for flagging! The branch has been cleanly rebased onto the latest master, |
CISC
approved these changes
Nov 30, 2026
CISC
left a comment
Member
There was a problem hiding this comment.
Please update OP to use our PR template.
aldehir
approved these changes
Nov 30, 2026
CISC
approved these changes
Dec 1, 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
Closes #29462.
A grammar with a minimum repeat count greater than the maximum (JSON schema
minItems: 10, maxItems: 2, or GBNF"a"{10,2}) underflowed the unsignedmax_times - min_timesinllama-grammar.cpp. That produced a huge allocation and astd::bad_alloccrash inllama-server.This PR:
max_items = min_itemsinbuild_repetition(json-schema-to-grammar.cpp) when the schema has contradictory bounds.std::runtime_errorinhandle_repetitions(llama-grammar.cpp) ifmin_times > max_timesreaches the parser.Additional information
Fixes #29462.
Requirements