Skip to content

Commit c46583b

Browse files
jpohhhhpwilkin
andauthored
common/parser : fix out_of_range crash in throw path (ggml-org#20424 regression) (ggml-org#20777)
* chat : fix out_of_range crash in throw path (ggml-org#20424 regression) ggml-org#20424 introduced effective_input = generation_prompt + input, but the throw path uses input.substr(result.end) where result.end is a position within effective_input. Every thinking model with a non-empty generation_prompt crashes with std::out_of_range instead of the intended error message. Test crashes on unpatched master, passes with fix: cmake -B build -DLLAMA_BUILD_TESTS=ON -DLLAMA_BUILD_TOOLS=OFF cmake --build build --target test-chat ./build/bin/test-chat * Update test-chat.cpp * Update test-chat.cpp * Update test-chat.cpp --------- Co-authored-by: Piotr Wilkin (ilintar) <piotr.wilkin@syndatis.com>
1 parent c1b9116 commit c46583b

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

common/chat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,7 @@ common_chat_msg common_chat_peg_parse(const common_peg_arena & src_pars
17741774
return msg;
17751775
}
17761776
throw std::runtime_error(std::string("Failed to parse input at pos ") + std::to_string(result.end) + ": " +
1777-
input.substr(result.end));
1777+
effective_input.substr(result.end));
17781778
}
17791779

17801780
common_chat_msg msg;

tests/test-chat.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,61 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
19541954
}
19551955
}
19561956

1957+
// Verify the throw path produces a readable error message, not std::out_of_range.
1958+
// #20424 introduced effective_input = generation_prompt + input, but the throw
1959+
// uses input.substr(result.end) where result.end is in effective_input space.
1960+
{
1961+
auto tmpls = common_chat_templates_ptr(
1962+
common_chat_templates_init(nullptr, read_file("models/templates/GLM-4.7-Flash.jinja")));
1963+
1964+
static common_chat_tool weather_tool{
1965+
"get_weather", "Get weather",
1966+
R"({"type":"object","properties":{"city":{"type":"string"}},"required":["city"]})",
1967+
};
1968+
1969+
common_chat_templates_inputs inputs;
1970+
inputs.tools = { weather_tool };
1971+
inputs.enable_thinking = true;
1972+
inputs.reasoning_format = COMMON_REASONING_FORMAT_AUTO;
1973+
inputs.add_generation_prompt = true;
1974+
inputs.use_jinja = true;
1975+
common_chat_msg msg;
1976+
msg.role = "user";
1977+
msg.content = "get_weather";
1978+
inputs.messages = { msg };
1979+
1980+
auto params = common_chat_templates_apply(tmpls.get(), inputs);
1981+
common_peg_arena arena;
1982+
arena.load(params.parser);
1983+
common_chat_parser_params pp(params);
1984+
1985+
// generation_prompt is non-empty for thinking models, so result.end
1986+
// will be offset by generation_prompt.size() into effective_input space.
1987+
assert(!pp.generation_prompt.empty());
1988+
1989+
std::string bad_input =
1990+
"Thinking.\n"
1991+
"</think>"
1992+
"<tool_call>get_weather"
1993+
"<arg_key>city</arg_key><arg_value>Tokyo</arg_value>"
1994+
"</tool_call>\n";
1995+
1996+
bool got_runtime_error = false;
1997+
bool got_out_of_range = false;
1998+
std::string error_msg;
1999+
try {
2000+
common_chat_peg_parse(arena, bad_input, /*is_partial=*/false, pp);
2001+
} catch (const std::out_of_range & e) {
2002+
got_out_of_range = true;
2003+
error_msg = e.what();
2004+
} catch (const std::runtime_error & e) {
2005+
got_runtime_error = true;
2006+
error_msg = e.what();
2007+
}
2008+
GGML_ASSERT(!got_out_of_range && "throw path crashed with out_of_range (input.substr in effective_input space)");
2009+
GGML_ASSERT(got_runtime_error && "throw path should produce std::runtime_error with parse position");
2010+
}
2011+
19572012
// Kimi-K2-Thinking tests - custom parser
19582013
// Unique feature: tool call ID embeds function name as functions.<name>:<counter>
19592014
{

0 commit comments

Comments
 (0)