[Patch, Newlib-nano]Improve printf to support non nul-terminated string

Terry Guo terry.guo@arm.com
Sat Nov 1 03:16:00 GMT 2014

Hi there,

When use printf to print string that isn't nul-terminated like below:

char cp[16];
printf("%.*s", sizeof(cp), moo);

current printf implementation in newlib-nano has a subtle bug that is the
call to strlen(cp). Suppose all 16 elements in cp are non-nul characters and
the storage next to cp is filled with value 0xFF (which is quite common for
flash of an embedded device), the strlen function will travel the memory
until trigger an error.

The Newlib implementation already realized this issue. The attached patch
intends to reuse Newlib implementation here for Newlib-nano to avoid such


2014-11-01  Terry Guo  <terry.guo@arm.com>

     * libc/stdio/nano-vfprintf_i.c (_printf_i): Use Newlib approach to
     handle string that might be not nul-terminated.
     * testsuite/newlib.stdio/nulprintf.c: New test.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: nano-non-nul-terminated-v3.txt
URL: <http://sourceware.org/pipermail/newlib/attachments/20141101/84e7a2a7/attachment.txt>

More information about the Newlib mailing list