Skip to content

Commit 484d2f3

Browse files
authored
bug-fix: snprintf prints NULL in place of the last character (#10419)
* bug-fix: snprintf prints NULL in place of the last character We need to give snprintf enough space to print the last character and the null character, thus we allocate one extra byte and then ignore it when converting to std::string. * add comment about extra null-term byte requirement
1 parent 4b4d92b commit 484d2f3

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

examples/server/utils.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static std::string llama_get_chat_template(const struct llama_model * model) {
333333
if (res < 2) {
334334
return "";
335335
} else {
336-
std::vector<char> model_template(res, 0);
336+
std::vector<char> model_template(res + 1, 0);
337337
llama_model_meta_val_str(model, template_key.c_str(), model_template.data(), model_template.size());
338338
return std::string(model_template.data(), model_template.size() - 1);
339339
}

include/llama.h

+1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ extern "C" {
456456
// Functions to access the model's GGUF metadata scalar values
457457
// - The functions return the length of the string on success, or -1 on failure
458458
// - The output string is always null-terminated and cleared on failure
459+
// - When retrieving a string, an extra byte must be allocated to account for the null terminator
459460
// - GGUF array values are not supported by these functions
460461

461462
// Get metadata value as a string by key name

0 commit comments

Comments
 (0)