]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commit
common/json-schema: fix: handle non-capturing groups (?:...) in JSON schema pattern...
authorAdrien <redacted>
Sat, 28 Mar 2026 16:55:38 +0000 (17:55 +0100)
committerGitHub <redacted>
Sat, 28 Mar 2026 16:55:38 +0000 (17:55 +0100)
commite397d3885c40fac0b91bc2784c2d948a259de8a4
treea1a48f88a2c4975d202760d7aa987170eac7bfd2
parente6f2ec01ff808b60f80ffcb46c748f871576c8f3
common/json-schema: fix: handle non-capturing groups (?:...) in JSON schema pattern converter (#21124)

The regex-to-grammar converter in _visit_pattern() crashes with SIGSEGV
when a JSON schema "pattern" field contains a non-capturing group (?:...).

Root cause: when the parser sees '(' followed by '?', it pushes a warning
but does not advance past '?:'. The recursive transform() call then
interprets '?' as a quantifier and calls seq.back() on an empty vector,
causing undefined behavior.

This commonly occurs when serving OpenAI-compatible tool calls from
clients that include complex regex patterns in their JSON schemas (e.g.,
date validation patterns like ^(?:(?:\d\d[2468][048]|...)-02-29|...)$).

The fix:
- Skip '?:' after '(' to treat non-capturing groups as regular groups
- For unsupported syntax (?=, ?!, etc.), skip to matching ')' safely,
  handling escaped characters to avoid miscounting parenthesis depth
- Adjust the ')' unbalanced-parentheses check using direct char
  comparisons instead of substr
- Add test cases for non-capturing groups (C++ only, as the JS/Python
  implementations do not yet support this syntax)
common/json-schema-to-grammar.cpp
tests/test-json-schema-to-grammar.cpp