*PING* [PATCH] PR fortran/82314 - ICE in gfc_conv_expr_descriptor, at fortran/trans-array.c:6972

2021-09-12 Thread Harald Anlauf via Fortran
Early *PING*.

> Gesendet: Dienstag, 07. September 2021 um 23:44 Uhr
> Von: "Harald Anlauf" 
> An: "fortran" , "gcc-patches" 
> Betreff: [PATCH] PR fortran/82314 - ICE in gfc_conv_expr_descriptor, at 
> fortran/trans-array.c:6972
>
> When adding the initializer for an array, we need to make sure that
> array bounds are properly simplified if that array is a PARAMETER.
> Otherwise the generated initializer could be wrong and screw up
> subsequent simplifications, see PR.
>
> The minimal solution is to attempt simplification of array bounds
> before adding the initializer as in the attached patch.  (We could
> place that part in a helper function if this functionality is
> considered useful elsewhere).
>
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>
> Thanks,
> Harald
>
>
> Fortran - ensure simplification of bounds of array-valued named constants
>
> gcc/fortran/ChangeLog:
>
>   PR fortran/82314
>   * decl.c (add_init_expr_to_sym): For proper initialization of
>   array-valued named constants the array bounds need to be
>   simplified before adding the initializer.
>
> gcc/testsuite/ChangeLog:
>
>   PR fortran/82314
>   * gfortran.dg/pr82314.f90: New test.
>
>


[PATCH] PR fortran/85130 - Handling of substring range

2021-09-12 Thread Harald Anlauf via Fortran
Dear all,

in find_substring_ref we erroneously handled given substring start and end
indices as unsigned integers.  However, gives indices could be negative,
which is legal as long as end < start, leading to a string of length zero.
The current behavior could lead to a wrong length as well as an invalid read
from (compiler) memory.

The fix allows to reintroduce code in testcase substr_6.f90 that was
erroneously considered as illegal.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

As this is invalid code, I'd like to backport this fix.

Thanks,
Harald


Fortran - fix handling of substring start and end indices

gcc/fortran/ChangeLog:

PR fortran/85130
* expr.c (find_substring_ref): Handle given substring start and
end indices as signed integers, not unsigned.

gcc/testsuite/ChangeLog:

PR fortran/85130
* gfortran.dg/substr_6.f90: Revert commit r8-7574, adding again
test that was erroneously considered as illegal.

diff --git a/gcc/fortran/expr.c b/gcc/fortran/expr.c
index dfecc3012e1..604e63e6164 100644
--- a/gcc/fortran/expr.c
+++ b/gcc/fortran/expr.c
@@ -1724,8 +1724,8 @@ find_substring_ref (gfc_expr *p, gfc_expr **newp)
   *newp = gfc_copy_expr (p);
   free ((*newp)->value.character.string);

-  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);
+  end = (gfc_charlen_t) mpz_get_si (p->ref->u.ss.end->value.integer);
+  start = (gfc_charlen_t) mpz_get_si (p->ref->u.ss.start->value.integer);
   if (end >= start)
 length = end - start + 1;
   else
diff --git a/gcc/testsuite/gfortran.dg/substr_6.f90 b/gcc/testsuite/gfortran.dg/substr_6.f90
index 0d5e3d75e88..83e788a55a6 100644
--- a/gcc/testsuite/gfortran.dg/substr_6.f90
+++ b/gcc/testsuite/gfortran.dg/substr_6.f90
@@ -6,6 +6,8 @@ CHARACTER(5), parameter :: c0(1) = (/ "123" // ACHAR(0) // "5" /)
 CHARACTER*5 c(1)
 CHARACTER(1), parameter :: c1(5) = (/ "1", "2", "3", ACHAR(0), "5" /)

+c = c0(1)(-5:-8)
+if (c(1) /= " ") STOP 1
 c = (/ c0(1)(1:5) /)
 do i=1,5
if (c(1)(i:i) /= c1(i)) STOP 2