User account creation filtered due to spam.

Bug 34476 - Parameters: Bogus out of bounds error in array constructor
Summary: Parameters: Bogus out of bounds error in array constructor
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords: rejects-valid
Depends on:
Blocks: 32834
  Show dependency treegraph
 
Reported: 2007-12-15 09:14 UTC by Tobias Burnus
Modified: 2008-01-08 15:16 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-12-17 22:53:09


Attachments
A fix and testcase for this PR (931 bytes, patch)
2008-01-08 10:31 UTC, Paul Thomas
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2007-12-15 09:14:19 UTC
The following valid program is rejected with

   integer(1), parameter :: ARR1(len(HEX1)) = [( MSKa1(i), i=1,len(HEX1) )]
                                                      1
Error: index in dimension 1 is out of bounds at (1)

module abuse_mod
   implicit none
   integer i
   character(8), parameter :: HEX1 = '40490FDB'
   integer(1), parameter :: MSKa1(len(HEX1)) =  [(1,i=1,len(HEX1))]
   integer(1), parameter :: ARR1(len(HEX1)) = [( MSKa1(i), i=1,len(HEX1) )]
end module abuse_mod

For a longer example (which uses supported vendor extensions), see:
http://groups.google.com/group/comp.lang.fortran/msg/2499294cb9a33299
Comment 1 Paul Thomas 2007-12-17 05:41:40 UTC
4.2 gives a second error:

   integer(1), parameter :: ARR1(len(HEX1)) = [( MSKa1(i), i=1,len(HEX1) )]
                                                      1
Error: index in dimension 1 is out of bounds at (1)
pr34476.f90:5.44:

   integer(1), parameter :: MSKa1(len(HEX1)) =  [(1,i=1,len(HEX1))]
                                           1
Error: Parameter array 'mska1' at (1) cannot be automatic or assumed shape

Paul
Comment 2 Walter Spector 2007-12-17 15:33:07 UTC
Here is an additional variant of this bug:

program init_bug
  implicit none

  integer :: i
  character(11), parameter :: string="hello world"

! This compiles:
  character, parameter :: up_string(len (string)) =  &
    (/ (string(i:i), i=1, len (string)) /)

  integer, parameter :: bytes(len (string)) =  &
    (/ (iachar (string(i:i)), i=1, len (string)) /)

  integer, parameter :: up_bytes(len (string)) =  &
    (/ (iachar (up_string(i)), i=1, size (up_string)) /)

  print *, string
  print *, up_string

  print *, bytes
  print *, up_bytes

end program

Besides the out-of-bounds error, it also gives a 'cannot appear in the expression error':


    (/ (iachar (up_string(i)), i=1, size (up_string)) /)
                         1
Error: index in dimension 1 is out of bounds at (1)
abuse_init4.f90:12.25:

    (/ (iachar (string(i:i)), i=1, len (string)) /)
                        1
Error: Variable 'i' cannot appear in the expression at (1)
abuse_init4.f90:21.19:

  print *, up_bytes
                  1
Error: Symbol 'up_bytes' at (1) has no IMPLICIT type
Comment 3 Paul Thomas 2007-12-17 22:53:08 UTC
The first part of this PR is fixed with:

Index: gcc/fortran/expr.c
===================================================================
*** gcc/fortran/expr.c  (revision 130811)
--- gcc/fortran/expr.c  (working copy)
*************** find_array_element (gfc_constructor *con
*** 1026,1035 ****
        }

        /* Check the bounds.  */
!       if (ar->as->upper[i]
!         && (mpz_cmp (e->value.integer, ar->as->upper[i]->value.integer) > 0
!             || mpz_cmp (e->value.integer,
!                         ar->as->lower[i]->value.integer) < 0))
        {
          gfc_error ("index in dimension %d is out of bounds "
                     "at %L", i + 1, &ar->c_where[i]);
--- 1026,1039 ----
        }

        /* Check the bounds.  */
!       if ((ar->as->upper[i]
!            && ar->as->upper[i]->expr_type == EXPR_CONSTANT
!            && mpz_cmp (e->value.integer,
!                        ar->as->upper[i]->value.integer) > 0)
!               ||
!         (ar->as->lower[i]->expr_type == EXPR_CONSTANT
!            && mpz_cmp (e->value.integer,
!                        ar->as->lower[i]->value.integer) < 0))
        {
          gfc_error ("index in dimension %d is out of bounds "
                     "at %L", i + 1, &ar->c_where[i]);

The second is due to the gfc_error at expr.c(check_restricted):2495

There is some attribute that 'i' has which should make it an exception - I just do not quite see what it is right now.

Paul
Comment 4 Paul Thomas 2008-01-08 10:31:26 UTC
Created attachment 14898 [details]
A fix  and testcase for this PR

This is regtesting right now.

Paul
Comment 5 Paul Thomas 2008-01-08 15:15:17 UTC
Subject: Bug 34476

Author: pault
Date: Tue Jan  8 15:14:33 2008
New Revision: 131396

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=131396
Log:
2008-01-08  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/34476
	* expr.c (find_array_element): Check that the array bounds are
	constant before using them.  Use lower, as well as upper bound.
	(check_restricted): Allow implied index variable.

2008-01-08  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/34476
	* gfortran.dg/parameter_array_init_3.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/parameter_array_init_3.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/expr.c
    trunk/gcc/testsuite/ChangeLog

Comment 6 Paul Thomas 2008-01-08 15:16:42 UTC
Fixed on trunk

Paul