Skip to content

[flang] Runtime failure at the READ statement of list-directed I/O with an array input item. #149885

@DanielCChen

Description

@DanielCChen

Consider the following code:

module m
    type dataType
        complex, allocatable :: cx
    end type

    type base
        character(20), pointer :: name => null()
        real, allocatable :: data
        class(dataType), allocatable :: d2
    end type

    interface read(formatted)
        procedure readBaseFmtd
        procedure readDataTypeFmtd
    end interface


    contains

    integer function currentPos (s)
        character(*), intent(in) :: s

        currentPos = len (trim(s)) + 1
    end function

    subroutine readDataTypeFmtd (dtv, unit, iotype, v_list, iostat, iomsg)
        class(dataType), intent(inout) :: dtv
        integer, intent(in) :: unit
        character(*), intent(in) :: iotype
        integer, intent(in) :: v_list(:)
        integer, intent(out) :: iostat
        character(*), intent(inout) :: iomsg

        if (.not. allocated(dtv%cx)) allocate (dtv%cx)

        read(unit, *, iostat=iostat, iomsg=iomsg) dtv%cx
    end subroutine

    subroutine readBaseFmtd (dtv, unit, iotype, v_list, iostat, iomsg)
        class(base), intent(inout) :: dtv
        integer, intent(in) :: unit
        character(*), intent(in) :: iotype
        integer, intent(in) :: v_list(:)
        integer, intent(out) :: iostat
        character(*), intent(inout) :: iomsg

        if (.not. associated(dtv%name)) allocate (dtv%name)

        read (unit, *, iostat=iostat, iomsg=iomsg) dtv%name

        if (iostat /= 0) error stop 1

        if (.not. allocated(dtv%data)) allocate (dtv%data)

        read (unit, *, iostat=iostat, iomsg=iomsg) dtv%data

        if (iostat /= 0) then
          print*, iomsg
          error stop 2
        end if

        if (.not. allocated(dtv%d2)) allocate(dtv%d2)

        read (unit, *, iostat=iostat, decimal='COMMA', iomsg=iomsg) dtv%d2

        if (iostat /= 0) error stop 3

    end subroutine
end module

program modeInquire002
use m
    class(base), allocatable :: b1(:)
    integer :: j = 3                                   !! If j is less than 3 or b1 is a scalar, the code works fine.


    open (1, file='modeInquire002.data')

    write (1, &
        fmt='("''", a, "'' ",dp,e15.8, " (", dc, e15.8," ; ",e15.8," ) ")', &
            sign='plus') &
            ('xlftest '//achar(iachar('0') + i-1), i*1.2, &
                cmplx(sin(i*1.1), cos(i*1.1), 4), i=1,j)

    rewind 1

    allocate (b1(j))

    read (1,*) b1

end

Flang failed at the runtime with:

> a.out
 Bad real input data at column 64 of record 1

Fortran ERROR STOP: code 2

IEEE arithmetic exceptions signaled: INEXACT

If b1 is a scalar or has less than 3 elements, it works fine.

Both gfortran and XLF compile and executes the code successfully.

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions