littlekernel / lk

LK embedded kernel
MIT License
3.2k stars 631 forks source link

snprintf behaviour different from the standard #28

Open jaybh opened 10 years ago

jaybh commented 10 years ago

Precision modifier when used with %s, should not pad the result with spaces. However, it does in the little kernel library.

char *temp  = “hello”;
char result[20];
snprintf (result, sizeof(result), “%.10s”, temp); // Notice the dot after the % sign
printf(“%s\n”, result);
Expected output

hello

Observed output

     hello

In essense, snprintf is padding spaces when it should not be with a precision modifier used with strings. At least, it doesn’t on my Ubuntu 12.04 x86_64 system, and also shouldn’t according to the man page.

travisg commented 10 years ago

Yeah, it doesn't handle . precision specifiers. It's something I've been meaning to work on, thanks for reminding me.