GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
fortran

Fortran: Fix NEWUNIT state corruption during internal file I/O

Internal file I/O operations no longer corrupt the unit management table in libgfortran.

A bug in libgfortran’s internal unit management corrupted its state when performing internal file I/O, such as writing to a character variable. This commit introduces a new boolean flag in the gfc_unit structure to track units designated for internal I/O, preventing corruption and ensuring correct behavior. A new test case validates this fix.

In Details

Libgfortran's internal I/O operations (e.g., WRITE to * or *, UNIT=*) previously failed to distinguish between actual external file units and temporary units implicitly created for character I/O. This commit adds is_internal_io flag to struct gfc_unit in libgfortran/io/io.h to differentiate these, preventing operations like close_unit from incorrectly deallocating or corrupting character I/O units. Helper functions is_internal_reserved, find_unit, etc. are updated accordingly in libgfortran/io/unit.c.

For Context
libgfortran
The runtime library for the GNU Fortran compiler (gfortran).
Internal file I/O
Fortran I/O operations that read from or write to a character variable in memory, rather than a physical file on disk. This is often used for formatting or string manipulation.
NEWUNIT
A Fortran keyword used in OPEN statements to ensure that a new, unique file unit number is assigned by the system, preventing accidental overwriting of existing files. The internal management of these unit numbers was affected by the bug.
gfc_unit
A structure within libgfortran that represents an I/O unit, managing its state, file association, and other I/O-related properties.
Filed Under: fortrancompilerruntimeio