-
Notifications
You must be signed in to change notification settings - Fork 9.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug-fix: snprintf prints NULL in place of the last character #10419
base: master
Are you sure you want to change the base?
Conversation
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.
@slaren The return value of In anyway, I think we should leave a comment in |
Edited: never mind, this is C code.. Added a comment. I think a cleaner approach might be to have this allocate and return a char* which is then strlen()'d instead. Chat templates are not enormous enough that the extra length check will pose a noticeable impact. |
It's up to the user. They do not need to use NUL-terminated strings. |
|
Should we also apply this patch to every places where |
I looked and it doesn't seem like it is affected since the allocated buffer is basically guaranteed to be bigger than the chat template (including NULL term). |
Sorry I mean changing all other places using |
Right -- the only other place is in server.cpp specifically in llama.cpp/examples/server/server.cpp Lines 663 to 674 in a5e4759
which also preallocates a big enough buffer, like |
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.Because of this, when copying the C string into the
std::string
, we get a\u0000
at the end of it, which can cause issues.