public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fortran: diagnose strings of non-constant length in DATA statements [PR68569]
@ 2023-07-26 19:10 Harald Anlauf
  2023-07-26 19:17 ` [PATCH, v2] " Harald Anlauf
  0 siblings, 1 reply; 4+ messages in thread
From: Harald Anlauf @ 2023-07-26 19:10 UTC (permalink / raw)
  To: fortran, gcc-patches

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

Dear all,

the attached patch fixes an ICE-on-invalid after use of strings of
non-constant length in DATA statements.

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

Thanks,
Harald


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

From b5b13db48c169ef18a8b75739bd4f22f9fd5654e Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Wed, 26 Jul 2023 20:46:50 +0200
Subject: [PATCH] Fortran: diagnose strings of non-constant length in DATA
 statements [PR68569]

gcc/fortran/ChangeLog:

	PR fortran/68569
	* resolve.cc (check_data_variable): Do not accept strings with
	deferred length or non-constant length in a DATA statement.
	Reject also substrings of string variables of non-constant length.

gcc/testsuite/ChangeLog:

	PR fortran/68569
	* gfortran.dg/data_char_6.f90: New test.
---
 gcc/fortran/resolve.cc                    | 22 ++++++++++++++++++-
 gcc/testsuite/gfortran.dg/data_char_6.f90 | 26 +++++++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/data_char_6.f90

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index f7cfdfc133f..3cd470ddcca 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -16771,7 +16771,6 @@ check_data_variable (gfc_data_variable *var, locus *where)
     return false;

   ar = NULL;
-  mpz_init_set_si (offset, 0);
   e = var->expr;

   if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
@@ -16838,8 +16837,24 @@ check_data_variable (gfc_data_variable *var, locus *where)
 		     "attribute", ref->u.c.component->name, &e->where);
 	  return false;
 	}
+
+      /* Reject substrings of strings of non-constant length.  */
+      if (ref->type == REF_SUBSTRING
+	  && ref->u.ss.length
+	  && ref->u.ss.length->length
+	  && !gfc_is_constant_expr (ref->u.ss.length->length))
+	goto bad_charlen;
     }

+  /* Reject strings with deferred length or non-constant length.  */
+  if (e->ts.type == BT_CHARACTER
+      && (e->ts.deferred
+	  || (e->ts.u.cl->length
+	      && !gfc_is_constant_expr (e->ts.u.cl->length))))
+    goto bad_charlen;
+
+  mpz_init_set_si (offset, 0);
+
   if (e->rank == 0 || has_pointer)
     {
       mpz_init_set_ui (size, 1);
@@ -16967,6 +16982,11 @@ check_data_variable (gfc_data_variable *var, locus *where)
   mpz_clear (offset);

   return t;
+
+bad_charlen:
+  gfc_error ("Non-constant character length at %L in DATA statement",
+	     &e->where);
+  return false;
 }


diff --git a/gcc/testsuite/gfortran.dg/data_char_6.f90 b/gcc/testsuite/gfortran.dg/data_char_6.f90
new file mode 100644
index 00000000000..4e32c647d4d
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/data_char_6.f90
@@ -0,0 +1,26 @@
+! { dg-do compile }
+! PR fortran/68569 - ICE with automatic character object and DATA
+! Contributed by G. Steinmetz
+
+subroutine s1 (n)
+  implicit none
+  integer, intent(in) :: n
+  character(n) :: x
+  data x /'a'/         ! { dg-error "Non-constant character length" }
+end
+
+subroutine s2 (n)
+  implicit none
+  integer, intent(in) :: n
+  character(n) :: x
+  data x(1:1) /'a'/    ! { dg-error "Non-constant character length" }
+end
+
+subroutine s3 ()
+  implicit none
+  type t
+     character(:) :: c ! { dg-error "must be a POINTER or ALLOCATABLE" }
+  end type t
+  type(t) :: tp
+  data tp%c /'a'/      ! { dg-error "Non-constant character length" }
+end
--
2.35.3


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

* [PATCH, v2] Fortran: diagnose strings of non-constant length in DATA statements [PR68569]
  2023-07-26 19:10 [PATCH] Fortran: diagnose strings of non-constant length in DATA statements [PR68569] Harald Anlauf
@ 2023-07-26 19:17 ` Harald Anlauf
  2023-07-26 19:33   ` [PATCH, v3] " Harald Anlauf
  0 siblings, 1 reply; 4+ messages in thread
From: Harald Anlauf @ 2023-07-26 19:17 UTC (permalink / raw)
  To: fortran, gcc-patches

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

Dear all,

the original submission missed the adjustments of the expected
patterns of 2 tests.  This is corrected in the new attachments.

Am 26.07.23 um 21:10 schrieb Harald Anlauf via Gcc-patches:
> Dear all,
>
> the attached patch fixes an ICE-on-invalid after use of strings of
> non-constant length in DATA statements.
>
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>
> Thanks,
> Harald
>

Thanks,
Harald


[-- Attachment #2: pr68569-v2.patch --]
[-- Type: text/x-patch, Size: 1415 bytes --]

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index f7cfdfc133f..cd8e223edce 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -16771,7 +16787,6 @@ check_data_variable (gfc_data_variable *var, locus *where)
     return false;
 
   ar = NULL;
-  mpz_init_set_si (offset, 0);
   e = var->expr;
 
   if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
@@ -16838,8 +16853,24 @@ check_data_variable (gfc_data_variable *var, locus *where)
 		     "attribute", ref->u.c.component->name, &e->where);
 	  return false;
 	}
+
+      /* Reject substrings of strings of non-constant length.  */
+      if (ref->type == REF_SUBSTRING
+	  && ref->u.ss.length
+	  && ref->u.ss.length->length
+	  && !gfc_is_constant_expr (ref->u.ss.length->length))
+	goto bad_charlen;
     }
 
+  /* Reject deferred length character and strings of non-constant length.  */
+  if (e->ts.type == BT_CHARACTER
+      && (e->ts.deferred
+	  || (e->ts.u.cl->length
+	      && !gfc_is_constant_expr (e->ts.u.cl->length))))
+    goto bad_charlen;
+
+  mpz_init_set_si (offset, 0);
+
   if (e->rank == 0 || has_pointer)
     {
       mpz_init_set_ui (size, 1);
@@ -16967,6 +16998,11 @@ check_data_variable (gfc_data_variable *var, locus *where)
   mpz_clear (offset);
 
   return t;
+
+bad_charlen:
+  gfc_error ("Non-constant character length at %L in DATA statement",
+	     &e->where);
+  return false;
 }
 
 

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

* [PATCH, v3] Fortran: diagnose strings of non-constant length in DATA statements [PR68569]
  2023-07-26 19:17 ` [PATCH, v2] " Harald Anlauf
