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

            Bug ID: 60112
           Summary: bogus error: array subscript is above array bounds
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: david.abdurachmanov at gmail dot com

GCC pre-4.9.0 (r206686), no issues on 4.6.2, 4.7.2, and 4.8.1. I found that if
I mark `get_size' as inline or constexpr errors are gone. If I keep `nFaces'
uninitialized it also complains, but if I set it explicitly to some number
(e.g., 0,..,<max>) it's fine. Even commenting out such line (49) as

    lp += VV(tanDistToFace[iFDest]);

removes the errors.

I tried removing more mass from the test case, but it also removes the errors.

Doesn't work with -O2, but works fine with -O0, -O1, -O3, -Os, and -Og.

### COMPILE ###

c++ -c -O2 -std=c++11 -Werror=array-bounds -fdiagnostics-show-option -fPIC
test2.cpp

### ERRORS ###

test2.cpp: In function 'void abc()':
test2.cpp:43:37: error: array subscript is above array bounds
[-Werror=array-bounds]
     iFDestSorted[nDestSorted - i - 1] = iFDestSorted[iMax];
                                     ^
test2.cpp:44:22: error: array subscript is above array bounds
[-Werror=array-bounds]
     iFDestSorted[iMax] = iTmp;
                      ^
cc1plus: some warnings being treated as errors

### TEST CASE ###

struct GP {
  double mx, my, mz;
  GP (double x, double y, double z) : mx(x), my(y), mz(z) { };
};
typedef struct GP GP;

void do_something(const GP& n) { }

struct VV {
  double mx;
  VV (double x) : mx(x) { };
};
typedef struct VV VV;

struct PP {
  double mx;
  PP (double x) : mx(x) { };
  double x() { return mx; };
  PP& operator+=(const VV& rhs) { mx += rhs.mx; return *this; };
};
typedef struct PP PP;

int get_size() { return 0; };

void abc() {
  double tanDistToFace[6] = {0,0,0,0,0,0};
  unsigned int iFDestSorted[6] = {0,0,0,0,0,0};
  unsigned int nDestSorted = 0;

  unsigned int nFaces = get_size();
  for (unsigned int iFace = 0; iFace < nFaces; ++iFace) {
    nDestSorted++;
  }

  for (unsigned int i = 0; i < nDestSorted; ++i) {
    unsigned int iMax = nDestSorted - i - 1;
    for (unsigned int j=0; j < nDestSorted-i; ++j) {
      if (tanDistToFace[iFDestSorted[j]] > tanDistToFace[iFDestSorted[iMax]]) {
        iMax = j;
      }
    }
    unsigned int iTmp = iFDestSorted[nDestSorted - i - 1];
    iFDestSorted[nDestSorted - i - 1] = iFDestSorted[iMax];
    iFDestSorted[iMax] = iTmp;
  }

    int iFDest = iFDestSorted[0];
    PP lp(0);
    lp += VV(tanDistToFace[iFDest]);
    GP gp(lp.x(), lp.x(), lp.x());
    do_something(gp);
}

Reply via email to