PR fortran/102520 - [10/11/12 Regression] ICE in expand_constructor, at fortran/array.c:1802

Message ID trinity-3e5df94d-a6a5-414e-a10a-88d4e0623356-1632861930464@3c-app-gmx-bs02
State New
Headers
Series PR fortran/102520 - [10/11/12 Regression] ICE in expand_constructor, at fortran/array.c:1802 |

Commit Message

Harald Anlauf Sept. 28, 2021, 8:45 p.m. UTC
  Dear Fortranners,

Gerhard's testcase triggers a NULL pointer dereference during the
attempt to expand an invalid constructor.  The simple and obvious
solution is to catch that case.

Regtested on x86_64-pc-linux-gnu.  OK for all affected branches?

Thanks,
Harald
  

Comments

Thomas Koenig Sept. 28, 2021, 9:15 p.m. UTC | #1
Hi Harald,

> Gerhard's testcase triggers a NULL pointer dereference during the
> attempt to expand an invalid constructor.  The simple and obvious
> solution is to catch that case.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for all affected branches?

OK.

Thanks for the patch!

Best regards

	Thomas
  

Patch

Fortran: fix error recovery for invalid constructor

gcc/fortran/ChangeLog:

	PR fortran/102520
	* array.c (expand_constructor): Do not dereference NULL pointer.

gcc/testsuite/ChangeLog:

	PR fortran/102520
	* gfortran.dg/pr102520.f90: New test.

diff --git a/gcc/fortran/array.c b/gcc/fortran/array.c
index b858bada18a..8d66e009f66 100644
--- a/gcc/fortran/array.c
+++ b/gcc/fortran/array.c
@@ -1798,6 +1805,9 @@  expand_constructor (gfc_constructor_base base)

       e = c->expr;

+      if (e == NULL)
+	return false;
+
       if (empty_constructor)
 	empty_ts = e->ts;

diff --git a/gcc/testsuite/gfortran.dg/pr102520.f90 b/gcc/testsuite/gfortran.dg/pr102520.f90
new file mode 100644
index 00000000000..1c98c185c17
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr102520.f90
@@ -0,0 +1,12 @@ 
+! { dg-do compile }
+! PR fortran/102520 - ICE in expand_constructor, at fortran/array.c:1802
+
+program p
+  type t
+  end type
+  type(t), parameter :: a(4)   = shape(1)         ! { dg-error "Incompatible" }
+  type(t), parameter :: b(2,2) = reshape(a,[2,2]) ! { dg-error "Incompatible" }
+  type(t), parameter :: c(2,2) = transpose(b)     ! { dg-error "Unclassifiable" }
+end
+
+! { dg-error "Different shape for array assignment" " " { target *-*-* } 7 }