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

            Bug ID: 86576
           Summary: [F03][OOP] Sourced allocation of object array fails
                    with SEGFAULT
           Product: gcc
           Version: 8.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: c...@mnet-mail.de
  Target Milestone: ---

The following Fortran 2003 test code, that makes extensive use of sourced
allocation, should build and allocate an array of objects, and then simply
terminate gracefully.

It indeed does so when compiled with flang 6.0, pgfortran 18.4, and ifort 19 
Beta. However, it leads to a segmentation fault when compiled with gfortran,
as follows:

$ cat test.F90
module build

   implicit none

   private
   public :: Builder, Otype

   type :: Ftype
      procedure(proc), pointer :: ptr => null()
   end type Ftype

   abstract interface
      subroutine proc( self,arr1,arr2 )
         import :: Ftype
         class(Ftype), intent(in) :: self
         real(8), dimension(:,:), intent(in)  :: arr1
         real(8), dimension(:,:), intent(out) :: arr2
      end subroutine proc
   end interface

   type :: Gtype
      type(Ftype) :: ff
   end type Gtype

   type :: Otype
      class(Gtype), allocatable :: og
   end type Otype

   type :: Builder
      class(Otype), dimension(:), allocatable :: outarr
   contains
      procedure :: init
      procedure :: get_result
   end type Builder

   interface Builder
      procedure constructor
   end interface Builder

contains

   function constructor( nd )
      !
      ! Constructor for Builder objects. 
      !
      type(Builder) :: constructor
      integer(4), intent(in) :: nd
      call constructor%init( nd )
   end function constructor


   subroutine init( self,nd )
      !
      ! Initializes the Builder. Constructs an array of output objects.
      !
      class(Builder), intent(inout) :: self
      integer(4),     intent(in) :: nd

      integer(4) :: i
      class(Ftype), allocatable :: ff

      ! allocate space for output array
      allocate( self%outarr(nd) )

      ! fill it with some values
      allocate( ff, source = Ftype() )      
      do i = 1, nd
         allocate( self%outarr(i)%og, source = Gtype( ff ) )
      end do

   end subroutine init   

   function get_result( self ) result( outarr )
      !
      ! Returns a copy of the array of output objects.
      !
      class(Builder), intent(in) :: self
      class(Otype), dimension(:), allocatable :: outarr
      allocate( outarr, source = self%outarr )
   end function get_result

end module build


program test

   use build, only: Builder, Otype

   implicit none

   integer(4), parameter :: nd = 2

   class(Builder), allocatable :: bld
   class(Otype), dimension(:), allocatable :: outarr

   ! get a builder object
   allocate( bld, source = Builder(nd) )

   ! return a copy of the array of output objects
   allocate( outarr, source = bld%get_result() )

end program test

$ gfortran-8 test.F90 -o test; ./test 

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

Backtrace for this error:
#0  0x2b08a615441a
#1  0x2b08a6153603
#2  0x2b08a65e74af
#3  0x40224f
#4  0x402438
#5  0x2b08a65d282f
#6  0x400768
#7  0xffffffffffffffff
Segmentation fault


Gfortran version output is:

$ gfortran-8 -v
Using built-in specs.
COLLECT_GCC=gfortran-8
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
8.1.0-5ubuntu1~16.04' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr
--with-gcc-major-version-only --program-suffix=-8
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib
--with-target-system-zlib --enable-objc-gc=auto --enable-multiarch
--disable-werror --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 8.1.0 (Ubuntu 8.1.0-5ubuntu1~16.04)

Reply via email to