I've committed the attached patch after checking that
it does not cause any regression and that it fixes the
problem in the PR.  The patch checks for a negative
string length, and resets it to 0 if it is < 0.

2018-04-24  Steven G. Kargl  <ka...@gcc.gnu.org>

        PR fortran/85520
        * decl.c (gfc_match_char_spec): Check for negative length and set to 0.

2018-04-24  Steven G. Kargl  <ka...@gcc.gnu.org>

        PR fortran/85520
        * gfortran.dg/pr85520.f90: New test.

-- 
Steve
Index: gcc/fortran/decl.c
===================================================================
--- gcc/fortran/decl.c	(revision 259619)
+++ gcc/fortran/decl.c	(working copy)
@@ -3235,7 +3249,11 @@ done:
 	  e = gfc_copy_expr (len);
 	  gfc_reduce_init_expr (e);
 	  if (e->expr_type == EXPR_CONSTANT)
-	    gfc_replace_expr (len, e);
+	    {
+	      gfc_replace_expr (len, e);
+	      if (mpz_cmp_si (len->value.integer, 0) < 0)
+		mpz_set_ui (len->value.integer, 0);
+	    }
 	  else
 	    gfc_free_expr (e);
 	  cl->length = len;
Index: gcc/testsuite/gfortran.dg/pr85520.f90
===================================================================
--- gcc/testsuite/gfortran.dg/pr85520.f90	(nonexistent)
+++ gcc/testsuite/gfortran.dg/pr85520.f90	(working copy)
@@ -0,0 +1,7 @@
+! { dg-do run }
+! PR fortran/85520
+! Original code from Gerhard Steinmetz <gscfq at t-online dot de>
+program p
+   character(-huge(1)) :: c = ' '
+    if (len(c) /= 0) stop 1
+end

Reply via email to