public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/37130]  New: warning: array subscript is above array bounds.
@ 2008-08-15 16:53 gdsjaar at sandia dot gov
  2008-08-15 17:28 ` [Bug c++/37130] " paolo dot carlini at oracle dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: gdsjaar at sandia dot gov @ 2008-08-15 16:53 UTC (permalink / raw)
  To: gcc-bugs

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

Given:
#include <algorithm>

class Id {
public:
  Id();
  Id( const Id & );
  operator int() const;
  int id;
};

int get_int();

int main()
{
struct SideVertexBuffer {
  enum { max_vertices = 4 };
  Id node_id[ max_vertices ] ;
  int     processor ;

};

 SideVertexBuffer entry;
 int num_side_vert = get_int();
   std::sort(entry.node_id, entry.node_id+num_side_vert);
}

when compiled as:
g++ -finline-functions -O2 -Wall -c test.C

Gives the warning message:
/home/sntools/extras/compilers/gcc-4.3.1/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/../../../../include/c++/4.3.1/bits/stl_algo.h:In
function ‘int main()’:
/home/sntools/extras/compilers/gcc-4.3.1/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/../../../../include/c++/4.3.1/bits/stl_algo.h:1829:
warning: array subscript is above array bounds

We are required compile with -Werror, so this warning results in a build error
from our code.

As best I can tell, the error is due to _S_threshold being larger than the
dimensioned size of the array being passed to std::sort. Since the compiler is
inlining the functions, it "knows" that "__first+_S_threshold" is larger than
the dimensioned size of the array I pass to sort and emits the warning.

If I change the "num_side_vert" variable to an explicit value (num_side_vert =
4), then I don't get the warning since the compiler then knows that the one
branch of the if statement won't be taken in std::sort.


-- 
           Summary: warning: array subscript is above array bounds.
           Product: gcc
           Version: 4.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gdsjaar at sandia dot gov
 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=37130


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

* [Bug c++/37130] warning: array subscript is above array bounds.
  2008-08-15 16:53 [Bug c++/37130] New: warning: array subscript is above array bounds gdsjaar at sandia dot gov
@ 2008-08-15 17:28 ` paolo dot carlini at oracle dot com
  2008-08-16 22:56 ` [Bug middle-end/37130] " pinskia at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: paolo dot carlini at oracle dot com @ 2008-08-15 17:28 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from paolo dot carlini at oracle dot com  2008-08-15 17:26 -------
I don't see anything wrong with the code in __final_insertion_sort, thus this
is indeed, in my opinion, a bogus warning emitted by the compiler. Seems
related to PR 36902.


-- 


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


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

* [Bug middle-end/37130] warning: array subscript is above array bounds.
  2008-08-15 16:53 [Bug c++/37130] New: warning: array subscript is above array bounds gdsjaar at sandia dot gov
  2008-08-15 17:28 ` [Bug c++/37130] " paolo dot carlini at oracle dot com
@ 2008-08-16 22:56 ` pinskia at gcc dot gnu dot org
  2009-10-23  7:41 ` enrio at online dot no
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-08-16 22:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2008-08-16 22:54 -------
  __insertion_sort (&entry.node_id[0], &entry.node_id[16]);


The length is only 4.

One issue is that we don't reduce:
  D.8167 = &entry.node_id[0] + (unsigned int) num_side_vert * 4;
  if (&entry.node_id[0] != D.8167)

Into:
 if ((unsigned int)num_side_vert * 4 != 0)

So I think the other issue is that the code for std::sort assumes that it will
be a pointer and an array with a small size.

The warning is only partly bogus but since this is only runtime undefinedness,
this is valid code.  The function get_int better never return more than 4
really.


-- 


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


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

* [Bug middle-end/37130] warning: array subscript is above array bounds.
  2008-08-15 16:53 [Bug c++/37130] New: warning: array subscript is above array bounds gdsjaar at sandia dot gov
  2008-08-15 17:28 ` [Bug c++/37130] " paolo dot carlini at oracle dot com
  2008-08-16 22:56 ` [Bug middle-end/37130] " pinskia at gcc dot gnu dot org
@ 2009-10-23  7:41 ` enrio at online dot no
  2009-10-23 10:06 ` enrio at online dot no
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: enrio at online dot no @ 2009-10-23  7:41 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from enrio at online dot no  2009-10-23 07:41 -------
Created an attachment (id=18877)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=18877&action=view)
Code that warns, 50 lines, no #include. #ifdef for variations w/o warning


