]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: diagnose strings of non-constant length in DATA statements [PR68569]
authorHarald Anlauf <anlauf@gmx.de>
Wed, 26 Jul 2023 19:12:45 +0000 (21:12 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Wed, 26 Jul 2023 19:45:28 +0000 (21:45 +0200)
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
gcc/testsuite/gfortran.dg/data_char_4.f90
gcc/testsuite/gfortran.dg/data_char_5.f90
gcc/testsuite/gfortran.dg/data_char_6.f90 [new file with mode: 0644]

index f7cfdfc133faab39d10b9fc8a9ebc9542eb1f416..3cd470ddccaf9a0d967fbdd2941cc9afa7d6fd46 100644 (file)
@@ -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;
 }
 
 
index ed0782ce8a08c0e55c2c4e0baaa2e547c17018ee..fa5e0a0134af5952868be232326aae5a8c9ddb65 100644 (file)
@@ -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
 
index ea26687e3d57d853ca3702335293e531ff299275..7556e63c01b819ac589b921b64c45bbb8209dd9f 100644 (file)
@@ -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 (file)
index 0000000..4e32c64
--- /dev/null
@@ -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