Bug 19568 - [4.0 only] incorrect formatted read
Summary: [4.0 only] incorrect formatted read
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: 4.0.1
Assignee: Not yet assigned to anyone
URL:
Keywords: patch
Depends on:
Blocks: 19595 20661
  Show dependency treegraph
 
Reported: 2005-01-21 17:45 UTC by Ian Gay
Modified: 2005-06-05 07:45 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.1.0
Known to fail:
Last reconfirmed: 2005-01-22 04:16:07


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ian Gay 2005-01-21 17:45:41 UTC
gfortran formatted read incorrectly reads beyond end of record when
record length is < io,format length.

Demo:
ian@ian:~/source/test> cat ftest.f95
program ftest
  integer i
  character*1 b(10)

  open (77, file='input_file')
  read (77, '(10A1)') b
  print *, b
end program ftest

ian@ian:~/source/test> cat input_file
Line 1
Line 2
Line 3
Line 4

ian@ian:~/source/test> gfortran -v
Using built-in specs.
Configured with: ../gcc/configure --enable-languages=c,f95
--prefix=/usr/work/2005020/irun
Thread model: posix
gcc version 4.0.0 20050120 (experimental)

ian@ian:~/source/test> gfortran -o ftest ftest.f95
ian@ian:~/source/test> ./ftest
 Line 1 Lin
Comment 1 Thomas Koenig 2005-01-31 17:02:30 UTC
This looks promising.

I'll do a full check later.

        Thomas

--- transfer.c.orig     2005-01-31 18:03:12.000000000 +0100
+++ transfer.c  2005-01-31 18:04:00.000000000 +0100
@@ -150,6 +150,14 @@
   else
     p = base = data;

+  /* If we have seen the end of the record already, we just
+   * return blanks.
+   */
+  if (sf_seen_eor) {
+    memset(base,' ',*length);
+    return base;
+  }
+
   memset(base,'\0',*length);

   current_unit->bytes_left = options.default_recl;
@@ -1222,8 +1230,11 @@
     case FORMATTED_SEQUENTIAL:
       length = 1;
       /* sf_read has already terminated input because of an '\n'  */
