Fortran: error recovery while simplifying intrinsic UNPACK [PR107054]

Message ID trinity-00fca6df-5ba0-4282-afff-39debc94a9ae-1664305529428@3c-app-gmx-bap61
State New
Headers
Series Fortran: error recovery while simplifying intrinsic UNPACK [PR107054] |

Commit Message

Harald Anlauf Sept. 27, 2022, 7:05 p.m. UTC
  Dear all,

invalid input may trigger an assert while trying to simplify an
expression involving the intrinsic UNPACK and when the constructor
is lacking sufficient valid elements.  The obvious solution is to
replace the assert by a condition that terminates simplification
in that case.

Report and testcase by Gerhard.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

This is a 10/11/12/13 regression and shall be backported.

Thanks,
Harald
  

Comments

Mikael Morin Sept. 27, 2022, 7:30 p.m. UTC | #1
Le 27/09/2022 à 21:05, Harald Anlauf via Fortran a écrit :
> Dear all,
> 
> invalid input may trigger an assert while trying to simplify an
> expression involving the intrinsic UNPACK and when the constructor
> is lacking sufficient valid elements.  The obvious solution is to
> replace the assert by a condition that terminates simplification
> in that case.
> 
> Report and testcase by Gerhard.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 
> This is a 10/11/12/13 regression and shall be backported.
> 
OK, thanks.
  

Patch

From 80285cdad1fe98c52ebf38f9f66070b2a50191c6 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Tue, 27 Sep 2022 20:54:28 +0200
Subject: [PATCH] Fortran: error recovery while simplifying intrinsic UNPACK
 [PR107054]

gcc/fortran/ChangeLog:

	PR fortran/107054
	* simplify.cc (gfc_simplify_unpack): Replace assert by condition
	that terminates simplification when there are not enough elements
	in the constructor of argument VECTOR.

gcc/testsuite/ChangeLog:

	PR fortran/107054
	* gfortran.dg/pr107054.f90: New test.
---
 gcc/fortran/simplify.cc                | 13 ++++++++++---
 gcc/testsuite/gfortran.dg/pr107054.f90 | 13 +++++++++++++
 2 files changed, 23 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr107054.f90

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index c0fbd0ed7c2..6ac92cf9db8 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -8458,9 +8458,16 @@  gfc_simplify_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
     {
       if (mask_ctor->expr->value.logical)
 	{
-	  gcc_assert (vector_ctor);
-	  e = gfc_copy_expr (vector_ctor->expr);
-	  vector_ctor = gfc_constructor_next (vector_ctor);
+	  if (vector_ctor)
+	    {
+	      e = gfc_copy_expr (vector_ctor->expr);
+	      vector_ctor = gfc_constructor_next (vector_ctor);
+	    }
+	  else
+	    {
+	      gfc_free_expr (result);
+	      return NULL;
+	    }
 	}
       else if (field->expr_type == EXPR_ARRAY)
 	e = gfc_copy_expr (field_ctor->expr);
diff --git a/gcc/testsuite/gfortran.dg/pr107054.f90 b/gcc/testsuite/gfortran.dg/pr107054.f90
new file mode 100644
index 00000000000..bbfe646beba
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr107054.f90
@@ -0,0 +1,13 @@ 
+! { dg-do compile }
+! PR fortran/107054 - ICE in gfc_simplify_unpack
+! Contributed by G.Steinmetz
+
+program p
+  type t
+     integer :: n = 0
+  end type
+  type(t), parameter :: a(4) = t(2)
+  type(t), parameter :: b(4) = reshape(a,[2]) ! { dg-error "Different shape" }
+  type(t), parameter :: c(2) = pack(b,[.false.,.true.,.false.,.true.]) ! { dg-error "Different shape" }
+  type(t), parameter :: d(4) = unpack(c,[.false.,.true.,.false.,.true.],a)
+end
--
2.35.3