+{
+ const char *name = setlocale (LC_NUMERIC, NULL);
+ return name != NULL && strcmp (name, saved_name) == 0;
+}
if (!old_locale_ctr++)
{
- old_locale = setlocale (LC_NUMERIC, NULL);
- setlocale (LC_NUMERIC, "C");
+ /* The next setlocale call may free this string; keep a copy. */
+ const char *cur_locale = setlocale (LC_NUMERIC, NULL);
+ if (cur_locale != NULL && strcmp (cur_locale, "C") != 0)
+ {
+ old_locale = strdup (cur_locale);
+ setlocale (LC_NUMERIC, "C");
+ }
}
The strdup here probably should be checked in case it fails.
There's another bug in this code (not related to this patch)
The counter’s increments and decrements don’t match:
data_transfer_init_worker increments old_locale_ctr only for
FORM_FORMATTED.
finalize_transfer decrements it for every transfer, including
unformatted ones and transfers that failed before the increment (for
example "Cannot READ after a nonadvancing WRITE").
It also returns early for child DTIO and for the namelist/unformatted
error, without decrementing.
One unformatted WRITE drops the counter to -1. After that,
!old_locale_ctr++ is never true, and formatted I/O stops switching to
“C” at all. Going the other way, the early returns can leave the counter
stuck above zero, so the locale is never restored.