https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84529
Bug ID: 84529 Summary: INQUIRE fails on "recycled" internal units Product: gcc Version: 7.3.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libfortran Assignee: unassigned at gcc dot gnu.org Reporter: albandil at atlas dot cz Target Milestone: --- The following program fails with the error "Inquire statement identifies an internal file" at the call to INQUIRE. program TestRecycleInternalUnit implicit none integer :: iunit, ipos character(len=10) :: sstream write(sstream,'(I0)') 12345 print *, 'sstream = ', sstream open(newunit=iunit, file='output.bin', access="stream", form="unformatted") inquire(iunit,pos=ipos) print *, 'n = ', ipos close(iunit) end program TestRecycleInternalUnit I looked into the code of libgfortran/io/open.c (and around) and it seems that OPEN reuses the cached internal unit (-10 in this case) used for writing to string. However, the member "internal_unit_kind" of the unit structure is not reset to 0 in the "st_open" function. This makes the subsequent INQUIRE think that its argument is an internal unit.