When a standard descriptor is not open at startup, fstat fails with EBADF and fd_to_stream stores fd -1 in the stream of the preconnected unit (5, 6 or 0). On Windows this is the state of every process that starts without standard handles: pythonw.exe, a GUI-subsystem program started from Explorer, a child created with STARTF_USESTDHANDLES and NULL handles.
Every later operation on such a unit then passes -1 to the C library: raw_write calls write (-1, ...) for a PRINT, and raw_read, raw_seek, raw_tell, raw_size, raw_truncate and stream_isatty do the same for READ, REWIND, BACKSPACE, ENDFILE, INQUIRE (SIZE=) and ISATTY. On mingw, find_file0 also calls id_from_fd, i.e. _get_osfhandle (-1), for each preconnected unit whenever find_file runs: on every INQUIRE (FILE=) of an existing file, and on every OPEN of an existing file when F2018 is not allowed, which is the case in a library without a Fortran main program, where _gfortran_set_options never runs. POSIX fails these calls with EBADF, so the output is silently lost. The Universal CRT (ucrtbase.dll) instead treats a negative descriptor as an invalid parameter: unless the process has installed an invalid parameter handler, it terminates with 0xC0000409 (__fastfail). A gfortran executable survives because the mingw-w64 startup code installs a handler, but a DLL that links libgfortran and is loaded by a host that installs none (such as python.exe and pythonw.exe) kills its host on the first OPEN of an existing file or the first PRINT. msvcrt.dll fails the same calls with EBADF, like POSIX. Never pass a negative descriptor to the C library: id_from_fd returns 0, the value it already returns for an invalid handle, and the raw I/O functions and stream_isatty fail with EBADF. These guards are not conditional on mingw, because they change nothing where the C library already fails such a call with EBADF. In raw_truncate the guard is in the mingw branch: the other branches only call ftruncate or chsize. PR libfortran/128486 libgfortran/ChangeLog: * io/unix.c (id_from_fd): Return 0 for a negative file descriptor. (raw_read, raw_write, raw_seek, raw_tell, raw_size): Fail with EBADF for a negative file descriptor. (raw_truncate) [__MINGW32__]: Likewise. (stream_isatty): Likewise, returning 0. Signed-off-by: Federico Perini <[email protected]> --- Bugzilla: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128486 I found this while using a Fortran DLL from a GUI program that has no console. It worked fine with MSYS2's MINGW64 toolchain, but after we switched to UCRT64 the host started dying with 0xC0000409 on the DLL's first PRINT, or its first OPEN of an existing file. Loading it from pythonw.exe crashes the same way. The reproducer is in the PR. The bug itself seems very old: since at least GCC 4.9, libgfortran has passed fd -1 to the C library for a standard unit that isn't open. msvcrt.dll, like POSIX, just returns EBADF and the output is silently lost; the UCRT aborts the process instead. raw_close already checks for -1, and the patch adds the same check to the other functions. I tested it on MSYS2 UCRT64 (GCC 16.2.0) by putting a patched unix.o into libgfortran.a: the DLL now gets through PRINT, OPEN, READ, REWIND and the rest without crashing. I also regtested on x86_64-pc-linux-gnu (non-bootstrap, make check-gcc-fortran): no new failures. I couldn't add a testcase, as the testsuite can't start a process without standard handles. OK for master, and later for the gcc-14 to gcc-16 branches, where it also applies? I don't have write access, so someone would need to push it. Thank you, Federico Assisted by: Claude Opus 5.5
0001-libgfortran-Never-pass-a-negative-fd-to-the-C-librar.patch
Description: Binary data
