On 12/18/24 4:11 AM, Harald Anlauf wrote:
Hi Steve,

thanks for the draft patch.

I haven't looked close enough, but you may have to add support
for 'asis' being an optional dummy variable.  The following
example crashes here with a segfault:

program foo
    use iso_c_binding, only : c_null_char, c_char, f_c_string, c_size_t
    implicit none

    logical asis
    character(len=6, kind=c_char) :: s1
    character(len=:, kind=c_char), allocatable :: s2

    interface
       !
       ! strlen() counts up to '\0', and excludes it from the count
       !
       function strlen(s) bind(c,name="strlen")
          import c_char, c_size_t
          integer(c_size_t) strlen
          character(len=1,kind=c_char), intent(in) :: s(*)
       end function strlen
    end interface

    s1 = 'abc   '
    asis = .true.
    call check (asis) ! OK
    asis = .false.
    call check (asis) ! OK
    call check ()     ! segfault

  contains

    subroutine check (asis)
      logical, optional, intent(in) :: asis

      s2 = f_c_string(s1, asis)
      print *, len_trim(s1), int(strlen(s2))

    end subroutine check

end program foo


--- snip ---

This is interesting. In my tests I tried:

   print *, len(s1), len(f_c_string(s1))

Which works fine. Is the user required to check for the presence of the optional argument before using it? From the 'view' of the subroutine 'asis' exists but is undefined.

Regards,

Jerry

Reply via email to