printf.3: Source and destination buffers may not overlap for *s*printf()
http://sourceware.org/bugzilla/show_bug.cgi?id=7075
Some existing code relies on techniques like the following to
append text to a buffer:
$ cat s.c
#include <stdio.h>
char buf[80] = "not ";
main()
{
sprintf(buf, "%sfail", buf);
puts(buf);
return 0;
}
$ cc s.c
$ ./a.out
not fail
However, the standards say the results are undefined if source
and destination buffers overlap, and with suitable compiler
options, recent changes can cause unexpected results:
$ cc -v 2>&1 | grep gcc
gcc version 4.3.1
20080507 (prerelease) [gcc-4_3-branch revision 135036] (SUSE Linux)
$ cc -D_FORTIFY_SOURCE -O2 s.c
$ ./a.out
fail
Reported-by: Petr Baudis <pasky@suse.cz>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>