https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121805

kargls at comcast dot net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kargls at comcast dot net

--- Comment #3 from kargls at comcast dot net ---
Here's a slightly shorter testcase.  It shows what I think is an off-by-one
issue.

program pointer_array_to_struct

   implicit none

   type str
      integer data
      integer(1) a
   end type  

   type(str), target :: e(8)

   integer i, array_sum
   integer, pointer :: dd(:)
   integer idx(8)

   idx = [(i,i=1,8)]
   e%data = idx
   dd=>e%data
   array_sum = sum(idx(dd))

   if (array_sum /= 36) stop 1

end program

If I compiler with -fbounds-check -fdump-tree-original.  One sees that idx is
correctly filled, but the array indexing is messed up.  Here's the relevant
part with long lines wrapped.

    {
      integer(kind=4) val.2;
      integer(kind=8) D.4692;

      val.2 = 0;
      D.4692 = dd.dim[0].ubound - dd.dim[0].lbound;
      {
        integer(kind=8) S.3;

        S.3 = 0;
        while (1)
          {
            if (S.3 > D.4692) goto L.2;
            {
              integer(kind=4) D.4694;
              integer(kind=8) D.4695;

              D.4694 = *(integer(kind=4) *) (dd.data + (sizetype)
((dd.dim[0].stride *
                        NON_LVALUE_EXPR <S.3>) * 4));
              D.4695 = (integer(kind=8)) D.4694;

I think the above should be D.4695 = (integer(kind=8)) D.4694 + 1.

              if (D.4695 <= 0)
                {
                  _gfortran_runtime_error_at (&"At line 19 of file
gg.f90"[1]{lb: 1 sz: 1},
                     &"Index \'%ld\' of dimension 1 of array \'idx\' outside of
expected
                     range (%ld:%ld)"[1]{lb: 1 sz: 1}, D.4695, 1, 8);
                }
              if (D.4695 > 8)
                {
                  _gfortran_runtime_error_at (&"At line 19 of file
gg.f90"[1]{lb: 1 sz: 1},
                     &"Index \'%ld\' of dimension 1 of array \'idx\' outside of
expected
                     range (%ld:%ld)"[1]{lb: 1 sz: 1}, D.4695, 1, 8);
                }
              val.2 = idx[D.4695 + -1] + val.2;

Note, we are correctly subtracting 1 to get zero-index C semantics.

            }
            S.3 = S.3 + 1;
          }
        L.2:;
      }
      array_sum = val.2;
    }

Reply via email to