@ 2023-07-26 19:33   ` Harald Anlauf
  2023-07-26 19:44     ` Steve Kargl
  0 siblings, 1 reply; 4+ messages in thread
From: Harald Anlauf @ 2023-07-26 19:33 UTC (permalink / raw)
  To: fortran, gcc-patches

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

I am going to get the brown bag for today...  This is now the right
corrected patch.

Sorry for all the noise!

Harald

Am 26.07.23 um 21:17 schrieb Harald Anlauf via Gcc-patches:
> Dear all,
>
> the original submission missed the adjustments of the expected
> patterns of 2 tests.  This is corrected in the new attachments.
>
> Am 26.07.23 um 21:10 schrieb Harald Anlauf via Gcc-patches:
>> Dear all,
>>
>> the attached patch fixes an ICE-on-invalid after use of strings of
>> non-constant length in DATA statements.
>>
>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>
>> Thanks,
>> Harald
>>
>
> Thanks,
> Harald
>

[-- Attachment #2: pr68569-v2.diff --]
[-- Type: text/x-patch, Size: 4553 bytes --]

From d872b8ffc121fd57d47aa7d3d12d9ba86389f092 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Wed, 26 Jul 2023 21:12:45 +0200
Subject: [PATCH] Fortran: diagnose strings of non-constant length in DATA
 statements [PR68569]

gcc/fortran/ChangeLog:

	PR fortran/68569
	* resolve.cc (check_data_variable): Do not accept strings with
	deferred length or non-constant length in a DATA statement.
	Reject also substrings of string variables of non-constant length.

gcc/testsuite/ChangeLog:

	PR fortran/68569
	* gfortran.dg/data_char_4.f90: Adjust expected diagnostic.
	* gfortran.dg/data_char_5.f90: Likewise.
	* gfortran.dg/data_char_6.f90: New test.
---
 gcc/fortran/resolve.cc                    | 22 ++++++++++++++++++-
 gcc/testsuite/gfortran.dg/data_char_4.f90 |  2 +-
 gcc/testsuite/gfortran.dg/data_char_5.f90 |  8 +++----
 gcc/testsuite/gfortran.dg/data_char_6.f90 | 26 +++++++++++++++++++++++
 4 files changed, 52 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/data_char_6.f90

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index f7cfdfc133f..3cd470ddcca 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -16771,7 +16771,6 @@ check_data_variable (gfc_data_variable *var, locus *where)
     return false;
 
   ar = NULL;
-  mpz_init_set_si (offset, 0);
   e = var->expr;
 
   if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
@@ -16838,8 +16837,24 @@ check_data_variable (gfc_data_variable *var, locus *where)
 		     "attribute", ref->u.c.component->name, &e->where);
 	  return false;
 	}
+
+      /* Reject substrings of strings of non-constant length.  */
+      if (ref->type == REF_SUBSTRING
+	  && ref->u.ss.length
+	  && ref->u.ss.length->length
+	  && !gfc_is_constant_expr (ref->u.ss.length->length))
+	goto bad_charlen;
     }
 
