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

* Re: [PATCH] PR fortran/70070 - ICE on initializing character data beyond min/max bound
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Koenig @ 2021-01-25 18:58 UTC (permalink / raw)
  To: Harald Anlauf, fortran, gcc-patches

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

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

* Re: [PATCH] PR fortran/70070 - ICE on initializing character data beyond min/max bound
  2021-01-25 18:58 ` Thomas Koenig
@ 2021-01-25 20:43   ` Harald Anlauf
  0 siblings, 0 replies; 3+ messages in thread
From: Harald Anlauf @ 2021-01-25 20:43 UTC (permalink / raw)
  To: Thomas Koenig; +Cc: fortran, gcc-patches

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

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


[-- Attachment #2: pr70070.patch-v2 --]
[-- Type: application/octet-stream, Size: 1528 bytes --]

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

+  if (start < 0)
+    {
+      gfc_error ("Substring start index at %L is less than one",
+		 &ref->u.ss.start->where);
+      return NULL;
+    }
+  if (end > init->value.character.length)
+    {
+      gfc_error ("Substring end index at %L exceeds the string length",
+		 &ref->u.ss.end->where);
+      return NULL;
+    }
+
   if (rvalue->ts.type == BT_HOLLERITH)
     {
       for (size_t i = 0; i < (size_t) len; i++)
@@ -576,6 +589,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..04a6dd585ab
--- /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 "Substring start index" }
+  data (b(i:i),i=2,3) /2*'#'/   ! { dg-error "Substring end index" }
+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).