https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96122
Bug ID: 96122 Summary: Segfault when using finalizer Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: abensonca at gcc dot gnu.org Target Milestone: --- The following code segfaults with gfortran 11.0.0: module a type vs character(len=1), dimension(:), allocatable :: ch contains final :: vsf end type vs type mtd type(vs), dimension(2) :: n end type mtd contains subroutine c() type(mtd) :: m allocate(m%n(1)%ch(1)) m%n(1)%ch(1)="a" allocate(m%n(2)%ch(1)) m%n(2)%ch(1)="b" return end subroutine c subroutine vsf(s) type(vs), intent(inout) :: s if (allocated(s%ch)) deallocate(s%ch) return end subroutine vsf end module a program b use a call c() end program b $ gfortran -v Using built-in specs. COLLECT_GCC=gfortran COLLECT_LTO_WRAPPER=/home/abenson/Galacticus/Tools/libexec/gcc/x86_64-pc-linux-gnu/11.0.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../gcc-git/configure --prefix=/home/abenson/Galacticus/Tools --enable-languages=c,c++,fortran --disable-multilib Thread model: posix Supported LTO compression algorithms: zlib gcc version 11.0.0 20200705 (experimental) (GCC) $ gfortran tmp.F90 -g $ ./a.out Program received signal SIGSEGV: Segmentation fault - invalid memory reference. Backtrace for this error: #0 0x7f47668d61ef in ??? at /data001/abenson/Galacticus/Tools/glibc-2.12.1/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0 #1 0x40118f in __a_MOD___final_a_Vs at /home/abenson/Galacticus/Nightlies/galacticus/tmp.F90:30 #2 0x400bfa in __a_MOD___final_a_Mtd at /home/abenson/Galacticus/Nightlies/galacticus/tmp.F90:30 #3 0x401556 in __a_MOD_c at /home/abenson/Galacticus/Nightlies/galacticus/tmp.F90:22 #4 0x401562 in b at /home/abenson/Galacticus/Nightlies/galacticus/tmp.F90:34 #5 0x401599 in main at /home/abenson/Galacticus/Nightlies/galacticus/tmp.F90:33 Segmentation fault The problem doesn't occur if the finalizer for type "vs" isn't used. It also doesn't occur if the "n" component of type "mtd" has dimension(1) or is a scalar. The finalizer in this case is pointless (since it just deallocates an allocatable, which would happen automatically if no finalizer were used), but as far as I can see is legal code. This appears to be a regression (worked in 10.1) but I haven't yet tracked down exactly where the problem was introduced.