public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR fortran/70070 - ICE on initializing character data beyond min/max bound
@ 2021-01-24 21:12 Harald Anlauf
  2021-01-25 18:58 ` Thomas Koenig
  0 siblings, 1 reply; 3+ messages in thread
From: Harald Anlauf @ 2021-01-24 21:12 UTC (permalink / raw)
  To: fortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 779 bytes --]

Dear all,

the attached patch is pretty much self-explaining: check for bounds violation
when initializing a substring in a data statement and treat the resulting error.

If more detailed information should be emitted with the error message, I'm
open for suggestions.

Regtested on x86_64-pc-linux-gnu.

OK for master?

Thanks,
Harald


PR fortran/70070 - ICE on initializing character data beyond min/max bound

Check for initialization of substrings beyond bounds in DATA statements.

gcc/fortran/ChangeLog:

	PR fortran/70070
	* data.c (create_character_initializer): Check substring indices
	against bounds.
	(gfc_assign_data_value): Catch error returned from
	create_character_initializer.

gcc/testsuite/ChangeLog:

	PR fortran/70070
	* gfortran.dg/pr70070.f90: New test.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr70070.patch --]
[-- Type: text/x-patch, Size: 1373 bytes --]

diff --git a/gcc/fortran/data.c b/gcc/fortran/data.c
index 1313b335c86..d9f0b45da9b 100644
--- a/gcc/fortran/data.c
+++ b/gcc/fortran/data.c
@@ -183,6 +183,13 @@ create_character_initializer (gfc_expr *init, gfc_typespec *ts,
 	}
     }

+  if (start < 0 || end > init->value.character.length)
+    {
+      gfc_error ("Invalid substring in DATA statement at %L",
+		 &ref->u.ss.start->where);
+      return NULL;
+    }
+
   if (rvalue->ts.type == BT_HOLLERITH)
     {
       for (size_t i = 0; i < (size_t) len; i++)
@@ -576,6 +583,8 @@ gfc_assign_data_value (gfc_expr *lvalue, gfc_expr *rvalue, mpz_t index,
       if (lvalue->ts.u.cl->length == NULL && !(ref && ref->u.ss.length != NULL))
 	return false;
       expr = create_character_initializer (init, last_ts, ref, rvalue);
+      if (!expr)
+	return false;
     }
   else
     {
diff --git a/gcc/testsuite/gfortran.dg/pr70070.f90 b/gcc/testsuite/gfortran.dg/pr70070.f90
new file mode 100644
index 00000000000..c79cd229552
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr70070.f90
@@ -0,0 +1,8 @@
+! { dg-do compile }
+! PR fortran/70070 - ICE on initializing character data beyond min/max bound
+
+program p
+  character(1) :: a, b
+  data (a(i:i),i=0,0) /1*'#'/   ! { dg-error "Invalid substring" }
+  data (b(i:i),i=1,2) /2*'#'/   ! { dg-error "Invalid substring" }
+end

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-01-25 20:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-24 21:12 [PATCH] PR fortran/70070 - ICE on initializing character data beyond min/max bound Harald Anlauf
2021-01-25 18:58 ` Thomas Koenig
2021-01-25 20:43   ` Harald Anlauf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).