https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128183

--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Yong <[email protected]>:

https://gcc.gnu.org/g:4ab39ca82231997cd925d4c4e04995e22836119e

commit r17-6537-g4ab39ca82231997cd925d4c4e04995e22836119e
Author: Julien Marrec <[email protected]>
Date:   Mon Oct 26 17:08:43 2026 +0000

    libgfortran: Copy saved LC_NUMERIC name before switching to "C" [PR128183]

    On targets without POSIX 2008 locales (e.g. MinGW-w64), formatted I/O
    saves the pointer returned by setlocale (LC_NUMERIC, NULL) and restores
    through it after setlocale (LC_NUMERIC, "C"). C11 allows that second call
    to overwrite the string, and both Windows C runtimes invalidate it. msvcrt
    overwrites its static buffer with "C", so a non-"C" LC_NUMERIC set by the
    program is reset to "C" by the first formatted I/O statement. UCRT frees
    the buffer, so the restore reads freed memory and leaves the process in
    an arbitrary locale or fails, and formatted I/O becomes several times
slower.

    Save a copy of the name instead, and skip the switch entirely when
LC_NUMERIC
    is already "C", which avoids two setlocale calls per data transfer
statement
    in the common case.

            PR libfortran/128183

    libgfortran/ChangeLog:

            * io/transfer.c (data_transfer_init_worker): Save a copy of the
            LC_NUMERIC name, and only switch to "C" if it is not already "C".
            (finalize_transfer): Only restore if a name was saved, and free it.
            * io/unit.c (old_locale): Update comment.

    gcc/testsuite/ChangeLog:

            * gfortran.dg/pr128183.f90: New test.
            * gfortran.dg/pr128183_aux.c: New file.

    Signed-off-by: Julien Marrec <[email protected]>
    Signed-off-by: Jonathan Yong <[email protected]>

Reply via email to