-- 


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


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

* [Bug middle-end/37130] warning: array subscript is above array bounds.
  2008-08-15 16:53 [Bug c++/37130] New: warning: array subscript is above array bounds gdsjaar at sandia dot gov
                   ` (2 preceding siblings ...)
  2009-10-23  7:41 ` enrio at online dot no
@ 2009-10-23 10:06 ` enrio at online dot no
  2010-01-02 19:30 ` rguenth at gcc dot gnu dot org
  2010-02-12 16:16 ` scott dot gccbugs dot 2009 at scottrix dot co dot uk
  5 siblings, 0 replies; 7+ messages in thread
From: enrio at online dot no @ 2009-10-23 10:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from enrio at online dot no  2009-10-23 10:06 -------
In my example code there is a pointer to a struct that has an array as the
first member. I appears that the compiler treats the pointer as a pointer to
this first member, and flags a loop that writes outside the first member
already at the first iteration. The code uses an integer offset from the
pointer, and the pointer itself is not changed.

If the members are reordered so that an unsigned char member becomes the first
one, the warning goes away. (But the example comes from a driver, and the
struct maps hardware registers, so it cannot be reordered at a whim.)

If the loop is modified so that it begins within the array, the warning goes
away, even the iterator has a constant end condition - outside the array.

If the loop is modified so that it begins exactly at the first byte after the
array, there are four warnings rather than three. It makes me wonder if the
compiler is generating code that does access the memory locations intended.

Since we got the strict aliasing optimizations, we probably should learn to
rewrite such old code using a union, or properly accessing the structure
members one by one.  I just tried the union approach: 

union mac_u {
  struct mac_regs regs;
  u8 bytes[sizeof(struct mac_regs)];
};

I changed the function argument to a pointer to this union rather than to a
struct mac_regs, initialized the local pointer variable from the 'bytes' union
member, and the warning went away.  This appears to be the way to properly tell
the compiler how one plans to use the pointer.

Also if the pointer is incremented to point directly at the bytes of interest
rather than being indexed, the warning goes away.


-- 

enrio at online dot no changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |enrio at online dot no


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


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

* [Bug middle-end/37130] warning: array subscript is above array bounds.
  2008-08-15 16:53 [Bug c++/37130] New: warning: array subscript is above array bounds gdsjaar at sandia dot gov
                   ` (3 preceding siblings ...)
  2009-10-23 10:06 ` enrio at online dot no
@ 2010-01-02 19:30 ` rguenth at gcc dot gnu dot org
  2010-02-12 16:16 ` scott dot gccbugs dot 2009 at scottrix dot co dot uk
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-01-02 19:30 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from rguenth at gcc dot gnu dot org  2010-01-02 19:30 -------
Works with 4.4 and 4.5.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
      Known to fail|                            |4.3.4
      Known to work|                            |4.4.2 4.5.0
         Resolution|                            |FIXED
   Target Milestone|---                         |4.4.2


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


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

* [Bug middle-end/37130] warning: array subscript is above array bounds.
  2008-08-15 16:53 [Bug c++/37130] New: warning: array subscript is above array bounds gdsjaar at sandia dot gov
                   ` (4 preceding siblings ...)
  2010-01-02 19:30 ` rguenth at gcc dot gnu dot org
@ 2010-02-12 16:16 ` scott dot gccbugs dot 2009 at scottrix dot co dot uk
  5 siblings, 0 replies; 7+ messages in thread
From: scott dot gccbugs dot 2009 at scottrix dot co dot uk @ 2010-02-12 16:16 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from scott dot gccbugs dot 2009 at scottrix dot co dot uk  2010-02-12 16:16 -------
I get this on 4.4.3 for x86 32bit, is there a patch, or will I have to wait for
4.5 to be released ?


-- 


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


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

end of thread, other threads:[~2010-02-12 16:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-15 16:53 [Bug c++/37130] New: warning: array subscript is above array bounds gdsjaar at sandia dot gov
2008-08-15 17:28 ` [Bug c++/37130] " paolo dot carlini at oracle dot com
2008-08-16 22:56 ` [Bug middle-end/37130] " pinskia at gcc dot gnu dot org
2009-10-23  7:41 ` enrio at online dot no
2009-10-23 10:06 ` enrio at online dot no
2010-01-02 19:30 ` rguenth at gcc dot gnu dot org
2010-02-12 16:16 ` scott dot gccbugs dot 2009 at scottrix dot co dot uk

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).