Bug ID: 82996
           Summary: ICE and segfault with derived type finalization
           Product: gcc
           Version: 6.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot
          Reporter: neil.n.carlson at gmail dot com
  Target Milestone: ---

I'm going to give 3 examples. The first gives a spurious run time segfault. The
others are attempts to work around the problem, but give an internal compiler
error.  These all work fine with the Intel and NAG compilers.

The first example:

module mod

  type foo
    integer, pointer :: f(:) => null()
    final :: foo_destroy
  end type

  type bar
    type(foo) :: b(2)
  end type


  elemental subroutine foo_destroy(this)
    type(foo), intent(inout) :: this
    if (associated(this%f)) deallocate(this%f)
  end subroutine

end module

program main

  use mod
  type(bar) :: x
  call sub(x)


  subroutine sub(x)
    type(bar), intent(out) :: x
  end subroutine

end program

And the output from running the executable:

$ gfortran -g gfortran-bug-20171114a.f90 
$ ./a.out

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7f1188b42df7 in ???
#1  0x7f1188b4202d in ???
#2  0x7f118803694f in ???
#3  0x400fa7 in __mod_MOD_foo_destroy
        at /home/nnc/Fortran/Bugs/gfortran/tmp/gfortran-bug-20171114a.f90:46
#4  0x400f0f in __mod_MOD___final_mod_Foo
        at /home/nnc/Fortran/Bugs/gfortran/tmp/gfortran-bug-20171114a.f90:49
#5  0x400b29 in __mod_MOD___final_mod_Bar
        at /home/nnc/Fortran/Bugs/gfortran/tmp/gfortran-bug-20171114a.f90:49
#6  0x401026 in sub
        at /home/nnc/Fortran/Bugs/gfortran/tmp/gfortran-bug-20171114a.f90:59
#7  0x40104a in MAIN__
        at /home/nnc/Fortran/Bugs/gfortran/tmp/gfortran-bug-20171114a.f90:55
#8  0x401080 in main
        at /home/nnc/Fortran/Bugs/gfortran/tmp/gfortran-bug-20171114a.f90:53
Segmentation fault (core dumped)

Reply via email to