This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gfortran,patch] Fix undue compile-time error message


:ADDPATCH fortran:

Attached patch fixes rejects-valid PR fortran/28081, where the following code:
     character(len=10) :: x
     x(16:15) = "aa"
     x(0:-1) = "bb"
used to generate out-of-bounds error messages during the compilation.
It's almost trivial, but I have had so little sleep these last few
days/weeks, that I won't commit anything as "obvious" :)

Regtested on i686-linux, comes with a testcase. OK for mainline and 4.1?

FX
Index: gcc/fortran/resolve.c
===================================================================
--- gcc/fortran/resolve.c	(revision 114721)
+++ gcc/fortran/resolve.c	(working copy)
@@ -2542,7 +2542,9 @@
 	  return FAILURE;
 	}
 
-      if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT)
+      if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
+	  && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
+	      || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
 	{
 	  gfc_error ("Substring start index at %L is less than one",
 		     &ref->u.ss.start->where);
@@ -2570,9 +2572,11 @@
 	}
 
       if (ref->u.ss.length != NULL
-	  && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT)
+	  && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
+	  && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
+	      || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
 	{
-	  gfc_error ("Substring end index at %L is out of bounds",
+	  gfc_error ("Substring end index at %L exceeds the string length",
 		     &ref->u.ss.start->where);
 	  return FAILURE;
 	}
Index: gcc/testsuite/gfortran.dg/substr_3.f
===================================================================
--- gcc/testsuite/gfortran.dg/substr_3.f	(revision 0)
+++ gcc/testsuite/gfortran.dg/substr_3.f	(revision 0)
@@ -0,0 +1,12 @@
+! { dg-do run }
+! Check that substrings behave correctly even when zero-sized
+      implicit none
+      character(len=10) :: s, t
+      integer :: i, j
+
+      s = "abcdefghij"
+      t(:10) = s(1:)
+      s(16:15) = "foo"
+      s(0:-1) = "foo"
+      if (s /= t) call abort
+      end

Attachment: substring3.ChangeLog
Description: Binary data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]