From: Bruno Haible Date: Wed, 10 Dec 2025 21:23:45 +0000 (+0100) Subject: localename-unsafe: Improve Windows LCID lookup cache. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fdbdc06db49643d838ba91c5084a4707fcdd87bf;p=thirdparty%2Fgnulib.git localename-unsafe: Improve Windows LCID lookup cache. Reported by Bryan Green in . * lib/localename-unsafe.c (get_lcid): Cache also the failed LCID lookups. --- diff --git a/ChangeLog b/ChangeLog index d9e20d56b2..6e07253e4b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2025-12-10 Bruno Haible + localename-unsafe: Improve Windows LCID lookup cache. + Reported by Bryan Green in + . + * lib/localename-unsafe.c (get_lcid): Cache also the failed LCID + lookups. + localename-unsafe: Improve Windows LCID lookup cache. * lib/localename-unsafe.c (get_lcid): Don't overrun the last_locale array. diff --git a/lib/localename-unsafe.c b/lib/localename-unsafe.c index f70cedafbc..009c044663 100644 --- a/lib/localename-unsafe.c +++ b/lib/localename-unsafe.c @@ -2580,33 +2580,36 @@ static glwthread_mutex_t get_lcid_lock = GLWTHREAD_MUTEX_INIT; /* Return the Locale ID (LCID) number given the locale's name, a string, in LOCALE_NAME. This works by enumerating all the locales supported by the system, until we find one whose name matches - LOCALE_NAME. */ + LOCALE_NAME. Return 0 if LOCALE_NAME does not correspond to a locale + supported by the system. */ static LCID get_lcid (const char *locale_name) { /* A simple cache. */ + static unsigned int last_cached /* = 0 */; static LCID last_lcid; static char last_locale[sizeof (lname)]; /* Lock while looking for an LCID, to protect access to static variables: last_lcid, last_locale, found_lcid, and lname. */ glwthread_mutex_lock (&get_lcid_lock); - if (last_lcid > 0 && streq (locale_name, last_locale)) - { - glwthread_mutex_unlock (&get_lcid_lock); - return last_lcid; - } - strncpy (lname, locale_name, sizeof (lname) - 1); - lname[sizeof (lname) - 1] = '\0'; - found_lcid = 0; - EnumSystemLocales (enum_locales_fn, LCID_SUPPORTED); - if (found_lcid > 0) + + if (!(last_cached && streq (locale_name, last_locale))) { + strncpy (lname, locale_name, sizeof (lname) - 1); + lname[sizeof (lname) - 1] = '\0'; + + found_lcid = 0; + EnumSystemLocales (enum_locales_fn, LCID_SUPPORTED); + last_lcid = found_lcid; strcpy (last_locale, lname); + last_cached = 1; } + + LCID result = last_lcid; glwthread_mutex_unlock (&get_lcid_lock); - return found_lcid; + return result; } # endif