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

            Bug ID: 123906
           Summary: INQUIRE w/IOLENGTH can return bad value with character
                    strings
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: w6ws at earthlink dot net
  Target Milestone: ---

This example has a character string of length 5, followed by a numeric
variable, in the midst of the record.  The INQUIRE statement is apparently not
rounding up to the next 4-byte boundary - so the record length returned is too
small.  Later when performing a direct access WRITE, the program aborts.

program daio5
  implicit none

  integer, parameter :: unit_no = 24
  integer :: i, i1, iolen
  logical :: l, pf
  character(5) :: s
  real :: r
  complex :: c
  complex, parameter :: cval = (12.34, 56.78)

  inquire (iolength=iolen) l, s, i, r
  print *, 'recl =', iolen

  open (unit_no, file='fort.24', access='direct', recl=iolen)

! Write some records
  do, i=1, 10
    l = mod (i,2) == 0
    write (unit_no, rec=i) l, '12345', i, i+0.5, cval
  end do

! Read them back and check for validity
  do, i=1, 10
    l = .false.; s = 'uini'; i1 = -42; r = -42.42
    read (unit_no, rec=i) l, s, i1, r, c
    pf = (l .eqv. mod (i, 2) == 0) .and. &
         (s == '12345') .and.  &
         (i == i1) .and.  &
         (abs (r - (i + 0.5)) < 0.0001) .and.  &
         (c == cval)
    print *, 'l, s, i1, r =', l, s, i1, r, c
    print *, 'read', i, merge ('pass', 'fail', pf)
  end do

  close (unit_no)

end program
$ gfortran -g daio5.f90
$ rm fort.24
$ ./a.out
 recl =          17
At line 20 of file daio5.f90 (unit = 24, file = 'fort.24')
Fortran runtime error: Write exceeds length of DIRECT access record

Error termination. Backtrace:
#0  0x401538 in daio5
        at /home/wws/computer/fortran/lfortran/wws/daio5.f90:20
#1  0x40195c in main
        at /home/wws/computer/fortran/lfortran/wws/daio5.f90:38
$ od -c fort.24
0000000  \0  \0  \0  \0   1   2   3   4   5 001  \0  \0  \0  \0  \0 300
0000020   ?
0000021
$ gfortran --version
GNU Fortran (GCC) 16.0.0 20251209 (experimental)
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$

Reply via email to