Bug 105456 - Child I/O does not propage iostat
Summary: Child I/O does not propage iostat
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: 13.3
Assignee: Jerry DeLisle
URL:
Keywords: wrong-code
Depends on:
Blocks: F2003 105361
  Show dependency treegraph
 
Reported: 2022-05-02 16:49 UTC by Thomas Koenig
Modified: 2024-03-12 01:08 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-08-15 00:00:00


Attachments
Proposed partial patch (1.17 KB, patch)
2024-02-22 19:13 UTC, Jerry DeLisle
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Koenig 2022-05-02 16:49:08 UTC
The program

module sk1
  implicit none
  type char
     character :: ch
  end type char
  interface read (formatted)
     module procedure read_formatted
  end interface read (formatted)
contains
  subroutine read_formatted (dtv, unit, iotype, vlist, iostat, iomsg)
    class (char), intent(inout) :: dtv
    integer, intent(in) :: unit
    character (len=*), intent(in) :: iotype
    integer, intent(in) :: vlist(:)
    integer, intent(out) :: iostat
    character (len=*), intent(inout) :: iomsg
    character :: ch
    read (unit,fmt='(A1)', advance="no", iostat=iostat, iomsg=iomsg) ch
    iostat = 42
    dtv%ch = ch
  end subroutine read_formatted
end module sk1

program skip1
  use sk1
  implicit none
  type (char) :: x
  open (10,status="scratch")
  write (10,'(A)') '', 'a'
  rewind (10)
  read (10,*) x
  write (*,'(10(A))') "Read: '",x%ch,"'"
end program skip1

runs to completion, but it should propagate the iostat to the
caller and error out on the read.
Comment 1 Richard Biener 2023-04-26 06:56:00 UTC
GCC 13.1 is being released, retargeting bugs to GCC 13.2.
Comment 2 Richard Biener 2023-07-27 09:23:00 UTC
GCC 13.2 is being released, retargeting bugs to GCC 13.3.
Comment 3 Jerry DeLisle 2023-08-15 03:10:02 UTC
On my list.
Comment 4 Jerry DeLisle 2024-02-22 19:13:09 UTC
Created attachment 57504 [details]
Proposed partial patch

Proposed patch for the original test case with a READ function.
Comment 5 GCC Commits 2024-02-25 22:55:39 UTC
The master branch has been updated by Jerry DeLisle <jvdelisle@gcc.gnu.org>:

https://gcc.gnu.org/g:3f58f96a4e8255e222953f9856bcd6c25f7b33cd

commit r14-9168-g3f58f96a4e8255e222953f9856bcd6c25f7b33cd
Author: Jerry DeLisle <jvdelisle@gcc.gnu.org>
Date:   Sun Feb 25 14:50:07 2024 -0800

    libgfortran: Propagate user defined iostat and iomsg.
    
            PR libfortran/105456
    
    libgfortran/ChangeLog:
    
            * io/list_read.c (list_formatted_read_scalar): Add checks
            for the case where a user defines their own error codes
            and error messages and generate the runtime error.
            * io/transfer.c (st_read_done): Whitespace.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/pr105456.f90: New test.
Comment 6 GCC Commits 2024-03-06 04:54:26 UTC
The master branch has been updated by Jerry DeLisle <jvdelisle@gcc.gnu.org>:

https://gcc.gnu.org/g:21edfb0051ed8d0ff46d5638c2bce2dd71f26d1f

commit r14-9328-g21edfb0051ed8d0ff46d5638c2bce2dd71f26d1f
Author: Jerry DeLisle <jvdelisle@gcc.gnu.org>
Date:   Tue Mar 5 20:49:23 2024 -0800

    Fortran: Add user defined error messages for UDTIO.
    
    The defines IOMSG_LEN and MSGLEN were redundant so these are combined
    into IOMSG_LEN as defined in io.h.
    
    The remainder of the patch adds checks for when a user defined
    derived type IO procedure sets the IOSTAT or IOMSG variables
    independent of the librrary defined I/O messages.
    
            PR libfortran/105456
    
    libgfortran/ChangeLog:
    
            * io/io.h (IOMSG_LEN): Moved to here.
            * io/list_read.c (MSGLEN): Removed MSGLEN.
            (convert_integer): Changed MSGLEN to IOMSG_LEN.
            (parse_repeat): Likewise.
            (read_logical): Likewise.
            (read_integer): Likewise.
            (read_character): Likewise.
            (parse_real): Likewise.
            (read_complex): Likewise.
            (read_real): Likewise.
            (check_type): Likewise.
            (list_formatted_read_scalar): Adjust to IOMSG_LEN.
            (nml_read_obj): Add user defined error message.
            * io/transfer.c (unformatted_read): Add user defined error
            message.
            (unformatted_write): Add user defined error message.
            (formatted_transfer_scalar_read): Add user defined error message.
            (formatted_transfer_scalar_write): Add user defined error message.
            * io/write.c (list_formatted_write_scalar): Add user defined error message.
            (nml_write_obj): Add user defined error message.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/pr105456-nmlr.f90: New test.
            * gfortran.dg/pr105456-nmlw.f90: New test.
            * gfortran.dg/pr105456-ruf.f90: New test.
            * gfortran.dg/pr105456-wf.f90: New test.
            * gfortran.dg/pr105456-wuf.f90: New test.
Comment 7 GCC Commits 2024-03-07 03:58:08 UTC
The master branch has been updated by Jerry DeLisle <jvdelisle@gcc.gnu.org>:

https://gcc.gnu.org/g:03932d3203bce244edd812b81921c2f16ea18d86

commit r14-9348-g03932d3203bce244edd812b81921c2f16ea18d86
Author: Jerry DeLisle <jvdelisle@gcc.gnu.org>
Date:   Wed Mar 6 19:46:04 2024 -0800

    Fortran: Fix issue with using snprintf function.
    
    The previous patch used snprintf to set the message
    string. The message string is not a formatted string
    and the snprintf will interpret '%' related characters
    as format specifiers when there are no associated
    output variables. A segfault ensues.
    
    This change replaces snprintf with a fortran string copy
    function and null terminates the message string.
    
            PR libfortran/105456
    
    libgfortran/ChangeLog:
    
            * io/list_read.c (list_formatted_read_scalar): Use fstrcpy
            from libgfortran/runtime/string.c to replace snprintf.
            (nml_read_obj): Likewise.
            * io/transfer.c (unformatted_read): Likewise.
            (unformatted_write): Likewise.
            (formatted_transfer_scalar_read): Likewise.
            (formatted_transfer_scalar_write): Likewise.
            * io/write.c (list_formatted_write_scalar): Likewise.
            (nml_write_obj): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/pr105456.f90: Revise using '%' characters
            in users error message.
Comment 8 Jerry DeLisle 2024-03-12 01:08:24 UTC
Not planning any backport on this. Closing.