This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Odd warning: array subscript is above array bounds
- From: Jeff Law <law at redhat dot com>
- To: Mason <slash dot tmp at free dot fr>, GCC help <gcc-help at gcc dot gnu dot org>
- Date: Thu, 12 Oct 2017 10:42:53 -0600
- Subject: Re: Odd warning: array subscript is above array bounds
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=law at redhat dot com
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 01696723A6
- References: <c03b3983-3d0c-2f65-41da-e66b6f446cef@free.fr>
On 10/12/2017 10:16 AM, Mason wrote:
> Hello,
>
> Consider the following testcase:
>
> extern int n, xyz, array[8];
> void foo(void)
> {
> int i, j, Dupe;
>
> for (i = 0; i < n; i ++) {
> Dupe = 0;
> for (j = 0; j < i; j ++)
> if (array[j] == array[i])
> Dupe = 1;
> if (array[i] && !Dupe)
> xyz = 42;
> }
> }
>
> $ gcc-7.2 -Wall -O3 -S testcase.c
> testcase.c: In function 'foo':
> testcase.c:11:12: warning: array subscript is above array bounds [-Warray-bounds]
> if (array[i] && !Dupe)
> ~~~~~^~~
>
> $ gcc-6.3 -Wall -O3 -S testcase.c
> /* NO WARNING */
>
> I'm not sure how/why GCC concludes out-of-bounds access?
> Could it be a false positive?
-O3 turns on loop unrolling which often triggers false positives for the
out-of-bounds array index checks. I believe there's a number of bugs in
the BZ database WRT this issue.
You might consider attaching your testcase to one of them.
jeff