This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Bug in GNU Fortran 95 version 4.0.2
- From: Georgy Salnikov <sge at nmr dot nioch dot nsc dot ru>
- To: <fortran at gcc dot gnu dot org>
- Cc: <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 3 Nov 2005 18:12:34 +0600 (NOVT)
- Subject: Bug in GNU Fortran 95 version 4.0.2
Dear developers of the GNU Fortran 95,
Recently I ran into the following problem in the IO part of the libgfortran
library version gcc-4.0.2.
Here is a sample program which demonstrates the bug (given that the file
'TEST' does not yet exist):
PROGRAM TEST
CHARACTER*8 TXT
DATA TXT/'**TEST**'/
OPEN (1, FILE='TEST', STATUS='UNKNOWN', FORM='UNFORMATTED')
WRITE (1) TXT
REWIND 1
READ (1, END=1) TXT
READ (1, END=1) TXT
1 CONTINUE
BACKSPACE 1
WRITE (1) TXT
CLOSE (1, STATUS='KEEP')
END
A new file 'TEST' is created for sequential unformatted IO.
One record (8 bytes in this example) is written into it.
The file is rewinded to the beginning. At that point the file contains one
single record 8 bytes long.
The same record is read from the file. Now the file pointer sits just before
the end-of-file.
The same record is read once more. As there is no more records there, an
end-of-file situation is generated which leads in this example just to the
'CONTINUE' jump. At that point the file pointer sits 'after the end-of-file'
which usually indicates a special position where it should be not possible
to do IO.
The 'BACKSPACE' instruction should move the pointer back to the position
'before the end-of-file' so that it should be possible to append further
records to the file, as is stated in many books about Fortran language.
However, the subsequent 'WRITE' instruction leads to an IO exception.
This seems to be caused by the fact that after the end-of-file condition the
pointer to the current record gets out of sync with the real IO status.
Zeroing the current record pointer while generating IO error helps against
this problem and the sample program given above runs as expected.
Here is an extremely small patch against gcc-4.0.2 which tries to correct
the described bug.
diff -Naur gcc-4.0.2.orig/libgfortran/io/transfer.c gcc-4.0.2/libgfortran/io/transfer.c
--- gcc-4.0.2.orig/libgfortran/io/transfer.c 2005-09-12 01:55:16.000000000 +0700
+++ gcc-4.0.2/libgfortran/io/transfer.c 2005-10-15 12:29:13.000000000 +0700
@@ -1641,11 +1641,13 @@
{
generate_error (ERROR_END, NULL);
current_unit->endfile = AFTER_ENDFILE;
+ current_unit->current_record = 0;
}
break;
case AFTER_ENDFILE:
generate_error (ERROR_ENDFILE, NULL);
+ current_unit->current_record = 0;
break;
}
}
Best regards,
Georgy Salnikov.
_______________________________________________________________________________
Georgy Salnikov
NMR Group
Novosibirsk Institute of Organic Chemistry
Lavrentjeva, 9, 630090 Novosibirsk, Russia
Tel. +7-383-3307864 +7-383-3331456
Fax +7-383-3331456
Email sge@nmr.nioch.nsc.ru
_______________________________________________________________________________