Bug 16597 - gfortran: bug in unformatted I/O on scratch files
Summary: gfortran: bug in unformatted I/O on scratch files
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2004-07-16 21:07 UTC by Harald Anlauf
Modified: 2004-08-27 08:39 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-07-17 00:58:38


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Harald Anlauf 2004-07-16 21:07:22 UTC
Hi all,

there's apparently a bug in the runtime when handling unformatted I/O on scratch
files.  It reads back garbage after writing.

Sample program:

      program gfbug4
      implicit none

      integer      strlen
      parameter    (strlen = 4)

      integer      iunit, i, k
      character    string *4

      do 10 i = 1, strlen
         string(i:i) = achar (i)
 10   continue

      iunit = 99
      open (UNIT=iunit,FORM='unformatted',ACCESS='direct',RECL=strlen)
      write (iunit, rec=1) string
      read (iunit, rec=1) i
      close (iunit)
      print *, i

      open (UNIT=iunit,FORM='unformatted',ACCESS='direct',
     $      STATUS='scratch',RECL=strlen)
      write (iunit, rec=1) string
      read (iunit, rec=1) k
      close (iunit)
      print *, k

      if (i .eq. k) then
         print *, 'O.K.'
      else
         print *, '*** Bug! ***'
      endif

      end

Result:

% ./a.out 
    67305985
   538976288
 *** Bug! ***


The first write to fort.99 and reading back works fine - it indicates that
I/O has little-endian form, but the second is just 0x20202020.
Comment 1 Bud Davis 2004-07-17 00:58:38 UTC
it is a run-time library bug.

--bud davis
Comment 2 Bud Davis 2004-08-26 02:34:06 UTC
proposed patch:
http://gcc.gnu.org/ml/fortran/2004-08/msg00225.html
Comment 3 GCC Commits 2004-08-27 07:59:33 UTC
Subject: Bug 16597

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	bdavis@gcc.gnu.org	2004-08-27 07:59:30

Modified files:
	gcc/testsuite  : ChangeLog 
	libgfortran    : ChangeLog 
	libgfortran/io : transfer.c io.h rewind.c 
Added files:
	gcc/testsuite/gfortran.dg: pr16597.f90 

Log message:
	2004-08-27  Bud Davis  <bdavis9659@comcast.net>
	
	PR fortran/16597
	* io/io.h: created typedef for unit_mode.
	* io/io.h (gfc_unit): added mode to unit structure.
	* io/transfer.c (data_transfer_init): flush if a write then
	read is done on a unit (direct access files).
	* io/rewind.c (st_rewind): Used unit mode instead of global.
	
	* gfortran.dg/pr16597.f90: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.4201&r2=1.4202
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/pr16597.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/ChangeLog.diff?cvsroot=gcc&r1=1.67&r2=1.68
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/transfer.c.diff?cvsroot=gcc&r1=1.12&r2=1.13
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/io.h.diff?cvsroot=gcc&r1=1.8&r2=1.9
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/rewind.c.diff?cvsroot=gcc&r1=1.3&r2=1.4

Comment 4 Andrew Pinski 2004-08-27 08:39:51 UTC
Fixed.