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]

[PATCH] PR fortran/67939 -- Fix zero length strings in DATA statement


The attach patch properly sets the length for a zero length string
in a data.  Built and regression tested on x86_64-*-freebsd.  The
testcase is self-explanatory.

OK to commit?


2015-10-21  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/67939
	* data.c (create_character_initializer): Deal with zero length string.

2015-10-21  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/67939
	* gfortran.dg/pr67939.f90: New test.

-- 
Steve
Index: gcc/fortran/data.c
===================================================================
--- gcc/fortran/data.c	(revision 229138)
+++ gcc/fortran/data.c	(working copy)
@@ -104,7 +104,7 @@ static gfc_expr *
 create_character_initializer (gfc_expr *init, gfc_typespec *ts,
 			      gfc_ref *ref, gfc_expr *rvalue)
 {
-  int len, start, end;
+  int len, start, end, tlen;
   gfc_char_t *dest;
   bool alloced_init = false;
 	    
@@ -162,12 +162,22 @@ create_character_initializer (gfc_expr *
   else
     len = rvalue->value.character.length;
 
-  if (len > end - start)
+  tlen = end - start;
+  if (len > tlen)
     {
-      gfc_warning_now (0, "Initialization string starting at %L was "
-		       "truncated to fit the variable (%d/%d)",
-		       &rvalue->where, end - start, len);
-      len = end - start;
+      if (tlen < 0)
+	{
+	  gfc_warning_now (0, "Unused initialization string at %L because "
+			   "variable has zero length", &rvalue->where);
+	  len = 0;
+	}
+      else
+	{
+	  gfc_warning_now (0, "Initialization string at %L was truncated to "
+			   "fit the variable (%d/%d)", &rvalue->where,
+			   tlen, len);
+	  len = tlen;
+	}
     }
 
   if (rvalue->ts.type == BT_HOLLERITH)
@@ -181,7 +191,7 @@ create_character_initializer (gfc_expr *
 	    len * sizeof (gfc_char_t));
 
   /* Pad with spaces.  Substrings will already be blanked.  */
-  if (len < end - start && ref == NULL)
+  if (len < tlen && ref == NULL)
     gfc_wide_memset (&dest[start + len], ' ', end - (start + len));
 
   if (rvalue->ts.type == BT_HOLLERITH)
Index: gcc/testsuite/gfortran.dg/pr67939.f90
===================================================================
--- gcc/testsuite/gfortran.dg/pr67939.f90	(revision 0)
+++ gcc/testsuite/gfortran.dg/pr67939.f90	(working copy)
@@ -0,0 +1,21 @@
+! { dg-do compile }
+! PR fortran/67939
+! Original code by Gerhard Steinmetz
+! gerhard dot steinmetz dot fortran at t-online dot de
+!
+program p
+   character(100) :: x
+   data x(998:99) /'ab'/   ! { dg-warning "Unused initialization string" }
+   call a
+end
+
+subroutine a
+   character(2) :: x
+   data x(:-1) /'ab'/      ! { dg-warning "Unused initialization string" }
+end subroutine a
+
+subroutine b
+   character(8) :: x
+   data x(3:1) /'abc'/     ! { dg-warning "Unused initialization string" }
+end subroutine b
+

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