From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id CC8583858D1E; Sun, 25 Sep 2022 06:34:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC8583858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664087685; bh=DBp23SDvh4PQZEeucQcwChph6BVG01w+kW0n05vqmO8=; h=From:To:Subject:Date:From; b=d0sRBB8WQ2omrHPzKGoEmIDBJkXPtzBqOnYbFSeQNd7S2MC/tJZIhBPr7RCWSy8zy M0wgFzB4SnMZ5OUVnKkJXnYuwJpPxOSgLdWCDiU+FQAffOiMFQRFDN0Jxo5ySeLR1w Kutrz2Xno4U3NxoZA5bwwRswBbRiNZo9XdImzwbE= From: "kishore96 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug fortran/107031] New: endfile truncates file at wrong position Date: Sun, 25 Sep 2022 06:34:45 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: fortran X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: kishore96 at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107031 Bug ID: 107031 Summary: endfile truncates file at wrong position Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: kishore96 at gmail dot com Target Milestone: --- SUMMARY ------- When `endfile` is used to truncate a file after a read statement, gfortran wrongly inserts end-of-file after the next line, rather than after the curr= ent line. STEPS TO REPRODUCE ------------------ 1. create the following program, truncate.f90: ```fortran program test_truncate integer :: num_rec, tmp, i, ioerr open(1, file=3D"in.dat", action=3D'readwrite') num_rec =3D 5 i =3D 1 ioerr =3D 0 do while (i <=3D num_rec .and. ioerr =3D=3D 0) read(1, *, iostat=3Dioerr) tmp i =3D i+1 enddo !backspace(1) endfile(1) close(1) end program test_truncate ``` 2. Create the following text file, `in.dat` ``` 1 2 3 4 5 6 7 8 9 10 ``` 3. Compile the program: `gfortran truncate.f90` 4. Run the program: `./a.out` OBSERVED RESULTS ---------------- ```bash $cat in.dat 1 2 3 4 5 6 ``` EXPECTED RESULTS ---------------- ```bash $cat in.dat 1 2 3 4 5 ``` Since after the end of the loop, the current record is `5`, one would expect endfile to write an end-of-file record as the next record (e.g. ), lead= ing to the result above. SOFTWARE VERSIONS ----------------- OS: Arch Linux gfortran: 12.2.0 ADDITIONAL INFORMATION ---------------------- Intel ifort (version 19.0.5.281 20190815) produces the expected result. May be related to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D52387, but= I think not, since my example program doesn't use nonadvancing reads anywhere. Uncommenting the `backspace(1)` gives output ```bash $cat in.dat 1 2 3 4 ```=