[Patch, fortran] PR30190 - Bounds check generates wrong code for associated(p1(i)%p2,p1(i)%p2)

Paul Richard Thomas paul.richard.thomas@gmail.com
Wed Dec 20 14:57:00 GMT 2006


:ADDPATCH fortran:

This is a salutory lesson as to when and how to fix an expression
using gfc_evaluate_now.  The offending line in trans-array.c
(gfc_conv_array_ref) assigned the gfc_evaluate_now to the tree
variable that was its first argument, without adding the resulting
expression to a block.  The result was that the declarations were
added to se->pre but all the assignments were lost except the last.
Unfortunately, the all the index variables that were declared were
subsequently used and a segfault occurred.

The proposed fix is highly economical; just remove the line that fixes
the expression!  This inserts the expression for the index in the
array reference, rather than a temporary, into the tree expression for
the overall index.  In the case where the fortran index is 'i', the
resulting code is marginally more readable.  The testcase is that of
the reporter, except that the PRINT statement is replaced by an
assignment to a logical variable.

Regtested on Cygwin_NT/PIV - OK for trunk and 4.2?

Paul
-------------- next part --------------
2006-12-20  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/30190
	* trans-array.c (gfc_conv_array_ref): Remove gfc_evaluate_now
	from the -fbounds-check branch.

2006-12-20  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/30190
	* gfortran.dg/bounds_check_5.f90: New test.

-------------- next part --------------
Index: gcc/fortran/trans-array.c
===================================================================
*** gcc/fortran/trans-array.c	(r?vision 120071)
--- gcc/fortran/trans-array.c	(copie de travail)
*************** gfc_conv_array_ref (gfc_se * se, gfc_arr
*** 2094,2101 ****
  	  tree cond;
  	  char *msg;
  
- 	  indexse.expr = gfc_evaluate_now (indexse.expr, &se->pre);
- 
  	  tmp = gfc_conv_array_lbound (se->expr, n);
  	  cond = fold_build2 (LT_EXPR, boolean_type_node, 
  			      indexse.expr, tmp);
--- 2094,2099 ----
Index: gcc/testsuite/gfortran.dg/bounds_check_5.f90
===================================================================
*** gcc/testsuite/gfortran.dg/bounds_check_5.f90	(r?vision 0)
--- gcc/testsuite/gfortran.dg/bounds_check_5.f90	(r?vision 0)
***************
*** 0 ****
--- 1,17 ----
+ ! { dg-do run }
+ ! This tests the fix for PR30190, in which the array reference
+ ! in the associated statement would cause a segfault.
+ !
+ ! Contributed by Tobias Burnus  <burnus@gcc.gnu.org>
+ !
+   TYPE particle_type
+      INTEGER, POINTER :: p(:)
+   END TYPE particle_type
+   TYPE(particle_type), POINTER  :: t(:)
+   integer :: i
+   logical :: f
+   i = 1
+   allocate(t(1))
+   allocate(t(1)%p(0))
+   f = associated(t(i)%p,t(i)%p)
+ end


More information about the Gcc-patches mailing list