# gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1'
--with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3
--program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug
--enable-objc-gc --enable-mpfr --enable-targets=all --enable-cld
--enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu
--target=i486-linux-gnu
Thread model: posix
gcc version 4.3.2 (Debian 4.3.2-1) 


Bug 37861 looks related, but I'm not sure if the fix is the same; if so, this
can be marked as a duplicate.


Here is the code (foo.c):


unsigned short foobar;

int array[100];

void g(unsigned short j) {
  if (j < 100)
    array[j]++;
}

void f() {
  int i = foobar;
  if (i >= 100)
    g(foobar);
}


And here is the gcc call and output:

# gcc -O3 -Wall -c foo.c
foo.c: In function ‘f’:
foo.c:7: warning: array subscript is above array bounds
foo.c:7: warning: array subscript is above array bounds


Since j is unsigned and the array access only occurs for j < 100, this can
never be out of bounds. The warning message is also somewhat fragile; e.g. it
disappears with any of the following changes:

1. Declaring foobar as volatile.
2. Replacing "if (i >= 100)" by "if (foobar >= 100)"
3. Replacing either of the unsigned shorts by an int or short.
4. Removing the call to g.


-- 
           Summary: bogus warning with -O3 -Wall: "array subscript is above
                    array bounds"
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: helmert at informatik dot uni-freiburg dot de


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

  • [Bug c/38480] New: bogus wa... helmert at informatik dot uni-freiburg dot de

Reply via email to