public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/55892] New: Bogus compiler warning
@ 2013-01-07  6:50 sworddragon2 at aol dot com
  2013-01-07  7:35 ` [Bug c/55892] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: sworddragon2 at aol dot com @ 2013-01-07  6:50 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 55892
           Summary: Bogus compiler warning
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: sworddragon2@aol.com


Created attachment 29092
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29092
Example code

In the attachments is the example file for this bug report. Compiling it with
"gcc -O3 -Wall -Wextra -o /dev/null -pedantic test.c" will result in a warning:

In file included from /usr/include/stdio.h:937:0,
                 from test.c:2:
In function 'fgets',
    inlined from 'main' at test.c:17:11:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:261:2: warning: call to
'__fgets_chk_warn' declared with attribute warning: fgets called with bigger
size than length of destination buffer [enabled by default]


There are 2 potential errors on this behavior:

- If the warning triggers on line 33 it should also be triggered on all 4
lines.
- In this code the limit of fgets can never exceed a length of 8. The compiler
thinks because read_limit can be 2147483647 (INT32_MAX) limit in fgets can it
be too (which as already said can never happen in this code). Removing the 3
lines which assign INT32_MAX removes the compiler warning.


I have a suggestion for a solution:

- Like conditional return values the compiler could check if the code has a
fixed construct which makes it possible to calculate if the limit will be
exceeded (this would match on the example code and trigger no compiler
warnings). If the code has a dynamic construct which makes it not possible to
calculate if the limit would be exceeded a comiler warning is thrown.


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

* [Bug c/55892] Bogus compiler warning
  2013-01-07  6:50 [Bug c/55892] New: Bogus compiler warning sworddragon2 at aol dot com
@ 2013-01-07  7:35 ` pinskia at gcc dot gnu.org
  2013-01-07  8:01 ` sworddragon2 at aol dot com
  2015-08-12 10:59 ` mpolacek at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2013-01-07  7:35 UTC (permalink / raw)
  To: gcc-bugs


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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2013-01-07 07:34:25 UTC ---
First off you also need -D_FORTIFY_SOURCE and a newish glibc headers.  Second I
think this warning is correct.
Take a line which is 16 characters long.
The first time at beginning of the loop, limit: 8; readlimit: 8.

The next time, limit:0 readlimit: 0.
The next time limit: (unsigned long)(-8) readlimit: INT32_MAX
Warn because readlimit > 1024.

The reason why the first call to fgetsl does not warn is because it does not
get inlined as it is not hot.
The second is also the same reason as the first but also the code for fgets
cannot figure out the size of the variable, buffer.
The last has the same issue as second minus the inline issue.

The trunk gives a very good warning message of the point of the inline function
happens too:
In file included from /usr/include/stdio.h:910:0,
                 from t67.c:2:
In function ‘fgets’,
    inlined from ‘fgetsl.constprop.0’ at t67.c:17:11,
    inlined from ‘main’ at t67.c:33:14:
/usr/include/bits/stdio2.h:253:2: warning: call to ‘__fgets_chk_warn’ declared
with attribute warning: fgets called with bigger size than length of
destination buffer [enabled by default]
  return __fgets_chk_warn (__s, __bos (__s), __n, __stream);
  ^


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

* [Bug c/55892] Bogus compiler warning
  2013-01-07  6:50 [Bug c/55892] New: Bogus compiler warning sworddragon2 at aol dot com
  2013-01-07  7:35 ` [Bug c/55892] " pinskia at gcc dot gnu.org
@ 2013-01-07  8:01 ` sworddragon2 at aol dot com
  2015-08-12 10:59 ` mpolacek at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: sworddragon2 at aol dot com @ 2013-01-07  8:01 UTC (permalink / raw)
  To: gcc-bugs


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

sworddragon2 at aol dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |

--- Comment #2 from sworddragon2 at aol dot com 2013-01-07 08:01:36 UTC ---
> Take a line which is 16 characters long.
> The first time at beginning of the loop, limit: 8; readlimit: 8.
>
> The next time, limit:0 readlimit: 0.
> The next time limit: (unsigned long)(-8) readlimit: INT32_MAX
> Warn because readlimit > 1024.

You are right, limit -= read_limit; makes anyway no sense there so I removed it
(and the compiler wanring is gone too).

But there is still something strange on the old example: Why does line 33
trigger the warning but line 34 not? The only difference between them is that
line 33 uses a stack variable and line 34 a heap variable. Both have the same
size so shouldn't this warning trigger on both lines?


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

* [Bug c/55892] Bogus compiler warning
  2013-01-07  6:50 [Bug c/55892] New: Bogus compiler warning sworddragon2 at aol dot com
  2013-01-07  7:35 ` [Bug c/55892] " pinskia at gcc dot gnu.org
  2013-01-07  8:01 ` sworddragon2 at aol dot com
@ 2015-08-12 10:59 ` mpolacek at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-08-12 10:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55892

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |mpolacek at gcc dot gnu.org
         Resolution|---                         |FIXED

--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
With current trunk I get two warnings, on line 33 and on line 34.  I guess
fixed then.


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

end of thread, other threads:[~2015-08-12 10:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-07  6:50 [Bug c/55892] New: Bogus compiler warning sworddragon2 at aol dot com
2013-01-07  7:35 ` [Bug c/55892] " pinskia at gcc dot gnu.org
2013-01-07  8:01 ` sworddragon2 at aol dot com
2015-08-12 10:59 ` mpolacek at gcc dot gnu.org

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).