View Bug Activity | Format For Printing
With the following testcase, it happens while it shouldn't, according to the manual: -----8<------- #include <stdio.h> #include <locale.h> #define STR "查木马杀病毒,周鸿祎您的360不专业!" int main(void) { char buf[200]; setlocale(LC_ALL, ""); printf("%d\n", snprintf(buf, 150, "%.50s", STR)); return 0; } ----->8------- The manual page has this to say: About precision: An optional precision, in the form of a period (‘.’) followed by an optional decimal digit string.(...) This gives (...) the maximum number of characters to be printed from a string for s and S conversions. About s: If no l modifier is present: The const char * argument is expected to be a pointer to an array of character type(...) If an l modifier is present: The const wchar_t * argument is expected to be a pointer to an array of wide characters. Wide characters from the array are converted to multibyte characters (...) There is no "l" modifier, but still, the string goes through the multibyte conversion code, and fails because the string is invalid multibyte. Note, it only works with non UTF-8 locale set in LC_CTYPE or LC_ALL. This is debian bug http://bugs.debian.org/208308
Err the title is bogus, the thing is that sprintf returns -1 bogusly, if you run the previous testcase using LC_ALL=ja_JP.EUC-JP e.g. %s should not care about multibyte at all