PR fortran/103591 - ICE in gfc_compare_string, at fortran/arith.c:1119

Message ID trinity-2d2ff381-ca4d-4b91-be3b-28ea0262bb7a-1638829176079@3c-app-gmx-bap31
State New
Headers
Series PR fortran/103591 - ICE in gfc_compare_string, at fortran/arith.c:1119 |

Commit Message

Harald Anlauf Dec. 6, 2021, 10:19 p.m. UTC
  Dear all,

we didn't check the type of the upper bound in a case range.
Bummer.  Simply add a corresponding check.

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

Thanks,
Harald
  

Comments

Mikael Morin Dec. 7, 2021, 12:35 p.m. UTC | #1
On 06/12/2021 23:19, Harald Anlauf via Fortran wrote:
> Dear all,
> 
> we didn't check the type of the upper bound in a case range.
> Bummer.  Simply add a corresponding check.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 
OK, Thanks.
  

Patch

From b4e7aeae4f6c59d8fe950d7981832e3f9c6a8f0e Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Mon, 6 Dec 2021 23:15:11 +0100
Subject: [PATCH] Fortran: add check for type of upper bound in case range

gcc/fortran/ChangeLog:

	PR fortran/103591
	* match.c (match_case_selector): Check type of upper bound in case
	range.

gcc/testsuite/ChangeLog:

	PR fortran/103591
	* gfortran.dg/select_9.f90: New test.
---
 gcc/fortran/match.c                    |  9 +++++++++
 gcc/testsuite/gfortran.dg/select_9.f90 | 10 ++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/select_9.f90

diff --git a/gcc/fortran/match.c b/gcc/fortran/match.c
index 2bf21434a42..52bc5af7542 100644
--- a/gcc/fortran/match.c
+++ b/gcc/fortran/match.c
@@ -6075,6 +6075,15 @@  match_case_selector (gfc_case **cp)
 	  m = gfc_match_init_expr (&c->high);
 	  if (m == MATCH_ERROR)
 	    goto cleanup;
+	  if (m == MATCH_YES
+	      && c->high->ts.type != BT_LOGICAL
+	      && c->high->ts.type != BT_INTEGER
+	      && c->high->ts.type != BT_CHARACTER)
+	    {
+	      gfc_error ("Expression in CASE selector at %L cannot be %s",
+			 &c->high->where, gfc_typename (c->high));
+	      goto cleanup;
+	    }
 	  /* MATCH_NO is fine.  It's OK if nothing is there!  */
 	}
     }
diff --git a/gcc/testsuite/gfortran.dg/select_9.f90 b/gcc/testsuite/gfortran.dg/select_9.f90
new file mode 100644
index 00000000000..c580e8162bd
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/select_9.f90
@@ -0,0 +1,10 @@ 
+! { dg-do compile }
+! PR fortran/103591 - ICE in gfc_compare_string
+! Contributed by G.Steinmetz
+
+program p
+  integer :: n
+  select case (n)
+  case ('1':2.)   ! { dg-error "cannot be REAL" }
+  end select
+end
--
2.26.2