https://gcc.gnu.org/g:58786428fb320e235dfe0c60e2a40941f96f08e6

commit r17-6563-g58786428fb320e235dfe0c60e2a40941f96f08e6
Author: Jerry DeLisle <[email protected]>
Date:   Tue Nov 17 17:09:34 2026 -0800

    fortran: [PR128833] Segfault in derived-type assignment to a pointer
    
    When a component has a defined assignment whose first argument is
    INTENT(INOUT), or INTENT(OUT) and finalizable,
    generate_component_assignments first copies the lhs to a temporary.
    With a scalar pointer lhs the temporary became a pointer too, which
    nothing allocated, so the copy wrote through a null pointer.  Do not
    give a scalar temporary the POINTER attribute.
    
    Assisted-by: Claude Opus 5.5
    
            PR fortran/128833
    
    gcc/fortran/ChangeLog:
    
            * resolve.cc (generate_component_assignments): Do not give a scalar
            temporary for the lhs the POINTER attribute.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/defined_assignment_pointer_lhs_1.f90: New test.

Diff:
---
 gcc/fortran/resolve.cc                             |  3 ++
 .../defined_assignment_pointer_lhs_1.f90           | 52 ++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 6b05f6a6a855..283231a8e650 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -14339,6 +14339,9 @@ generate_component_assignments (gfc_code **code, 
gfc_namespace *ns)
                    tmp_ns = (*code)->expr1->symtree->n.sym->ns;
                  t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
                  t1->symtree->n.sym->attr.artificial = 1;
+                 /* The temporary is a copy of the lhs, not a pointer.  */
+                 if (t1->rank == 0)
+                   t1->symtree->n.sym->attr.pointer = 0;
                  temp_code = build_assignment (EXEC_ASSIGN,
                                                t1, (*code)->expr1,
                                NULL, NULL, (*code)->loc);
diff --git a/gcc/testsuite/gfortran.dg/defined_assignment_pointer_lhs_1.f90 
b/gcc/testsuite/gfortran.dg/defined_assignment_pointer_lhs_1.f90
new file mode 100644
index 000000000000..1004312b8464
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/defined_assignment_pointer_lhs_1.f90
@@ -0,0 +1,52 @@
+! { dg-do run }
+!
+! PR 128833
+module m
+  implicit none
+  integer :: n_assign = 0, n_final = 0
+  type :: c
+     integer :: i = 0
+   contains
+     final     :: c_final
+     procedure :: c_assign
+     generic   :: assignment(=) => c_assign
+  end type c
+  type :: t
+     type(c)          :: comp
+     integer          :: j = 0
+     integer, pointer :: p => null()
+  end type t
+contains
+  subroutine c_final (self)
+    type(c), intent(inout) :: self
+    n_final = n_final + 1
+  end subroutine c_final
+  subroutine c_assign (to, from)
+    class(c), intent(out) :: to
+    class(c), intent(in)  :: from
+    n_assign = n_assign + 1
+    to%i = from%i
+  end subroutine c_assign
+end module m
+
+program p
+  use m
+  implicit none
+  type(t), pointer :: q
+  type(t), target  :: r
+  integer :: n_assign_ref, n_final_ref
+
+  ! Reference: the same assignment to a variable that is not a pointer.
+  r = t (c (1), 2)
+  if (r%comp%i /= 1 .or. r%j /= 2) stop 1
+  n_assign_ref = n_assign
+  n_final_ref = n_final
+
+  n_assign = 0
+  n_final = 0
+  allocate (q)
+  q = t (c (1), 2)
+  if (q%comp%i /= 1 .or. q%j /= 2) stop 2
+  if (n_assign /= n_assign_ref .or. n_final /= n_final_ref) stop 3
+  deallocate (q)
+end program p

Reply via email to