-      if (sf_seen_eor)
-         break;
+      if (sf_seen_eor)
+       {
+         sf_seen_eor = 0;
+         break;
+       }

       do
         {
Comment 2 Thomas Koenig 2005-02-01 13:08:33 UTC
Patch here:

http://gcc.gnu.org/ml/gcc-patches/2005-01/msg02295.html
Comment 3 Thomas Koenig 2005-02-01 16:41:42 UTC
The patch does not handle advance="NO" correctly.

I'll need to do something else.

        Thomas
Comment 4 Thomas Koenig 2005-02-01 20:01:16 UTC
Replying to myself here...

After having thought of this, I think the patch should go in
because it fixes a g77 regression, and advance= isn't among the
things that g77 supports.

Thomas
Comment 5 Thomas Koenig 2005-03-06 23:34:19 UTC
Updated patch:

http://gcc.gnu.org/ml/gcc-patches/2005-03/msg00566.html
Comment 6 Thomas Koenig 2005-03-08 08:21:34 UTC
Updated patch:

http://gcc.gnu.org/ml/gcc-patches/2005-03/msg00729.html
Comment 7 GCC Commits 2005-04-10 08:36:08 UTC
Subject: Bug 19568

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	tkoenig@gcc.gnu.org	2005-04-10 08:35:40

Modified files:
	libgfortran    : ChangeLog 
	libgfortran/io : transfer.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gfortran.dg: eor_handling_1.f90 eor_handling_2.f90 
	                           eor_handling_3.f90 eor_handling_4.f90 
	                           eor_handling_5.f90 noadv_size.f90 
	                           pad_no.f90 

Log message:
	2005-04-10  Thomas Koenig <Thomas.Koenig@online.de>
	
	PR libfortran/17992
	PR libfortran/19568
	PR libfortran/19595
	PR libfortran/20005
	PR libfortran/20092
	PR libfortran/20131
	PR libfortran/20138
	PR libfortran/20661
	PR libfortran/20744
	* io/transfer.c (top level): eor_condition: New static variable.
	(read_sf): Remove unnecessary zeroing of buffer (there is enough
	information in its length).
	Return a string of length 0 (to be padded by caller) if EOR was
	seen previously.
	Remove erroneous special casing of EOR for standard input.
	Set eor_condition for non-advancing I/O if an end of line was
	detected.
	Increment ioparm.size if necessary.
	(formatted_transfer):  Skip the function if there is an EOR condition.
	(data_transfer_init):  Initialize eor_condition to zero (false).
	(next_record_r):  Clear sf_seen_eor if a \n has been seen already.
	(finalize_transfer):  If there is an EOR condition, raise the error.
	
	2005-04-10   Thomas Koenig  <Thomas.Koenig@online.de>
	
	* eor_handling_1.f90: New test case.
	* eor_handling_2.f90: New test case.
	* eor_handling_3.f90: New test case.
	* eor_handling_4.f90: New test case.
	* eor_handling_5.f90: New test case.
	* noadv_size.f90: New test case.
	* pad_no.f90: New test case.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/ChangeLog.diff?cvsroot=gcc&r1=1.188&r2=1.189
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/transfer.c.diff?cvsroot=gcc&r1=1.35&r2=1.36
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5320&r2=1.5321
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/eor_handling_1.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/eor_handling_2.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/eor_handling_3.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/eor_handling_4.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/eor_handling_5.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/noadv_size.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/pad_no.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 8 Thomas Koenig 2005-04-10 08:38:25 UTC
Fixed in 4.1.0, waiting for 4.0 to reopen.
Comment 9 GCC Commits 2005-05-03 19:40:06 UTC
Subject: Bug 19568

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-4_0-branch
Changes by:	tkoenig@gcc.gnu.org	2005-05-03 19:39:36

Modified files:
	libgfortran    : ChangeLog 
	libgfortran/io : transfer.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gfortran.dg: eor_handling_1.f90 eor_handling_2.f90 
	                           eor_handling_3.f90 eor_handling_4.f90 
	                           eor_handling_5.f90 noadv_size.f90 
	                           pad_no.f90 

Log message:
	2005-05-03  Thomas Koenig <Thomas.Koenig@online.de>
	
	Backport from mainline:
	PR libfortran/17992
	PR libfortran/19568
	PR libfortran/19595
	PR libfortran/20005
	PR libfortran/20092
	PR libfortran/20131
	PR libfortran/20661
	PR libfortran/20744
	* io/transfer.c (top level): eor_condition: New static variable.
	(read_sf): Remove unnecessary zeroing of buffer (there is enough
	information in its length).
	Return a string of length 0 (to be padded by caller) if EOR was
	seen previously.
	Remove erroneous special casing of EOR for standard input.
	Set eor_condition for non-advancing I/O if an end of line was
	detected.
	Increment ioparm.size if necessary.
	(formatted_transfer):  Skip the function if there is an EOR condition.
	(data_transfer_init):  Initialize eor_condition to zero (false).
	(next_record_r):  Clear sf_seen_eor if a \n has been seen already.
	(finalize_transfer):  If there is an EOR condition, raise the error.
	
	2005-05-03  Thomas Koenig  <Thomas.Koenig@online.de>
	
	Backport from mainline:
	* eor_handling_1.f90: New test case.
	* eor_handling_2.f90: New test case.
	* eor_handling_3.f90: New test case.
	* eor_handling_4.f90: New test case.
	* eor_handling_5.f90: New test case.
	* noadv_size.f90: New test case.
	* pad_no.f90: New test case.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.163.2.22&r2=1.163.2.23
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/transfer.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.32.2.2&r2=1.32.2.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.5084.2.156&r2=1.5084.2.157
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/eor_handling_1.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.6.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/eor_handling_2.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.6.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/eor_handling_3.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.6.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/eor_handling_4.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.6.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/eor_handling_5.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.6.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/noadv_size.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.6.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/pad_no.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.6.1

Comment 10 Thomas Koenig 2005-05-03 19:51:07 UTC
Patch committed to 4.0.1.  Fixed.