llama.cpp
dee778bd - grammar : fix builder crash on invalid minItems/maxItems (#29462) (#29497)

Commit
13 hours ago
grammar : fix builder crash on invalid minItems/maxItems (#29462) (#29497) * bug: fix grammar builder crash on invalid minItems/maxItems (#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 #29462 * grammar : simplify min/max repetition check (review feedback) --------- Co-authored-by: Siddharth U <cid@example.com>
Author
Parents
Loading