This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch, libfortran] PR33400 Formatted read fails if line ends without line break


:ADDPATCH fortran:

The problem here is specific to list directed reads. Hitting the end of a file on a read was interpreted has an error rather than a legitimate end of the read.

The attached patch is straight forward. The first encounter of the end of the file is interpreted as the end of the line. Subsequent attempts to read are then triggered as an error.

In the course of testing I discovered a flaw in the test logic in PR19872.f. The conditions were ANDed together when they should be ORed and the last condition could never never be true so the test could never fail. The fixed version is attached.

I have provided the new test case, list_read_7.f90.

Regression tested on x86-64-gnu-linux.

I do not know the status of trunk right now. OK to commit?

Best regards,

Jerry

2007-09-26 Jerry DeLisle <jvdelisle@gcc.gnu.org>

	PR libfortran/33400
	* io/list_read.c (next_char): Interpret encountering the end of file the
	first time as an end of line.  Subsequent reads give EOF error.

Index: list_read.c
===================================================================
--- list_read.c	(revision 128777)
+++ list_read.c	(working copy)
@@ -232,8 +232,14 @@ next_char (st_parameter_dt *dtp)
 	  return '\0';
 	}
       if (length == 0)
-	longjmp (*dtp->u.p.eof_jump, 1);
-      c = *p;
+	{
+	  if (dtp->u.p.current_unit->endfile == AT_ENDFILE)
+	    longjmp (*dtp->u.p.eof_jump, 1);
+	  dtp->u.p.current_unit->endfile = AT_ENDFILE;
+	  c = '\n';
+	}
+      else
+	c = *p;
     }
 done:
   dtp->u.p.at_eol = (c == '\n' || c == '\r');
! { dg-do run }
! PR33400 Formatted read fails if line ends without line break
! Test case modified from that in PR by <jvdelisle@gcc.gnu.org>
integer, parameter :: fgsl_strmax = 128
character(len=fgsl_strmax) :: ieee_str1, ieee_str2
open(unit=20, file='test.dat',form='FORMATTED')
write(20,'(a)',advance="no") ' 1.01010101010101010101010101010101&
       &01010101010101010101*2^-2 1.01010101010101010101011*2^-2'
rewind(20)
read(20, fmt=*) ieee_str1, ieee_str2
if (trim(ieee_str1) /= &
    '1.0101010101010101010101010101010101010101010101010101*2^-2') &
  call abort
if (trim(ieee_str2) /= &
     '1.01010101010101010101011*2^-2') &
  call abort
end
! { dg-do run } 
! PR 19872 - closed and re-opened file not overwriten
      implicit none
      integer i(4)
      data i / 4 * 0 /
      open(1,form='FORMATTED',status='UNKNOWN')
      write(1,'("1 2 3 4 5 6 7 8 9")')
      close(1)
      open(1,form='FORMATTED')
      write(1,'("9 8 7 6")')
      close(1)
      open(1,form='FORMATTED')
      read(1,*)i
      if(i(1).ne.9.or.i(2).ne.8.or.i(3).ne.7.or.i(4).ne.6)call abort
      read(1,*, end=200)i
! should only be able to read one line from the file
      call abort
 200  continue
      close(1,STATUS='keep')
      end

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]