Bug 35279 - incorrect "array subscript is above array bounds" warning in loop at -O3
Summary: incorrect "array subscript is above array bounds" warning in loop at -O3
Status: RESOLVED DUPLICATE of bug 37861
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-02-21 19:45 UTC by Rob Peters
Modified: 2008-11-14 23:59 UTC (History)
4 users (show)

See Also:
Host: x86_64-unknown-linux-gnu
Target: x86_64-unknown-linux-gnu
Build: x86_64-unknown-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments
test case; compile with "-Wall -O3" (245 bytes, text/plain)
2008-02-21 19:47 UTC, Rob Peters
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Rob Peters 2008-02-21 19:45:01 UTC
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
Comment 1 Rob Peters 2008-02-21 19:47:24 UTC
Created attachment 15197 [details]
test case; compile with "-Wall -O3"
Comment 2 Andrew Pinski 2008-11-14 23:59:57 UTC

*** This bug has been marked as a duplicate of 37861 ***