+  /* Reject strings with deferred length or non-constant length.  */
+  if (e->ts.type == BT_CHARACTER
+      && (e->ts.deferred
+	  || (e->ts.u.cl->length
+	      && !gfc_is_constant_expr (e->ts.u.cl->length))))
+    goto bad_charlen;
+
+  mpz_init_set_si (offset, 0);
+
   if (e->rank == 0 || has_pointer)
     {
       mpz_init_set_ui (size, 1);
@@ -16967,6 +16982,11 @@ check_data_variable (gfc_data_variable *var, locus *where)
   mpz_clear (offset);
 
   return t;
+
+bad_charlen:
+  gfc_error ("Non-constant character length at %L in DATA statement",
+	     &e->where);
+  return false;
 }
 
 
diff --git a/gcc/testsuite/gfortran.dg/data_char_4.f90 b/gcc/testsuite/gfortran.dg/data_char_4.f90
index ed0782ce8a0..fa5e0a0134a 100644
--- a/gcc/testsuite/gfortran.dg/data_char_4.f90
+++ b/gcc/testsuite/gfortran.dg/data_char_4.f90
@@ -4,7 +4,7 @@
 
 program p
   character(l) :: c(2) ! { dg-error "must have constant character length" }
-  data c /'a', 'b'/
+  data c /'a', 'b'/    ! { dg-error "Non-constant character length" }
   common c
 end
 
diff --git a/gcc/testsuite/gfortran.dg/data_char_5.f90 b/gcc/testsuite/gfortran.dg/data_char_5.f90
index ea26687e3d5..7556e63c01b 100644
--- a/gcc/testsuite/gfortran.dg/data_char_5.f90
+++ b/gcc/testsuite/gfortran.dg/data_char_5.f90
@@ -4,12 +4,12 @@
 subroutine sub ()
   integer :: ll = 4
   block
-    character(ll) :: c(2) ! { dg-error "non-constant" }
-    data c /'a', 'b'/
+    character(ll) :: c(2)
+    data c /'a', 'b'/     ! { dg-error "Non-constant character length" }
   end block
 contains
   subroutine sub1 ()
-    character(ll) :: d(2) ! { dg-error "non-constant" }
-    data d /'a', 'b'/
+    character(ll) :: d(2)
+    data d /'a', 'b'/     ! { dg-error "Non-constant character length" }
   end subroutine sub1
 end subroutine sub
diff --git a/gcc/testsuite/gfortran.dg/data_char_6.f90 b/gcc/testsuite/gfortran.dg/data_char_6.f90
new file mode 100644
index 00000000000..4e32c647d4d
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/data_char_6.f90
@@ -0,0 +1,26 @@
+! { dg-do compile }
+! PR fortran/68569 - ICE with automatic character object and DATA 
+! Contributed by G. Steinmetz
+
+subroutine s1 (n)
+  implicit none
+  integer, intent(in) :: n
+  character(n) :: x
+  data x /'a'/         ! { dg-error "Non-constant character length" }
+end
+
+subroutine s2 (n)
+  implicit none
+  integer, intent(in) :: n
+  character(n) :: x
+  data x(1:1) /'a'/    ! { dg-error "Non-constant character length" }
+end
+
+subroutine s3 ()
+  implicit none
+  type t
+     character(:) :: c ! { dg-error "must be a POINTER or ALLOCATABLE" }
+  end type t
+  type(t) :: tp
+  data tp%c /'a'/      ! { dg-error "Non-constant character length" }
+end
-- 
2.35.3


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

* Re: [PATCH, v3] Fortran: diagnose strings of non-constant length in DATA statements [PR68569]
  2023-07-26 19:33   ` [PATCH, v3] " Harald Anlauf
@ 2023-07-26 19:44     ` Steve Kargl
  0 siblings, 0 replies; 4+ messages in thread
From: Steve Kargl @ 2023-07-26 19:44 UTC (permalink / raw)
  To: Harald Anlauf via Fortran; +Cc: gcc-patches

On Wed, Jul 26, 2023 at 09:33:22PM +0200, Harald Anlauf via Fortran wrote:
> I am going to get the brown bag for today...  This is now the right
> corrected patch.
> 
> Sorry for all the noise!
> 

Third times a charm (as the saying goes).

Looks good to me.  Thanks for the patch.

-- 
Steve

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

end of thread, other threads:[~2023-07-26 19:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-26 19:10 [PATCH] Fortran: diagnose strings of non-constant length in DATA statements [PR68569] Harald Anlauf
2023-07-26 19:17 ` [PATCH, v2] " Harald Anlauf
2023-07-26 19:33   ` [PATCH, v3] " Harald Anlauf
2023-07-26 19:44     ` Steve Kargl

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