http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48618
Summary: Negative unit number in OPEN(...) is sometimes allowed Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: libfortran AssignedTo: unassig...@gcc.gnu.org ReportedBy: j...@gcc.gnu.org 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.