See attached patch. For a component with a defined assignment whose
first argument is INTENT(INOUT), or INTENT(OUT) and finalizable,
generate_component_assignments copies the lhs to a temporary first.
get_temp_from_expr takes the temporary's attributes from the lhs, so a
scalar pointer lhs produced a pointer temporary that nothing allocated,
and the copy wrote through a null pointer. The fix clears the POINTER
attribute on a scalar temporary; array temporaries are unchanged.
The fix is three lines in generate_component_assignments in
resolve.cc; the rest is the new test, which checks the pointer lhs
against the same assignment to a variable that is not a pointer.
Regression tested on x86_64.
I plan to commit this as simple and obvious.
Regards,
Jerry
---
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.
>From b8ae208bd467131660305e01d58ed843af988e1c Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 17 Nov 2026 17:09:34 -0800
Subject: [PATCH] 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.
---
gcc/fortran/resolve.cc | 3 ++
.../defined_assignment_pointer_lhs_1.f90 | 52 +++++++++++++++++++
2 files changed, 55 insertions(+)
create mode 100644 gcc/testsuite/gfortran.dg/defined_assignment_pointer_lhs_1.f90
diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index a89148b2ebe..fad430490c6 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -14340,6 +14340,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 00000000000..1004312b846
--- /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
--
2.56.1