This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: gcc 4.8.1: -O2 warnings one element off
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Jens Bauer <jens-lists at gpio dot dk>
- Cc: gcc at gcc dot gnu dot org
- Date: Sat, 1 Jun 2013 23:19:45 +0200
- Subject: Re: gcc 4.8.1: -O2 warnings one element off
- References: <20130601230821231127 dot ebd41391 at gpio dot dk>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Sat, Jun 01, 2013 at 11:08:21PM +0200, Jens Bauer wrote:
> Here's my result:
>
> ---8<-----8<-----8<-----
> elementTest.c: In function 'main':
> elementTest.c:27:19: warning: iteration 8u invokes undefined behavior [-Waggressive-loop-optimizations]
> eightMembers[i] = 0;
> ^
> elementTest.c:25:2: note: containing loop
> for(i = 0; i < 10; i++)
> ^
> --->8----->8----->8-----
>
> I would expect gcc to complain when it meets the second loop as well as the third loop, but it didn't detect that there is something wrong with the second loop.
>
> ...Is this a bug ?
It is not a bug, the warning isn't guaranteed to report all such cases of
undefined behavior, and due to lack of infrastructure in GCC 4.8 can't be
reported if certain passes discover the undefined behavior.
GCC 4.9 warns on both of the bad loops, but even in 4.9, if the loop
doesn't have constant bounds or has multiple exits etc., GCC will not warn
and just might optimize based on the fact that undefined behavior doesn't
happen in correct code. Lack of warning doesn't mean code is bug free.
Jakub