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

Reply via email to