Patchwork [Fortran] Avoid segfault in conformable_arrays

login
register
mail settings
Submitter Tobias Burnus
Date April 30, 2013, 4:56 p.m.
Message ID <517FF7C9.4060306@net-b.de>
Download mbox | patch
Permalink /patch/240669/
State New
Headers show

Comments

Tobias Burnus - April 30, 2013, 4:56 p.m.
The issue was found by Reinhold Bader when testing the Fortran-Dev 
branch (thanks!), but it also affects GCC 4.6 and later.

The patch is rather obvious: The segfault occured as tail->u.ar.start[i] 
== NULL. An alternative is to could use "continue" instead of "break".

Build and regtested on x86-64-gnu-linux.
OK for the trunk?

Tobias

PS: The test case is invalid Fortran 2003 but valid Fortran 2008. As 
gfortran lacks that feature, it unconditionally rejects the test case. 
Work around is to add a "(1)" to the alloc-expr.
Tobias Burnus - May 5, 2013, 1:07 p.m.
Ping.
http://gcc.gnu.org/ml/fortran/2013-04/msg00272.html

Also pending: use-associated NULL() at 
http://gcc.gnu.org/ml/fortran/2013-05/msg00006.html

Tobias Burnus wrote:
> The issue was found by Reinhold Bader when testing the Fortran-Dev 
> branch (thanks!), but it also affects GCC 4.6 and later.
>
> The patch is rather obvious: The segfault occured as 
> tail->u.ar.start[i] == NULL. An alternative is to could use "continue" 
> instead of "break".
>
> Build and regtested on x86-64-gnu-linux.
> OK for the trunk?
>
> Tobias
>
> PS: The test case is invalid Fortran 2003 but valid Fortran 2008. As 
> gfortran lacks that feature, it unconditionally rejects the test case. 
> Work around is to add a "(1)" to the alloc-expr.
Thomas Koenig - May 5, 2013, 1:41 p.m.
Hi Tobias,

> Ping.
> http://gcc.gnu.org/ml/fortran/2013-04/msg00272.html

OK.

> Also pending: use-associated NULL() at
> http://gcc.gnu.org/ml/fortran/2013-05/msg00006.html

Also OK (obvious really ;-)

Thanks for the patches!

	Thomas

Patch

2013-04-30  Tobias Burnus  <burnus@net-b.de>

	* resolve.c (conformable_arrays): Avoid segfault
	when ar.start[i] == NULL.

2013-04-30  Tobias Burnus  <burnus@net-b.de>

	* gfortran.dg/allocate_with_source_3.f90: New.

diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 6e1f56f..0f2fce0 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -6498,24 +6498,27 @@  conformable_arrays (gfc_expr *e1, gfc_expr *e2)
       return false;
     }
 
   if (e1->shape)
     {
       int i;
       mpz_t s;
 
       mpz_init (s);
 
       for (i = 0; i < e1->rank; i++)
 	{
+	  if (tail->u.ar.start[i] == NULL)
+	    break;
+
 	  if (tail->u.ar.end[i])
 	    {
 	      mpz_set (s, tail->u.ar.end[i]->value.integer);
 	      mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
 	      mpz_add_ui (s, s, 1);
 	    }
 	  else
 	    {
 	      mpz_set (s, tail->u.ar.start[i]->value.integer);
 	    }
 
 	  if (mpz_cmp (e1->shape[i], s) != 0)
--- /dev/null	2013-04-30 09:21:48.687062896 +0200
+++ gcc/gcc/testsuite/gfortran.dg/allocate_with_source_3.f90	2013-04-30 18:13:52.884740171 +0200
@@ -0,0 +1,28 @@ 
+! { dg-do compile }
+!
+! Contributed by Reinhold Bader
+!
+program assumed_shape_01
+  use, intrinsic :: iso_c_binding
+  implicit none
+  type, bind(c) :: cstruct
+     integer(c_int) :: i
+     real(c_float) :: r(2)
+  end type cstruct
+  interface
+     subroutine psub(this, that) bind(c, name='Psub')
+       import :: c_float, cstruct
+       real(c_float) :: this(:,:)
+       type(cstruct) :: that(:)
+     end subroutine psub
+  end interface
+
+  real(c_float) :: t(3,7)
+  type(cstruct), pointer :: u(:)
+
+! The following is VALID Fortran 2008 but NOT YET supported 
+  allocate(u, source=[cstruct( 4, [1.1,2.2] ) ]) ! { dg-error "Array specification required in ALLOCATE statement" }
+  call psub(t, u)
+  deallocate (u)
+
+end program assumed_shape_01