Bug 67077

Summary: [6 Regression] Incorrect "array subscript is above array bounds" warning with -O2
Product: gcc Reporter: Vincent Lefèvre <vincent-gcc>
Component: tree-optimizationAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P3    
Version: 6.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Vincent Lefèvre 2015-07-31 12:59:10 UTC
With Debian's gcc-snapshot 20150722-1 on the following program, I get an incorrect warning "array subscript is above array bounds".

unsigned char buffer[8];
unsigned long foo (void)
{
  unsigned long i;
  i = buffer[0];
  if (i >= 8)
    return i - 7;
  i++;
  while (i > 8)
    {
      if (buffer[i-1] != 0)
        return 0;
      i--;
    }
  return 1;
}

zira:~> gcc-snapshot -Wall -O2 -c tst.c
tst.c: In function 'foo':
tst.c:11:17: warning: array subscript is above array bounds [-Warray-bounds]
       if (buffer[i-1] != 0)
                 ^

I don't get this warning if I use -O1 or gcc-snapshot 20150701-1 (thus this problem is very recent) or GCC 5.1.1 (gcc-5 Debian package).

Notes:
* There are other bugs concerning this warning, but they are older, while here this is a recent regression.
* The above code has been obtained by simplifying MPFR's mpfr_fpif_read_precision_from_file function in fpif.c (trunk).
Comment 1 Richard Biener 2015-08-03 10:32:34 UTC
I believe this has been fixed with

2015-07-23  Richard Biener  <rguenther@suse.de>

        PR middle-end/66916
        * match.pd: Guard widen and sign-change comparison simplification
        with single_use.
Comment 2 hjl@gcc.gnu.org 2015-08-03 16:58:34 UTC
Author: hjl
Date: Mon Aug  3 16:58:03 2015
New Revision: 226518

URL: https://gcc.gnu.org/viewcvs?rev=226518&root=gcc&view=rev
Log:
Add a testcase for PR tree-optimization/67077

	PR tree-optimization/67077
	* gcc.dg/pr67077.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr67077.c
Modified:
    trunk/gcc/testsuite/ChangeLog