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

            Bug ID: 100337
           Summary: Should be able to pass non-present optional arguments
                    to CO_BROADCAST
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: everythingfunctional at protonmail dot com
  Target Milestone: ---

Attempting to write a CO_BROADCAST wrapper for a derived type, I found that
passing optional arguments to CO_BROADCAST caused a segfault if the arguments
were not present. This should be allowed. See the following minimal example

module iso_varying_string
    implicit none

    type :: varying_string
        character(len=1), allocatable :: characters(:)
    contains
        procedure :: co_broadcast => co_broadcast_varying_string
    end type
contains
    subroutine co_broadcast_varying_string(a, source_image, stat, errmsg)
        class(varying_string), intent(inout) :: a
        integer, intent(in) :: source_image
        integer, intent(out), optional :: stat
        character(len=*), intent(inout), optional :: errmsg

        integer :: string_length

        if (this_image() == source_image) string_length = size(a%characters)

        call co_broadcast(string_length, source_image, stat, errmsg)

        if (present(stat)) then
            if (stat /= 0) return
        end if

        if (this_image() /= source_image) then
            allocate(a%characters(string_length))
        end if

        call co_broadcast(a%characters, source_image, stat, errmsg)
    end subroutine
end module

program main
    use iso_varying_string, only: varying_string

    implicit none

    character(len=*), parameter :: MESSAGE = "Hello, World!"
    integer :: i
    type(varying_string) :: the_string

    if (this_image() == 1) the_string%characters = [(MESSAGE(i:i), i = 1,
len(MESSAGE))]
    call the_string%co_broadcast(1)
    print *, the_string%characters
end program

Compiling and executing with gfortran produces the following:

$ gfortran -fcoarray=single -g example.f90
$ ./a.out                                 

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

Backtrace for this error:
#0  0x7fed85487d01 in ???
#1  0x7fed85486ed5 in ???
#2  0x7fed852be94f in ???
#3  0x55cd33d21683 in __iso_varying_string_MOD_co_broadcast_varying_string
        at /home/brad/tmp/co_broadcast_optional/example.f90:20
#4  0x55cd33d21872 in MAIN__
        at /home/brad/tmp/co_broadcast_optional/example.f90:43
#5  0x55cd33d21915 in main
        at /home/brad/tmp/co_broadcast_optional/example.f90:35
zsh: segmentation fault (core dumped)  ./a.out

Note that using opencoarrays this program works, as follows:

$ caf example.f90                         
$ cafrun -n 2 ./a.out                     
 Hello, World!
 Hello, World!

Reply via email to