]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
bug-fix: snprintf prints NULL in place of the last character (#10419)
authorkallewoof <redacted>
Wed, 11 Dec 2024 13:48:04 +0000 (22:48 +0900)
committerGitHub <redacted>
Wed, 11 Dec 2024 13:48:04 +0000 (14:48 +0100)
* 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

examples/server/utils.hpp
include/llama.h

index 8f545aea52dc495dae2ceb02bc533feda360eac0..2fcb895abe7e48efb7cd80afaff0bb0152766e68 100644 (file)
@@ -333,7 +333,7 @@ static std::string llama_get_chat_template(const struct llama_model * model) {
     if (res < 2) {
         return "";
     } else {
-        std::vector<char> model_template(res, 0);
+        std::vector<char> model_template(res + 1, 0);
         llama_model_meta_val_str(model, template_key.c_str(), model_template.data(), model_template.size());
         return std::string(model_template.data(), model_template.size() - 1);
     }
index 36945cde335dcd1c2970af241a047332900aeb97..eebbacb803454c9e8134c990342b90b42ccfd39d 100644 (file)
@@ -456,6 +456,7 @@ extern "C" {
     // Functions to access the model's GGUF metadata scalar values
     // - The functions return the length of the string on success, or -1 on failure
     // - The output string is always null-terminated and cleared on failure
+    // - When retrieving a string, an extra byte must be allocated to account for the null terminator
     // - GGUF array values are not supported by these functions
 
     // Get metadata value as a string by key name