Bug 20156 - gfortran - bus error on backspace
Summary: gfortran - bus error on backspace
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:
Depends on:
Blocks:
 
Reported: 2005-02-23 00:38 UTC by Dale Ranta
Modified: 2005-04-03 08:10 UTC (History)
1 user (show)

See Also:
Host: powerpc-apple-darwin7.8.0
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-02-23 14:20:59


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dale Ranta 2005-02-23 00:38:53 UTC
Here is a program that tests the some of the basic file operations that my real
programs do. Absoft 9.0 has no problem. gfortran gets a bus error.


[dir:~/tests/gfortran] dir% f90 -o backspace2 backspace2.f
[dir:~/tests/gfortran] dir% backspace2
 nr =  5
 x(1)=   1.00000
 x(1)=   2.00000
 x(1)=   3.00000
 x(1)=   4.00000
 x(1)=   5.00000
 STOP  
[dir:~/tests/gfortran] dir% gfortran  -o backspace2 backspace2.f
[dir:~/tests/gfortran] dir% backspace2
 nr =           5
Bus error
[dir:~/tests/gfortran] dir% cat backspace2.f
      program main
      dimension x(10)
      do 10 i=1,5
      x(1)=i
      write(3)n,(x(n),n=1,10)
  10  continue
      nr=0
      rewind 3
  20  continue
      read(3,end=30,err=90)n,(x(n),n=1,10)
      nr=nr+1
      goto 20
  30  continue
      write(*,*)'nr =',nr
      do 40 i=1,nr+1
      backspace 3
  40  continue
      do 50 i=1,nr
      read(3,end=70,err=90)n,(x(n),n=1,10)
      write(*,*)'x(1)= ',x(1)
  50  continue
      stop
  70  continue
      write(*,*)'error incorrect eof'
      stop
  90  continue
      write(*,*)' error on 3'
      stop
      end
Comment 1 Andrew Pinski 2005-02-23 14:20:59 UTC
Confirmed.
Comment 2 Dale Ranta 2005-02-26 03:28:01 UTC
I looked into the fortran source code and found a quick fix for the the
backspace bugs that were stopping my programs. In source file backspace.c. 

1). changed 

new = file_position (current_unit->s) - *p - length;
to 
new = file_position (current_unit->s) - *p - 2*length;

2). deleted
    if (u->current_record)
       next_record (1);
 
3). added 
       if (u->mode==WRITING){
          flush(u->s);
          struncate (u->s);
          u->mode = READING;
       }
       
4). added 
     u->endfile = NO_ENDFILE;
     u->current_record = 0;

These changes fix  bugs 20125 and 20156 and a few others that I had not yet
reported. I have run 48 test problems with 5 of my programs on the Macintosh
with these changes and they all ran correctly. If this is of value to the person
that actually fixes these problems please feel free to use it in any way.

[dir:~/fe/mystic/decks] dir% diff -c
/Users/dir/gfortran/gcc/libgfortran/io/backspace.c /Users/dir/junk/io/backspace.c
*** /Users/dir/gfortran/gcc/libgfortran/io/backspace.c  Fri Feb 25 18:48:05 2005
--- /Users/dir/junk/io/backspace.c      Wed Jan 12 13:27:30 2005
***************
*** 111,117 ****
    if (p == NULL)
      goto io_error;
  
!   new = file_position (current_unit->s) - *p - 2*length;
    if (sseek (current_unit->s, new) == FAILURE)
      goto io_error;
  
--- 111,117 ----
    if (p == NULL)
      goto io_error;
  
!   new = file_position (current_unit->s) - *p - length;
    if (sseek (current_unit->s, new) == FAILURE)
      goto io_error;
  
***************
*** 155,175 ****
      u->endfile = AT_ENDFILE;
    else
      {
        if (file_position (u->s) == 0)
        goto done;              /* Common special case */
!       if (u->mode==WRITING){
!          flush(u->s);
!          struncate (u->s);
!            u->mode = READING;
!       }
        if (u->flags.form == FORM_FORMATTED)
        formatted_backspace ();
        else
        unformatted_backspace ();
-     u->endfile = NO_ENDFILE;
-       u->current_record = 0;
      }
- 
  
   done:
    library_end ();
--- 155,171 ----
      u->endfile = AT_ENDFILE;
    else
      {
+       if (u->current_record)
+       next_record (1);
+ 
        if (file_position (u->s) == 0)
        goto done;              /* Common special case */
! 
        if (u->flags.form == FORM_FORMATTED)
        formatted_backspace ();
        else
        unformatted_backspace ();
      }
  
   done:
    library_end ();

Comment 3 kargls 2005-02-26 06:28:27 UTC
Dale,

I believe Bud Davis or Thomas Koening are the logical people
to look at your patch.  If either one doesn't pursue your patch
within the next week drop me an email.

One thing to keep in mind.  The patch here is fairly minimal
so it probably doesn't need copyright assignment paperwork on
file with FSF.  However, if you send additional patches
(which I strongly encourage :-) and if you don't have paperwork
on file with the FSF, please consider pursuing assignment.
Comment 4 GCC Commits 2005-04-03 08:04:27 UTC
Subject: Bug 20156

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-4_0-branch
Changes by:	fxcoudert@gcc.gnu.org	2005-04-03 08:04:04

Modified files:
	gcc/testsuite  : ChangeLog 
	libgfortran    : ChangeLog 
	libgfortran/io : backspace.c 
Added files:
	gcc/testsuite/gfortran.dg: backspace.f 

Log message:
	PR libfortran/20068
	PR libfortran/20125
	PR libfortran/20156
	PR libfortran/20471
	
	* io/backspace.c (unformatted_backspace): Fix error in arithmetic.
	(st_backspace): When in WRITING mode, we flush and falling back
	into READING mode. In all cases, correctly position the stream.
	
	* gfortran.dg/backspace.f: New test.

Patches:
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.90&r2=1.5084.2.91
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/backspace.f.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.2.1
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.13&r2=1.163.2.14
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/backspace.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.9&r2=1.9.12.1

Comment 5 GCC Commits 2005-04-03 08:07:56 UTC
Subject: Bug 20156

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	fxcoudert@gcc.gnu.org	2005-04-03 08:07:43

Modified files:
	gcc/testsuite  : ChangeLog 
	libgfortran    : ChangeLog 
	libgfortran/io : backspace.c 
Added files:
	gcc/testsuite/gfortran.dg: backspace.f 

Log message:
	PR libfortran/20068
	PR libfortran/20125
	PR libfortran/20156
	PR libfortran/20471
	
	* io/backspace.c (unformatted_backspace): Fix error in arithmetic.
	(st_backspace): When in WRITING mode, we flush and falling back
	into READING mode. In all cases, correctly position the stream.
	
	* gfortran.dg/backspace.f: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5270&r2=1.5271
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/backspace.f.diff?cvsroot=gcc&r1=1.1&r2=1.2
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/ChangeLog.diff?cvsroot=gcc&r1=1.180&r2=1.181
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/backspace.c.diff?cvsroot=gcc&r1=1.9&r2=1.10

Comment 6 Francois-Xavier Coudert 2005-04-03 08:10:28 UTC
Fixed.