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

2015-10-21 Thread Steve Kargl
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  

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

2015-10-21  Steven G. Kargl  

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)",
-		   >where, end - start, len);
-  len = end - start;
+  if (tlen < 0)
+	{
+	  gfc_warning_now (0, "Unused initialization string at %L because "
+			   "variable has zero length", >where);
+	  len = 0;
+	}
+  else
+	{
+	  gfc_warning_now (0, "Initialization string at %L was truncated to "
+			   "fit the variable (%d/%d)", >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 ([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
+


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

2015-10-21 Thread FX
> 2015-10-21  Steven G. Kargl  
> 
>   PR fortran/67939
>   * data.c (create_character_initializer): Deal with zero length string.
> 
> 2015-10-21  Steven G. Kargl  
> 
>   PR fortran/67939
>   * gfortran.dg/pr67939.f90: New test.

OK, thanks


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

2015-10-21 Thread Paul Richard Thomas
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
 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  
>
> PR fortran/67939
> * data.c (create_character_initializer): Deal with zero length string.
>
> 2015-10-21  Steven G. Kargl  
>
> PR fortran/67939
> * gfortran.dg/pr67939.f90: New test.
>
> --
> Steve



-- 
Outside of a dog, a book is a man's best friend. Inside of a dog it's
too dark to read.

Groucho Marx


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

2015-10-21 Thread Steve Kargl
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. ;-)

-- 
Steve