Skip to content

bug: fix grammar builder crash on invalid minItems/maxItems (#29462) - #29497

Merged
CISC merged 2 commits into
ggml-org:masterfrom
Cid-oe:fix/issue-29462
Dec 1, 2026
Merged

CISC merged 2 commits into
ggml-org:masterfrom
Cid-oe:fix/issue-29462

Conversation

@Cid-oe

@Cid-oe Cid-oe commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

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 unsigned max_times - min_times in llama-grammar.cpp. That produced a huge allocation and a std::bad_alloc crash in llama-server.

This PR:

  1. Clamps max_items = min_items in build_repetition (json-schema-to-grammar.cpp) when the schema has contradictory bounds.
  2. Throws a clean std::runtime_error in handle_repetitions (llama-grammar.cpp) if min_times > max_times reaches the parser.

Additional information

Fixes #29462.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes. Code written by Qwen 3.5 9B, supervised by Antigravity (Gemini). Reviewed by me.

@github-actions github-actions Bot added documentation Improvements or additions to documentation model Model specific build Compilation issues testing Everything test related nix Issues specific to consuming flake.nix, or generally concerned with ❄ Nix-based llama.cpp deployment Vulkan Issues specific to the Vulkan backend devops improvements to build systems and github actions server labels Sep 26, 2026
@CISC CISC removed the documentation Improvements or additions to documentation label Sep 27, 2026
@CISC

CISC commented Sep 27, 2026

Copy link
Copy Markdown
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
@Cid-oe

Cid-oe commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

@CISC Thanks for flagging! The branch has been cleanly rebased onto the latest master,
merge conflicts are resolved, and all CI checks are green. Ready for review whenever
you have a moment.

@CISC CISC left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update OP to use our PR template.

Comment thread src/llama-grammar.cpp Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants