diff mbox

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

Message ID 20151021200558.GA71516@troutmask.apl.washington.edu
State New
Headers show

Commit Message

Steve Kargl Oct. 21, 2015, 8:05 p.m. UTC
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.

Comments

FX Coudert Oct. 21, 2015, 9:18 p.m. UTC | #1
> 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.

OK, thanks
Paul Richard Thomas Oct. 21, 2015, 9:20 p.m. UTC | #2
Hi Steve,

It looks good to me - OK to commit.

Cheers

Paul

PS You, as far as I can tell, are the second most prolific bug fixer
of 2015. The only person that beats you is Mr(or Ms) Unassigned. We
need to sort out who this person is.....

On 21 October 2015 at 22:05, Steve Kargl
<sgk@troutmask.apl.washington.edu> wrote:
> 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
Steve Kargl Oct. 21, 2015, 9:31 p.m. UTC | #3
On Wed, Oct 21, 2015 at 11:20:55PM +0200, Paul Richard Thomas wrote:
> 
> It looks good to me - OK to commit.
> 
> Cheers
> 
> Paul
> 
> PS You, as far as I can tell, are the second most prolific bug fixer
> of 2015. The only person that beats you is Mr(or Ms) Unassigned. We
> need to sort out who this person is.....
> 

Well, Gerhard Steinmetz is submitting small self-contained
bugs that appear to be corner cases in the matchers.  Allr
are low hanging fruit.  I leave the hard PR's to people that
know what they are doing. ;-)
diff mbox

Patch

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
+