public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/45180]  New: bogus warning: array subscript is above array bounds
@ 2010-08-04 12:58 joachim dot reichel at gmx dot de
  2010-08-04 13:10 ` [Bug tree-optimization/45180] " rguenth at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: joachim dot reichel at gmx dot de @ 2010-08-04 12:58 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2502 bytes --]

Please see 43949 which was about a very similar test case.

$ cat test.cpp
void f();

int c[3];
int result;

struct Vector {
    static int get(int i) {
        if (i >= 3)
            f();
        return c[i];
    }
};

void g(int index)
{
    result = Vector::get(index) + Vector::get(index);
}

$ g++ -Wall -c test.cpp
[no warnings]

$ g++ -Wall -c -O2 test.cpp
test.cpp: In function ‘void g()’:
test.cpp:10:19: error: array subscript is above array bounds

$ g++ -v
Using built-in specs.
COLLECT_GCC=/prefix/gcc-4.5.0/x86_64-unknown-linux-gnu/bin/g++
COLLECT_LTO_WRAPPER=/prefix/gcc-4.5.0/x86_64-unknown-linux-gnu/bin/../libexec/gcc/x86_64-unknown-linux-gnu/4.5.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/prefix/gcc-4.5.0
--exec-prefix=/prefix/gcc-4.5.0/x86_64-unknown-linux-gnu
--enable-version-specific-runtime-libs --enable-stage1-checking --disable-nls
--with-system-zlib --enable-multilib --enable-languages=c,c++,objc
--with-gmp-include=/prefix/gmp-4.2.2/x86_64-unknown-linux-gnu/include
--with-gmp-lib=/prefix/gmp-4.2.2/x86_64-unknown-linux-gnu/lib
--with-mpfr-include=/prefix/mpfr-2.4.2/include
--with-mpfr-lib=/prefix/mpfr-2.4.2/x86_64-unknown-linux-gnu/lib
--with-mpc-include=/prefix/mpc-0.8.1/include
--with-mpc-lib=/prefix/mpc-0.8.1/x86_64-unknown-linux-gnu/lib --with-gnu-as
--with-as=/prefix/binutils-2.20.1/x86_64-unknown-linux-gnu/bin/as --with-gnu-ld
--with-ld=/prefix/binutils-2.20.1/x86_64-unknown-linux-gnu/bin/ld
Thread model: posix
gcc version 4.5.0 (GCC)

The bogus warning disappears if
- Vector::get(index) is assigned to result (no addition)
- get() is a global function

The bogus warning does not appear in gcc 4.2.4 (Debian 4.2.4-6). It appears
also in 4.3.2 (Debian 4.3.2-1.1), 4.4.4 (Debian 4.4.4-7) and 4.5.0 (GCC).

Note that in contrast to PR43949 in this case the array subscript *could* be
above array bounds depending on how g() is called, but in the code snippet
itself there is no such call.


-- 
           Summary: bogus warning: array subscript is above array bounds
           Product: gcc
           Version: 4.5.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: joachim dot reichel at gmx dot de
 GCC build triplet: x86_64-linux-gnu
  GCC host triplet: x86_64-linux-gnu
GCC target triplet: x86_64-linux-gnu


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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug tree-optimization/45180] bogus warning: array subscript is above array bounds
  2010-08-04 12:58 [Bug tree-optimization/45180] New: bogus warning: array subscript is above array bounds joachim dot reichel at gmx dot de
@ 2010-08-04 13:10 ` rguenth at gcc dot gnu dot org
  2010-08-04 15:01 ` joachim dot reichel at gmx dot de
  2010-08-04 15:45 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-08-04 13:10 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2010-08-04 13:09 -------
The reasoning of GCC goes as follows.  There is a partial redundancy
along the two invocations of get(), as c[i] is possibly clobbered by f().
So we transform g() to

  if (i >= 3)
     f();
  tem1 = c[i];
  if (i >= 3)
    {
    f();
    tem2 = c[i];
   }
  else tem2 = tem1;
  return tem1 + tem2;

now you can see that the load to tem2 is _always_ accessing c with
an out-of-bound index.

There is no code in the warning machinery to restrict the "is above"
warning to code regions that are always executed.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
           Keywords|                            |diagnostic
   Last reconfirmed|0000-00-00 00:00:00         |2010-08-04 13:09:50
               date|                            |


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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug tree-optimization/45180] bogus warning: array subscript is above array bounds
  2010-08-04 12:58 [Bug tree-optimization/45180] New: bogus warning: array subscript is above array bounds joachim dot reichel at gmx dot de
  2010-08-04 13:10 ` [Bug tree-optimization/45180] " rguenth at gcc dot gnu dot org
@ 2010-08-04 15:01 ` joachim dot reichel at gmx dot de
  2010-08-04 15:45 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: joachim dot reichel at gmx dot de @ 2010-08-04 15:01 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from joachim dot reichel at gmx dot de  2010-08-04 15:00 -------
Ok, I see. But that seems a bit unfortunate. Isn't there a great deal of such
code? Just think of some vector class: c would be a class member, get()
non-static and "if...f()" is an assert-like statement (that might return or
not).

void f();

int result;

struct Vector {
    int c[3];
    int get(int i) const {
        if (i >= 3)
            f();
        return c[i];
    }
};

void g(const Vector& x, int index)
{
    result = x.get(index) + x.get(index);
}

--

In the original test case if I make get() a global function, then there is no
warning. Is the code then transformed differently?

And if I replace the argument in the second call of get() by index+1 I also
don't get the warning. I guess that's because the load into tem2 gets moved out
of the if-block then?

Just trying to understand how common the problem is ...


-- 


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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug tree-optimization/45180] bogus warning: array subscript is above array bounds
  2010-08-04 12:58 [Bug tree-optimization/45180] New: bogus warning: array subscript is above array bounds joachim dot reichel at gmx dot de
  2010-08-04 13:10 ` [Bug tree-optimization/45180] " rguenth at gcc dot gnu dot org
  2010-08-04 15:01 ` joachim dot reichel at gmx dot de
@ 2010-08-04 15:45 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-08-04 15:45 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2010-08-04 15:44 -------
Since the compiler does not know that f() will never return, it is hard problem
to solve.  If you mark f with the attribute noreturn, the warning will
disappear.


-- 


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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-08-04 15:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-04 12:58 [Bug tree-optimization/45180] New: bogus warning: array subscript is above array bounds joachim dot reichel at gmx dot de
2010-08-04 13:10 ` [Bug tree-optimization/45180] " rguenth at gcc dot gnu dot org
2010-08-04 15:01 ` joachim dot reichel at gmx dot de
2010-08-04 15:45 ` pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).