This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PR fortran/19269 (partial fix): Lengths of reshaped arrays

PR fortran/19269 is about a segmentation fault on:

    program main
      character(len=1), dimension(2,2) :: a,b
      a = transpose(reshape((/'a', 'b', 'c', 'd'/), shape(a)))
      print *,a
    end program main

The location of the segfault has changed since the PR was originally
filed (at least it has with my recent patches applied).  To quote from
the PR notes, there are now two separate problems:

    First, gfc_simplify_reshape doesn't set the result's typespec
    correctly (it doesn't include a character length).  Second, we're
    passing character lengths to the transpose library function, which
    only wants array descriptors.

The attached patch fixes the first bug.  The problem was pretty simple:
gfc_simplify_reshape is using the typespec of the first element of the
constructor.  This is wrong for characters because (a) the first element
might be a constant character and hence have no gfc_charlen and (b) the
character length of the array is the maximum of every individual element.
We should be using the original array's typespec instead.

Bootstrapped & regression tested on i686-pc-linux-gnu.  OK to install?

Has the second problem already been discussed?  If so, what was the
outcome?  I suppose it could be fixed by suppressing the length
arguments or by creating special character versions of the library


	PR fortran/19269
	* simplify.c (gfc_simplify_transpose): Set the result's typespec from
	the source, not the first element of the return value.

	PR fortran/19269
	* gfortran.fortran-torture/execute/pr19269-1.c: New test.

--- gcc/fortran/simplify.c	2005-07-10 07:28:06.000000000 +0100
+++ gcc/fortran/simplify.c	2005-09-06 16:29:15.000000000 +0100
@@ -2858,7 +2858,7 @@ inc:
   for (i = 0; i < rank; i++)
     mpz_init_set_ui (e->shape[i], shape[i]);
-  e->ts = head->expr->ts;
+  e->ts = source->ts;
   e->rank = rank;
   return e;
diff -c /dev/null gcc/testsuite/gfortran.fortran-torture/execute/pr19269-1.f90
*** /dev/null	2005-06-16 22:49:09.000000000 +0100
--- gcc/testsuite/gfortran.fortran-torture/execute/pr19269-1.f90	2005-09-06 17:04:28.000000000 +0100
*** 0 ****
--- 1,16 ----
+ program main
+   call test (reshape ((/ 'a', 'bee', 'cedar', 'dog' /), (/ 2, 2 /)))
+ contains
+   subroutine test (a)
+     character (len = *), dimension (:, :) :: a
+     if (size (a, 1) .ne. 2) call abort
+     if (size (a, 2) .ne. 2) call abort
+     if (len (a) .ne. 5) call abort
+     if (a (1, 1) .ne. 'a') call abort
+     if (a (2, 1) .ne. 'bee') call abort
+     if (a (1, 2) .ne. 'cedar') call abort
+     if (a (2, 2) .ne. 'dog') call abort
+   end subroutine test
+ end program main

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]