diff mbox

[Fortran] Prevent segfault on illegal input

Message ID 20150313113336.154f327b@vepi2
State New
Headers show

Commit Message

Andre Vehreschild March 13, 2015, 10:33 a.m. UTC
Hi all,

during debugging I found a segfault of gfortran, when it encounters an illegal
fortran code. The mistake in Fortran is flagged correctly, but later gfortran
crashes. This patch prevents the crash.

Bootstraps and regtest ok on x86_64-linux-gnu.

Ok for trunk?

Regards,
	Andre

Comments

Tobias Burnus March 13, 2015, 1:05 p.m. UTC | #1
Andre Vehreschild wrote:
> during debugging I found a segfault of gfortran, when it encounters an illegal
> fortran code. The mistake in Fortran is flagged correctly, but later gfortran
> crashes. This patch prevents the crash.
>
> Bootstraps and regtest ok on x86_64-linux-gnu.
> Ok for trunk?

OK. Thanks for the patch.

Tobias
diff mbox

Patch

diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 942a9ad..465cf2b 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -2639,6 +2639,10 @@  found:
     expr->ts = sym->ts;
   expr->value.function.name = sym->name;
   expr->value.function.esym = sym;
+  /* Prevent crash when sym->ts.u.derived->components is not set due to previous
+     error(s).  */
+  if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
+    return MATCH_ERROR;
   if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
     expr->rank = CLASS_DATA (sym)->as->rank;
   else if (sym->as != NULL)
diff --git a/gcc/testsuite/gfortran.dg/pointer_2.f90 b/gcc/testsuite/gfortran.dg/pointer_2.f90
new file mode 100644
index 0000000..d3b95d6
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pointer_2.f90
@@ -0,0 +1,18 @@ 
+! { dg-do compile }
+! Check that the compiler reports the errors, but does not segfault.
+! Contributed by: Andre Vehreschild  <vehre@gcc.gnu.org>
+!
+program test
+    implicit none
+    class(*), pointer :: P
+    class(*), allocatable :: P2
+
+    allocate(P2, source=convertType(P))
+
+contains
+
+  function convertType(in) ! { dg-error "must be dummy, allocatable or pointer" }
+    class(*), intent(in) :: in
+    class(*) :: convertType
+  end function
+end program test