On 11/6/26 3:12 PM, Jerry D wrote:
The following attached patch regression tested on x86_64 and arm.

OK for mainline?

Regards,

Jerry


I did some additional checking while working on another PR and discovered I missed a dependency. Attached here is a v2 of the patch which now includes its own CLASS copy helper and no longer depends on PR51284 or PR128017-3 which I am working on.

Regression tested on x86_64.

Ok for mainline?

Jerry


---

[PATCH] fortran: [PR128478] Copy a parenthesized scalar CLASS actual argument

A parenthesized scalar CLASS variable passed as an actual argument was
not copied, so the dummy aliased the variable, and passing it to a TYPE
dummy with allocatable components segfaulted, because the components of
the CLASS container were copied and freed as if it were the derived
type.  Make the value of a parenthesized scalar CLASS variable a copy in
gfc_conv_expr_op, which also serves the ASSOCIATE selector, keep the
free of the copy until after the call, and leave its components to the
copy's own finalization.

Assisted-by: Claude Opus 5.5

     PR fortran/128478

gcc/fortran/ChangeLog:

     * trans-expr.cc (gfc_conv_expr_op): Copy a parenthesized scalar
     CLASS variable.
     (gfc_conv_procedure_call): Do not copy or free the allocatable
     components of the copy of a parenthesized scalar CLASS variable.
     (gfc_conv_expr_reference): Keep the free of that copy in se->post.
     * trans-stmt.cc (trans_associate_var): Remove the copy of a
     parenthesized CLASS selector, now done by gfc_conv_expr_op.

gcc/testsuite/ChangeLog:

     * gfortran.dg/asan/paren_class_copy_1.f90: New test.
---
From 0bbe6fa3c0e438486177a017416356b4fcf2d2fd Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Fri, 6 Nov 2026 17:00:22 -0800
Subject: [PATCH v2] fortran: [PR128478] Copy a parenthesized scalar CLASS
 actual argument

A parenthesized scalar CLASS variable passed as an actual argument was
not copied, so the dummy aliased the variable, and passing it to a TYPE
dummy with allocatable components segfaulted, because the components of
the CLASS container were copied and freed as if it were the derived
type.  Make the value of a parenthesized scalar CLASS variable a copy in
gfc_conv_expr_op, made through the vtable and finalized after its use,
keep the free of the copy until after the call, and leave its
components to the copy's own finalization.

Assisted-by: Claude Opus 5.5

	PR fortran/128478

gcc/fortran/ChangeLog:

	* trans-expr.cc (conv_class_value_copy): New function.
	(gfc_conv_expr_op): Use it to copy a parenthesized scalar CLASS
	variable.
	(gfc_conv_procedure_call): Do not copy or free the allocatable
	components of the copy of a parenthesized scalar CLASS variable.
	(gfc_conv_expr_reference): Keep the free of that copy in se->post.

gcc/testsuite/ChangeLog:

	* gfortran.dg/asan/paren_class_copy_1.f90: New test.
---
 gcc/fortran/trans-expr.cc                     | 77 ++++++++++++++++++-
 .../gfortran.dg/asan/paren_class_copy_1.f90   | 74 ++++++++++++++++++
 2 files changed, 149 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/asan/paren_class_copy_1.f90

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 5b720cf4be0..f3761e41b34 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -4185,6 +4185,59 @@ parenthesized_variable (gfc_expr *e)
 }
 
 
