https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128833

            Bug ID: 128833
           Summary: Segfault in intrinsic assignment to a scalar pointer
                    when a component has a type-bound defined assignment
           Product: gcc
           Version: 16.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jvdelisle at gcc dot gnu.org
  Target Milestone: ---

This one shared to me on MatterMost. We have a patch.

This one segfaults:

module m
  implicit none
  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, pointer :: p => null()
  end type t
contains
  subroutine c_final (self)
    type(c), intent(inout) :: self
  end subroutine c_final
  subroutine c_assign (to, from)
    class(c), intent(out) :: to
    class(c), intent(in)  :: from
    to%i = from%i
  end subroutine c_assign
end module m

program p
  use m
  implicit none
  type(t), pointer :: q
  allocate (q)
  q = t (c (1))
  if (q%comp%i /= 1) stop 1
end program p

$ gfortran -g pr.f90 && ./a.out
Program received signal SIGSEGV: Segmentation fault - invalid memory
reference.
... at pr.f90:30          (the assignment q = t (c (1)))

Each of these makes it pass: an ALLOCATABLE instead of POINTER q; no
pointer component in t; no FINAL procedure for c.  Without the FINAL
procedure it still fails if the first argument of c_assign is
INTENT(INOUT) instead of INTENT(OUT).

Cause: generate_component_assignments in resolve.cc copies the lhs into
a temporary when the defined assignment's first argument is INTENT(INOUT),
or is INTENT(OUT) and finalizable.  get_temp_from_expr gives that
temporary the attributes of the lhs, so for a pointer q it is a pointer
(-fdump-tree-original shows "struct t * _F.DA3;") that is never
allocated, and the copy writes through a null pointer.

Reply via email to