Am 18.12.24 um 19:09 schrieb Jerry D:
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.

Since the standard defines the argument as optional, we have to
handle it.  This is similar to e.g. the SIZE intrinsic:

program p
  implicit none
  integer :: a(3) = [1,2,3]
  integer :: dim = 1
  call check_size (dim)
  call check_size ()
contains
  subroutine check_size (dim)
    integer, optional, intent(in) :: dim
    print *, size (a, dim=dim)
  end subroutine check_size
end

Also other intrinsics (e.g. minloc/maxloc/lbound/ubound/...)
need to do it right (and I think/hope they do).

Cheers,
Harald

Regards,

Jerry



Reply via email to