Hi all, hi Harald, attached patch fixes a runtime out-of-bounds error when an iterator of an implied do-loop was not substituted as expected. The resulting code would still use the uninitialized iterator variable which then lead to out-of-bounds accesses.
Harald, I did not look at the patch you put into the PR and reported to have got stuck on just to not suffer from the same fate. Unfortunately I did just the same with the same insufficient result. It took me a lot of head scratching to come up with the this solution. Please have an in-depth look into it. Regtests fine on x86_64-pc-linux-gnu / F41. Ok for mainline? Regards, Andre -- Andre Vehreschild * Email: vehre ad gmx dot de
From faabad5c90b7505b7e118ff3fa1dcbfb4ff4d428 Mon Sep 17 00:00:00 2001 From: Andre Vehreschild <ve...@gcc.gnu.org> Date: Fri, 27 Jun 2025 11:42:54 +0200 Subject: [PATCH] Fortran: Allow for iterator substitution in array constructors [PR119106] PR fortran/119106 gcc/fortran/ChangeLog: * expr.cc (simplify_constructor): Do not simplify constants. (gfc_simplify_expr): Continue to simplify expression when an iterator is present. gcc/testsuite/ChangeLog: * gfortran.dg/array_constructor_58.f90: New test. --- gcc/fortran/expr.cc | 5 +++-- .../gfortran.dg/array_constructor_58.f90 | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/array_constructor_58.f90 diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc index b0495b7733e..b8d04ff6f36 100644 --- a/gcc/fortran/expr.cc +++ b/gcc/fortran/expr.cc @@ -1372,7 +1372,7 @@ simplify_constructor (gfc_constructor_base base, int type) || !gfc_simplify_expr (c->iterator->step, type))) return false; - if (c->expr) + if (c->expr && c->expr->expr_type != EXPR_CONSTANT) { /* Try and simplify a copy. Replace the original if successful but keep going through the constructor at all costs. Not @@ -2469,7 +2469,8 @@ gfc_simplify_expr (gfc_expr *p, int type) { if (!simplify_parameter_variable (p, type)) return false; - break; + if (!iter_stack) + break; } if (type == 1) diff --git a/gcc/testsuite/gfortran.dg/array_constructor_58.f90 b/gcc/testsuite/gfortran.dg/array_constructor_58.f90 new file mode 100644 index 00000000000..1473be0da01 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/array_constructor_58.f90 @@ -0,0 +1,17 @@ +!{ dg-do run } + +! Contributed by Federico Perini <federico.per...@gmail.com> +! Check that PR fortran/119106 is fixed. + +program char_param_array +implicit none +character, parameter :: p(5) = ['1','2','3','4','5'] +character, save :: n(5) = ['1','2','3','4','5'] +integer :: i(10), j + +i = 4 +if (any([(n(i(j)),j=1,10)] /= '4')) stop 1 ! OK +if (any([(p(i(j)),j=1,10)] /= '4')) stop 2 ! used to runtime out-of-bounds error + +end program char_param_array + -- 2.50.1