public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [patch, libgfortran] PR31207 Last record truncated for read after  short write, direct access file
@ 2007-04-01  0:12 Jerry DeLisle
  2007-04-01  7:59 ` FX Coudert
  0 siblings, 1 reply; 2+ messages in thread
From: Jerry DeLisle @ 2007-04-01  0:12 UTC (permalink / raw)
  To: Fortran List; +Cc: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 657 bytes --]

:ADDPATCH fortran:

The attached patch fixes this bug by checking the saved_pos value just before 
closing a unit.  If there are bytes indicated, we allocate, which effectively 
moves the file position over the previously written bytes so that they are sent 
to the output when the sclose is called.

That patch works for both implicit and explicit closing of the file.

Regression tested on x86-64-Gnu/Linux.

Test cases attached.

OK for trunk?

Regards,

Jerry
2007-03-31  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR libgfortran/31207
	* io/unit.c (close_unit_1): If there are bytes previously written from
	ADVANCE="no", move to the end before closing.

[-- Attachment #2: pr31207.diff --]
[-- Type: text/x-patch, Size: 899 bytes --]

Index: unit.c
===================================================================
*** unit.c	(revision 123355)
--- unit.c	(working copy)
*************** close_unit_1 (gfc_unit *u, int locked)
*** 590,595 ****
--- 590,619 ----
  {
    int i, rc;
  
+   /* If there are previously written bytes from a write with ADVANCE="no"
+      Reposition the buffer before closing.  */
+   if (u->saved_pos > 0)
+     {
+       char *p;
+ 
+       p = salloc_w (u->s, &u->saved_pos);
+ 
+       if (!(u->unit_number == options.stdout_unit
+ 	    || u->unit_number == options.stderr_unit))
+         {
+ 	  size_t len;
+ 
+ 	  const char crlf[] = "\r\n";
+ #ifdef HAVE_CRLF
+ 	  len = 2;
+ #else
+ 	  len = 1;
+ #endif
+ 	  if (swrite (u->s, &crlf[2-len], &len) != 0)
+ 	    os_error ("Close after ADVANCE_NO failed");
+ 	}
+     }
+ 
    rc = (u->s == NULL) ? 0 : sclose (u->s) == FAILURE;
  
    u->closed = 1;

[-- Attachment #3: advance_4.f90 --]
[-- Type: text/x-fortran, Size: 292 bytes --]

! { dg-do run }
! PR31207 Last record truncated for read after short write
program main
  character(10) :: answer
  write (12,'(A,T2,A)',advance="no") 'XXXXXX','ABCD'
  close (12)
  read (12, '(6A)') answer
  close (12, status="delete")
  if (answer /= "XABCDX") call abort()
end program main

[-- Attachment #4: advance_5.f90 --]
[-- Type: text/x-fortran, Size: 483 bytes --]

! { dg-do run }
! PR31207 Last record truncated for read after short write.
character(len=20) :: b
! write something no advance
open(10,file="fort.10",position="rewind")
write(10, '(a,t1,a)',advance='no') 'xxxxxx', 'abc'
close(10)
! append some data
open(10,file="fort.10",position="append")
write(10, '(a)') 'def'
close(10)
! check what is in the first record
open(10,file="fort.10",position="rewind")
read(10,'(a)') b
close(10, status="delete")
if (b.ne."abcxxx") call abort()
end

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [patch, libgfortran] PR31207 Last record truncated for read after  short write, direct access file
  2007-04-01  0:12 [patch, libgfortran] PR31207 Last record truncated for read after short write, direct access file Jerry DeLisle
@ 2007-04-01  7:59 ` FX Coudert
  0 siblings, 0 replies; 2+ messages in thread
From: FX Coudert @ 2007-04-01  7:59 UTC (permalink / raw)
  To: Jerry DeLisle; +Cc: Fortran List, gcc-patches

:REVIEWMAIL:

> 2007-03-31  Jerry DeLisle  <jvdelisle@gcc.gnu.org>
>
> 	PR libgfortran/31207
> 	* io/unit.c (close_unit_1): If there are bytes previously written  
> from
> 	ADVANCE="no", move to the end before closing.

OK.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-04-01  7:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-01  0:12 [patch, libgfortran] PR31207 Last record truncated for read after short write, direct access file Jerry DeLisle
2007-04-01  7:59 ` FX Coudert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).