+/* Replace the scalar class container in SE->EXPR, or its address, by a
+   container of a copy of its data, which SE->POST finalizes and frees.  */
+
+static void
+conv_class_value_copy (gfc_se *se)
+{
+  tree from, copy, data, size, tmp;
+  stmtblock_t block;
+  symbol_attribute attr;
+  gfc_se fse;
+  bool unlimited;
+
+  from = se->expr;
+  if (POINTER_TYPE_P (TREE_TYPE (from)))
+    from = build_fold_indirect_ref_loc (input_location, from);
+  if (!DECL_P (from))
+    from = gfc_evaluate_now (from, &se->pre);
+  unlimited = !integer_zerop (gfc_class_len_or_zero_get (from));
+
+  /* The copy has the _vptr and _len of FROM.  */
+  copy = gfc_create_var (TREE_TYPE (from), "class_copy");
+  gfc_add_modify (&se->pre, copy, from);
+
+  /* Leave a null _data alone; its _vptr may not be valid.  */
+  gfc_init_block (&block);
+  size = fold_convert (size_type_node, gfc_class_vtab_size_get (from));
+  size = gfc_resize_class_size_with_len (&block, from, size);
+  data = gfc_class_data_get (copy);
+  gfc_add_modify (&block, data,
+		  gfc_call_malloc (&block, TREE_TYPE (data), size));
+  gfc_add_expr_to_block (&block, gfc_copy_class_to_class (from, copy,
+							  NULL_TREE,
+							  unlimited));
+  tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
+			 gfc_class_data_get (from),
+			 fold_convert (TREE_TYPE (data), null_pointer_node));
+  tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block),
+		  build_empty_stmt (input_location));
+  gfc_add_expr_to_block (&se->pre, tmp);
+
+  gfc_init_se (&fse, NULL);
+  fse.expr = copy;
+  gfc_clear_attr (&attr);
+  attr.allocatable = 1;
+  gfc_finalize_tree_expr (&fse, NULL, attr, 0);
+  gfc_add_block_to_block (&se->pre, &fse.pre);
+  gfc_add_block_to_block (&se->post, &fse.post);
+  gfc_add_block_to_block (&se->post, &fse.finalblock);
+
+  se->expr = copy;
+}
+
+
 /* Translates an op expression. Common (binary) cases are handled by this
    function, others are passed on. Recursion is used in either case.
    We use the fact that (op1.ts == op2.ts) (except for the power
@@ -4208,6 +4261,14 @@ gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
   switch (expr->value.op.op)
     {
     case INTRINSIC_PARENTHESES:
+      /* The value of a parenthesized CLASS variable is a copy.  */
+      if (expr->ts.type == BT_CLASS && expr->rank == 0
+	  && parenthesized_variable (expr))
+	{
+	  gfc_conv_expr (se, expr->value.op.op1);
+	  conv_class_value_copy (se);
+	  return;
+	}
       if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
 	  && flag_protect_parens)
 	{
@@ -8556,12 +8617,14 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
 	 procedures.  As bounds information for descriptorless arrays is no
 	 longer available here, they are dealt with in trans-array.cc
 	 (gfc_conv_array_parameter).  A derived-type temporary for a
-	 parenthesized variable shares the components of the variable.  */
+	 parenthesized variable shares the components of the variable, and
+	 the copy of a parenthesized scalar CLASS variable frees its own.  */
       if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
 	    && e->ts.u.derived->attr.alloc_comp
 	    && (e->rank == 0 || elemental_proc || !nodesc_arg)
 	    && !expr_may_alias_variables (e, elemental_proc)
-	    && !(e->ts.type == BT_DERIVED && parenthesized_variable (e)))
+	    && !((e->ts.type == BT_DERIVED || e->rank == 0)
+		 && parenthesized_variable (e)))
 	{
 	  int parm_rank;
 	  /* It is known the e returns a structure type with at least one
@@ -11383,6 +11446,16 @@ gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
       return;
     }
 
+  /* The copy of a parenthesized CLASS variable is freed in se->post, so it
+     must stay there until after the copy's use.  */
+  if (expr->ts.type == BT_CLASS && expr->rank == 0
+      && parenthesized_variable (expr))
+    {
+      gfc_conv_expr (se, expr);
+      se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
+      return;
+    }
+
   if (expr->expr_type == EXPR_FUNCTION
       && ((expr->value.function.esym
 	   && expr->value.function.esym->result
diff --git a/gcc/testsuite/gfortran.dg/asan/paren_class_copy_1.f90 b/gcc/testsuite/gfortran.dg/asan/paren_class_copy_1.f90
new file mode 100644
index 00000000000..2fd63364b62
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/asan/paren_class_copy_1.f90
@@ -0,0 +1,74 @@
+! { dg-do run }
+!
+! PR 128478
+! A parenthesized scalar CLASS variable passed as an actual argument is a
+! copy; without the fix the dummy aliased the variable, and a TYPE dummy
+! segfaulted.  Checked under -fsanitize=address.
+
+module m
+  implicit none
+  type :: t
+    integer, allocatable :: v(:)
+  end type
+contains
+  subroutine sq (x, z)
+    class(*), intent(in) :: x
+    complex, intent(inout) :: z
+    z = (0., 0.)
+    select type (x)
+    type is (complex)
+      if (x /= (5., 6.)) stop 1
+    class default
+      stop 2
+    end select
+  end subroutine
+  subroutine st (x, z)
+    class(t), intent(in) :: x
+    type(t), intent(inout) :: z
+    z%v = [0, 0]
+    if (any (x%v /= [5, 6])) stop 3
+  end subroutine
+  subroutine s1 (x)
+    type(t), intent(in) :: x
+    if (any (x%v /= [5, 6])) stop 6
+  end subroutine
+  subroutine sc (x, c)
+    class(*), intent(in) :: x
+    character(*), intent(inout) :: c
+    c = 'xyz'
+    select type (x)
+    type is (character(*))
+      if (len (x) /= 3 .or. x /= 'abc') stop 8
+    class default
+      stop 9
+    end select
+  end subroutine
+end module
+
+program p
+  use m
+  implicit none
+  complex, target :: zs = (5., 6.)
+  character(3), target :: cc = 'abc'
+  type(t), target :: ts
+  class(*), pointer :: q
+  class(t), pointer :: qt
+  class(t), allocatable :: cs
+
+  q => zs
+  call sq ((q), zs)
+  if (zs /= (0., 0.)) stop 4
+
+  q => cc
+  call sc ((q), cc)
+  if (cc /= 'xyz') stop 10
+
+  ts = t ([5, 6])
+  qt => ts
+  call st ((qt), ts)
+  if (any (ts%v /= 0)) stop 5
+
+  allocate (cs, source = t ([5, 6]))
+  call s1 ((cs))
+  if (any (cs%v /= [5, 6])) stop 7
+end program
-- 
2.56.0

Reply via email to