From 58da192e7a35ed16e23d5a33246d68fae3d1a1dc Mon Sep 17 00:00:00 2001 From: "Steven G. Kargl" Date: Tue, 24 Apr 2018 23:17:29 +0000 Subject: [PATCH] re PR fortran/85520 (Out of memory when declaring a character with len << 0) 2018-04-24 Steven G. Kargl PR fortran/85520 * decl.c (gfc_match_char_spec): Check for negative length and set to 0. 2018-04-24 Steven G. Kargl PR fortran/85520 * gfortran.dg/pr85520.f90: New test. From-SVN: r259623 --- gcc/fortran/ChangeLog | 5 +++++ gcc/fortran/decl.c | 6 +++++- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gfortran.dg/pr85520.f90 | 7 +++++++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/pr85520.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 9d126868cc9..46c234fdab9 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,8 @@ +2018-04-24 Steven G. Kargl + + PR fortran/85520 + * decl.c (gfc_match_char_spec): Check for negative length and set to 0. + 2018-04-14 Andre Vehreschild PR fortran/81773 diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c index 192910dfe65..d1b75ccd54c 100644 --- a/gcc/fortran/decl.c +++ b/gcc/fortran/decl.c @@ -3235,7 +3235,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; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index ff4b5fbe283..6ba8fede06b 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-04-24 Steven G. Kargl + + PR fortran/85520 + * gfortran.dg/pr85520.f90: New test. + 2018-04-24 Kyrylo Tkachov PR target/85512 diff --git a/gcc/testsuite/gfortran.dg/pr85520.f90 b/gcc/testsuite/gfortran.dg/pr85520.f90 new file mode 100644 index 00000000000..3e66a9020f8 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr85520.f90 @@ -0,0 +1,7 @@ +! { dg-do run } +! PR fortran/85520 +! Original code from Gerhard Steinmetz +program p + character(-huge(1)) :: c = ' ' + if (len(c) /= 0) stop 1 +end -- 2.30.2