This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/35279] New: incorrect "array subscript is above array bounds" warning in loop at -O3


With -O3, I get an apparently incorrect "array subscript is above array bounds"
warning when a multidimensional array is passed to a function and accessed in a
loop:

$ /home/tmp/u/rjpeters/gcc-svn-install/bin/gcc -v
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: /home/tmp/u/rjpeters/gcc-svn/configure
--enable-languages=c,c++ --enable-checking=yes --enable-__cxa_atexit
--with-mpfr=/home/tmp/u/rjpeters/mpfr-2.2.1-install
--with-gmp=/home/tmp/u/rjpeters/gmp-4.2.1-install
--prefix=/home/tmp/u/rjpeters/gcc-svn-install
Thread model: posix
gcc version 4.4.0 20080220 (experimental) [trunk revision 129827] (GCC)

$ cat bug2.c
#include <stdio.h>

static void test1(char f[][64], const int n)
{
  int i;
  for (i = 0; i < n; ++i)
    printf("%s", f[i]); /* warning here */
  printf("\n");
}

static void test2(char f[][64])
{
  int i;
  for (i = 0; i < 4; ++i)
    printf("%s", f[i]); /* warning here */
  printf("\n");
}

static void test3(char f[][64])
{
  printf("%s", f[0]);
  printf("%s", f[1]);
  printf("%s", f[2]);
  printf("%s", f[3]); /* NOTE: no warning here */
  printf("\n");
}

int main()
{
  char f[4][64] = { "a", "b", "c", "d" };

  test1(f, 4);
  test2(f);
  test3(f);

  return 0;
}

$ /home/tmp/u/rjpeters/gcc-svn-install/bin/gcc -Wall -O3 bug2.c
bug2.c: In function ?main?:
bug2.c:7: warning: array subscript is above array bounds
bug2.c:7: warning: array subscript is above array bounds
bug2.c:15: warning: array subscript is above array bounds
bug2.c:15: warning: array subscript is above array bounds

$ ./a.out
abcd
abcd
abcd

The fact that there are two warnings each at lines 7 and 14 suggests that
somehow the last two iterations of the unrolled loop are considered as
out-of-bounds; however, the generated code is apparently correct, at least
judging by the runtime output of the program.

I don't see these warnings with "gcc version 4.1.1 20060724 (prerelease)
(4.1.1-3mdk)".

Thanks,
Rob


-- 
           Summary: incorrect "array subscript is above array bounds"
                    warning in loop at -O3
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rjpeters at klab dot caltech dot edu
 GCC build triplet: x86_64-unknown-linux-gnu
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]