Bug 48618 - Negative unit number in OPEN(...) is sometimes allowed
Summary: Negative unit number in OPEN(...) is sometimes allowed
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-04-15 09:09 UTC by Janne Blomqvist
Modified: 2013-03-20 08:14 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Janne Blomqvist 2011-04-15 09:09:16 UTC
From F2008 9.5.1 paragraph 2: 

"A unit is either an external unit or an internal unit. An external unit is used to refer to an external file and
is specified by an asterisk or a file-unit-number . The value of file-unit-number shall be nonnegative, equal to
one of the named constants INPUT UNIT, OUTPUT UNIT, or ERROR UNIT of the intrinsic module ISO -
FORTRAN ENV (13.8.2), or a NEWUNIT value (9.5.6.12)."

That is, consider:

program unittest
  implicit none
  open(10, file="foo.txt")
  open(10, file="bar.txt")
end program unittest

The second open will close the file foo.txt and open bar.txt on the existing unit. Similarly,

program nutest
  implicit none
  integer :: id
  open(newunit=id, file="foo.txt")
  open(id, file="bar.txt")
end program nutest

should work ("...or a NEWUNIT value" in 9.5.1), but

$ ./a.out 
At line 5 of file nutest.f90
Fortran runtime error: Bad unit number in OPEN statement

However for

program negunit
  implicit none
  open(-10, file="foo.txt")
end program negunit

we are correct to generate the error

$ ./negunit 
At line 3 of file negunit.f90
Fortran runtime error: Bad unit number in OPEN statement

as the unit number does not refer to an existing unit. That is, we must change the logic such that we generate an error for negative unit number in the OPEN statement only after we make sure that the unit doesn't exist.
Comment 1 Tobias Burnus 2013-03-20 08:13:59 UTC
Author: burnus
Date: Wed Mar 20 07:45:49 2013
New Revision: 196805

URL: http://gcc.gnu.org/viewcvs?rev=196805&root=gcc&view=rev
Log:
2013-03-20  Tilo Schwarz  <tilo@tilo-schwarz.de>

        PR libfortran/48618
        * io/open.c (st_open): Raise error for unit number < 0 only if
        unit number does not exist already.

2013-03-20  Tilo Schwarz  <tilo@tilo-schwarz.de>

        PR libfortran/48618
        * gfortran.dg/open_negative_unit_1.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/open_negative_unit_1.f90
Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/io/open.c
Comment 2 Tobias Burnus 2013-03-20 08:14:25 UTC
FIXED on the 4.9 trunk.

Thanks for the report Janne. Thanks for the patch Tilo.