]> git.ipfire.org Git - thirdparty/man-pages.git/commit - man3/printf.3
printf.3: Source and destination buffers may not overlap for *s*printf()
authorMichael Kerrisk <mtk.manpages@gmail.com>
Fri, 19 Dec 2008 16:47:04 +0000 (11:47 -0500)
committerMichael Kerrisk <mtk.manpages@gmail.com>
Fri, 19 Dec 2008 16:47:04 +0000 (11:47 -0500)
commit08f8825735b400df702fa9bf22e43710fd82e225
treeb1b9ffa85cedaae5e396704f7ed57f1329701f95
parent4209865fa41e9bde75cb9b90949cb448c02b8e51
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>
man3/printf.3