diff mbox series

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

Message ID trinity-09564ecb-327d-451b-8e81-ced547aa785b-1611522719872@3c-app-gmx-bap45
State New
Headers show
Series PR fortran/70070 - ICE on initializing character data beyond min/max bound | expand

Commit Message

Harald Anlauf Jan. 24, 2021, 9:12 p.m. UTC
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.

Comments

Thomas Koenig Jan. 25, 2021, 6:58 p.m. UTC | #1
Hi Harald,

> 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.

Currently, we issue these error messages in similar circumstances:

$ cat a.f90
   character (len=3) :: a
   a = 'abc'
   print a(0:3)
   print a(1:4)
end program
$ gfortran a.f90
a.f90:3:10:

     3 |   print a(0:3)
       |          1
Error: Substring start index at (1) is less than one
a.f90:4:10:

     4 |   print a(1:4)
       |          1
Error: Substring end index at (1) exceeds the string length

Could you maybe just re-use these?

> Regtested on x86_64-pc-linux-gnu.
> 
> OK for master?

OK with adjusted error message.  Thanks for the patch!

Best regards

	Thomas
Harald Anlauf Jan. 25, 2021, 8:43 p.m. UTC | #2
Hi Thomas,

> Gesendet: Montag, 25. Januar 2021 um 19:58 Uhr
> Von: "Thomas Koenig" <tkoenig@netcologne.de>

> a.f90:3:10:
>
>      3 |   print a(0:3)
>        |          1
> Error: Substring start index at (1) is less than one
> a.f90:4:10:
>
>      4 |   print a(1:4)
>        |          1
> Error: Substring end index at (1) exceeds the string length
>
> Could you maybe just re-use these?

this is done in the attached patch.  Committed and pushed to master.

> OK with adjusted error message.  Thanks for the patch!

Thanks for the review!

Harald
diff mbox series

Patch

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