From 335d03387408e2ce580e59ace0a593d7d9e1950d Mon Sep 17 00:00:00 2001 From: Thomas Koenig Date: Sat, 24 Mar 2018 17:37:21 +0000 Subject: [PATCH] re PR fortran/70068 (ICE: out of memory on involving empty substring) 2018-03-24 Thomas Koenig PR fortran/70068 * expr.c (find_substring_ref): Change types of start, end and length variables to gfc_charlen_t. Set length to zero for empty substring. 2018-03-24 Thomas Koenig PR fortran/70068 * gfortran.dg/substr_7.f90: New test. From-SVN: r258835 --- gcc/fortran/ChangeLog | 7 +++++++ gcc/fortran/expr.c | 15 +++++++++------ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gfortran.dg/substr_7.f90 | 13 +++++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/substr_7.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 2cf32f3a415..12348f1674d 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,10 @@ +2018-03-24 Thomas Koenig + + PR fortran/70068 + * expr.c (find_substring_ref): Change types of start, end + and length variables to gfc_charlen_t. Set length to zero + for empty substring. + 2018-03-24 Steven G. Kargl PR fortran/42651 diff --git a/gcc/fortran/expr.c b/gcc/fortran/expr.c index bbaabb684c9..9e113d26a59 100644 --- a/gcc/fortran/expr.c +++ b/gcc/fortran/expr.c @@ -1662,9 +1662,9 @@ cleanup: static bool find_substring_ref (gfc_expr *p, gfc_expr **newp) { - int end; - int start; - int length; + gfc_charlen_t end; + gfc_charlen_t start; + gfc_charlen_t length; gfc_char_t *chr; if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT @@ -1674,9 +1674,12 @@ find_substring_ref (gfc_expr *p, gfc_expr **newp) *newp = gfc_copy_expr (p); free ((*newp)->value.character.string); - end = (int) mpz_get_ui (p->ref->u.ss.end->value.integer); - start = (int) mpz_get_ui (p->ref->u.ss.start->value.integer); - length = end - start + 1; + end = (gfc_charlen_t) mpz_get_ui (p->ref->u.ss.end->value.integer); + start = (gfc_charlen_t) mpz_get_ui (p->ref->u.ss.start->value.integer); + if (end >= start) + length = end - start + 1; + else + length = 0; chr = (*newp)->value.character.string = gfc_get_wide_string (length + 1); (*newp)->value.character.length = length; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 962711c2868..1e1f51566b8 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-03-24 Thomas Koenig + + PR fortran/70068 + * gfortran.dg/substr_7.f90: New test. + 2018-03-24 Steven G. Kargl PR fortran/42651 diff --git a/gcc/testsuite/gfortran.dg/substr_7.f90 b/gcc/testsuite/gfortran.dg/substr_7.f90 new file mode 100644 index 00000000000..3d8f2e1bd55 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/substr_7.f90 @@ -0,0 +1,13 @@ +! { dg-do run } +! PR 70068 - used to allocate too much memory +! Original test cases by Gerhard Steinmetz +program p + integer :: i + character(3), parameter :: x(3) = ['abc', 'ijk', 'xyz'] + character(3) :: y(2) + character(99), parameter :: x2(2) = ' ' + character(99), parameter :: y2=x(2)(99:1) + y = [(x(i)(i:1), i=2,3)] + if (any(y /= '')) stop 1 + if (y2 /= '') stop 2 +end -- 2.30.2