public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/54582] New: gap in FORTIFY checking of buffer lengths
@ 2012-09-14 19:48 dcb314 at hotmail dot com
  2012-09-17  8:51 ` [Bug c/54582] " rguenth at gcc dot gnu.org
                   ` (14 more replies)
  0 siblings, 15 replies; 16+ messages in thread
From: dcb314 at hotmail dot com @ 2012-09-14 19:48 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54582

             Bug #: 54582
           Summary: gap in FORTIFY checking of buffer lengths
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: dcb314@hotmail.com


Consider the following code

# include <stdio.h>

void f(int n)
{
    char buf[2];

    sprintf(buf, "ab%d", n);
    printf("%s\n", buf);

    sprintf(buf, "ab");
    printf("%s\n", buf);
}

int
main()
{
    f(2);

    return 0;
}

compiled as

[dcb@zippy Alphasrc]$ ~/gcc/trunk/results/bin/gcc -g -O2 -Wall
-D_FORTIFY_SOURCE=2 -c sep14a.c
In file included from /usr/include/stdio.h:936:0,
                 from sep14a.c:2:
In function ‘sprintf’,
    inlined from ‘f’ at sep14a.c:11:9:
/usr/include/bits/stdio2.h:34:3: warning: call to __builtin___sprintf_chk will
always overflow destination buffer [enabled by default]
   return __builtin___sprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
   ^

so gcc can find the problem in the 2nd sprintf, but not the first.

All the numeric specifiers (%d, %u etc) all produce at least one 
character, so gcc could take account of this in checking buffer lengths.

Here is cppcheck finding the problem

Checking sep14a.c...
[sep14a.c:8]: (error) Buffer is accessed out of bounds.
[sep14a.c:11]: (error) Buffer is accessed out of bounds.


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2013-03-08  8:49 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-14 19:48 [Bug c/54582] New: gap in FORTIFY checking of buffer lengths dcb314 at hotmail dot com
2012-09-17  8:51 ` [Bug c/54582] " rguenth at gcc dot gnu.org
2013-02-06 10:58 ` dcb314 at hotmail dot com
2013-02-06 12:18 ` [Bug middle-end/54582] " rguenth at gcc dot gnu.org
2013-02-06 12:21 ` rguenth at gcc dot gnu.org
2013-02-06 12:41 ` jakub at gcc dot gnu.org
2013-02-06 13:14 ` manu at gcc dot gnu.org
2013-02-06 13:26 ` jakub at gcc dot gnu.org
2013-02-06 13:40 ` manu at gcc dot gnu.org
2013-02-06 13:47 ` jakub at gcc dot gnu.org
2013-02-06 14:29 ` fweimer at redhat dot com
2013-02-06 18:48 ` dcb314 at hotmail dot com
2013-02-07 21:19 ` dcb314 at hotmail dot com
2013-02-07 21:22 ` dcb314 at hotmail dot com
2013-02-14 19:07 ` dcb314 at hotmail dot com
2013-03-08  8:49 ` dcb314 at hotmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).