aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikael Morin <mikael@gcc.gnu.org>2022-01-28 22:00:57 +0100
committerMikael Morin <mikael@gcc.gnu.org>2022-04-10 18:40:10 +0200
commit3b0f715744d7919ee729d370155a68d5fa97cba9 (patch)
tree538f9922fa8add0644653cb8ac6b0fbbe38c78ad
parenta9c54c5ec762672d600d312336217ccaa7ffd8e4 (diff)
downloadgcc-3b0f715744d7919ee729d370155a68d5fa97cba9.zip
gcc-3b0f715744d7919ee729d370155a68d5fa97cba9.tar.gz
gcc-3b0f715744d7919ee729d370155a68d5fa97cba9.tar.bz2
fortran: Unshare associate var charlen [PR104228]
PR104228 showed that character lengths were shared between associate variable and associate targets. This is problematic when the associate target is itself a variable and gets a variable to hold the length, as the length variable is added (and all the variables following it in the chain) to both the associate variable scope and the target variable scope. This caused an ICE when compiling with -O0 -fsanitize=address. This change forces the creation of a separate character length for the associate variable. It also forces the initialization of the character length variable to avoid regressing associate_32 and associate_47 tests. PR fortran/104228 gcc/fortran/ChangeLog: * resolve.c (resolve_assoc_var): Also create a new character length for non-dummy associate targets. * trans-stmt.c (trans_associate_var): Initialize character length even if no temporary is used for the associate variable. gcc/testsuite/ChangeLog: * gfortran.dg/asan/associate_58.f90: New test. * gfortran.dg/asan/associate_59.f90: New test. (cherry picked from commit 57da34939703a6e6d3267a0d25d1fb9369d3ac0e)
-rw-r--r--gcc/fortran/resolve.c1
-rw-r--r--gcc/fortran/trans-stmt.c2
-rw-r--r--gcc/testsuite/gfortran.dg/asan/associate_58.f9019
-rw-r--r--gcc/testsuite/gfortran.dg/asan/associate_59.f9019
4 files changed, 39 insertions, 2 deletions
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 4edc0b6..b45f0bb 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -9213,7 +9213,6 @@ resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
sym->ts.u.cl = target->ts.u.cl;
if (sym->ts.deferred && target->expr_type == EXPR_VARIABLE
- && target->symtree->n.sym->attr.dummy
&& sym->ts.u.cl == target->ts.u.cl)
{
sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
diff --git a/gcc/fortran/trans-stmt.c b/gcc/fortran/trans-stmt.c
index d9562d7..0e387bb 100644
--- a/gcc/fortran/trans-stmt.c
+++ b/gcc/fortran/trans-stmt.c
@@ -1939,7 +1939,7 @@ trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block)
gfc_conv_expr_descriptor (&se, e);
if (sym->ts.type == BT_CHARACTER
- && !se.direct_byref && sym->ts.deferred
+ && sym->ts.deferred
&& !sym->attr.select_type_temporary
&& VAR_P (sym->ts.u.cl->backend_decl)
&& se.string_length != sym->ts.u.cl->backend_decl)
diff --git a/gcc/testsuite/gfortran.dg/asan/associate_58.f90 b/gcc/testsuite/gfortran.dg/asan/associate_58.f90
new file mode 100644
index 0000000..b5ea754
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/asan/associate_58.f90
@@ -0,0 +1,19 @@
+! { dg-do compile }
+! { dg-additional-options "-O0" }
+!
+! PR fortran/104228
+! The code generated code for the program below wrongly pushed the Y character
+! length variable to both P and S scope, which was leading to an ICE when
+! address sanitizer was in effect
+
+program p
+ character(:), save, allocatable :: x(:)
+ call s
+contains
+ subroutine s
+ associate (y => x)
+ y = [x]
+ end associate
+ end
+end
+
diff --git a/gcc/testsuite/gfortran.dg/asan/associate_59.f90 b/gcc/testsuite/gfortran.dg/asan/associate_59.f90
new file mode 100644
index 0000000..9bfb2bf
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/asan/associate_59.f90
@@ -0,0 +1,19 @@
+! { dg-do compile }
+! { dg-additional-options "-O0" }
+!
+! PR fortran/104228
+! The code generated code for the program below wrongly pushed the Y character
+! length variable to both P and S scope, which was leading to an ICE when
+! address sanitizer was in effect
+
+program p
+ character(:), allocatable :: x(:)
+ call s
+contains
+ subroutine s
+ associate (y => x)
+ y = [x]
+ end associate
+ end
+end
+