Improve locale handling in libgfortran's formatted I/O for non-POSIX locales
libgfortran now copies the LC_NUMERIC locale name before switching to 'C' to prevent issues on platforms without POSIX locales.
On systems lacking POSIX 2008 locales, such as MinGW-w64, libgfortran’s formatted I/O faced issues with locale handling. Previously, it saved a pointer to the locale name and restored it after temporarily setting the locale to ‘C’. This approach could lead to memory corruption or performance degradation due to incorrect handling of the locale string by Windows C runtimes. The fix involves copying the locale name and only switching to ‘C’ if necessary, improving robustness and performance.
In Details
For targets without full POSIX 2008 locale support, libgfortran's data_transfer_init_worker and finalize_transfer routines historically relied on setlocale(LC_NUMERIC, NULL) to retrieve the current locale name. This pointer was then used for restoration after a temporary setlocale(LC_NUMERIC, "C") call. However, C runtimes on Windows (MSVCRT, UCRT) can invalidate this pointer or its buffer. This commit changes the approach to instead save a copy of the locale name string. It also optimizes by skipping the setlocale(LC_NUMERIC, "C") and subsequent restoration if the locale is already…
- LC_NUMERIC
- A locale category that affects the formatting of numeric and string conversions.
- setlocale
- A C standard library function used to query and set the program's locale.
- POSIX locales
- Standards-defined sets of locale-specific information, providing consistent internationalization and localization behavior across systems.
- MinGW-w64
- A development environment for Windows that includes GCC toolchains, often lacking full POSIX compliance.
- MSVCRT
- Microsoft Visual C++ Runtime; the runtime library for Microsoft's C/C++ compilers.
- UCRT
- Universal C Runtime; a newer C runtime library developed by Microsoft.