]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - locale/setlocale.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / locale / setlocale.c
index 01838c5cf25fdb118bfb6326908a8f1bf064aa93..7bd27e5398e7a21139c7d52ef4d5e8a063cbc86e 100644 (file)
@@ -1,5 +1,4 @@
-/* Copyright (C) 1991, 1992, 1995-2000, 2002, 2003, 2004, 2006, 2008, 2010, 2011
-   Free Software Foundation, Inc.
+/* Copyright (C) 1991-2024 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
 
    You should have received a copy of the GNU Lesser General Public
    License along with the GNU C Library; if not, see
-   <http://www.gnu.org/licenses/>.  */
+   <https://www.gnu.org/licenses/>.  */
 
 #include <alloca.h>
 #include <argz.h>
 #include <errno.h>
-#include <bits/libc-lock.h>
+#include <libc-lock.h>
 #include <locale.h>
 #include <stdlib.h>
 #include <string.h>
@@ -66,20 +65,18 @@ static char *const _nl_current_used[] =
 
 
 /* Define an array of category names (also the environment variable names).  */
-const union catnamestr_t _nl_category_names attribute_hidden =
+const struct catnamestr_t _nl_category_names attribute_hidden =
   {
-    {
 #define DEFINE_CATEGORY(category, category_name, items, a) \
-      category_name,
+    category_name,
 #include "categories.def"
 #undef DEFINE_CATEGORY
-    }
   };
 
 const uint8_t _nl_category_name_idxs[__LC_LAST] attribute_hidden =
   {
 #define DEFINE_CATEGORY(category, category_name, items, a) \
-    [category] = offsetof (union catnamestr_t, CATNAMEMF (__LINE__)),
+    [category] = offsetof (struct catnamestr_t, CATNAMEMF (__LINE__)),
 #include "categories.def"
 #undef DEFINE_CATEGORY
   };
@@ -138,7 +135,7 @@ extern int _nl_msg_cat_cntr;
 
 /* Construct a new composite name.  */
 static char *
-new_composite_name (int category, const char *newnames[__LC_LAST])
+new_composite_name (int category, const char **newnames)
 {
   size_t last_len = 0;
   size_t cumlen = 0;
@@ -149,9 +146,9 @@ new_composite_name (int category, const char *newnames[__LC_LAST])
   for (i = 0; i < __LC_LAST; ++i)
     if (i != LC_ALL)
       {
-       const char *name = (category == LC_ALL ? newnames[i] :
-                           category == i ? newnames[0] :
-                           _nl_global_locale.__names[i]);
+       const char *name = (category == LC_ALL ? newnames[i]
+                           : category == i ? newnames[0]
+                           _nl_global_locale.__names[i]);
        last_len = strlen (name);
        cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1;
        if (same && name != newnames[0] && strcmp (name, newnames[0]) != 0)
@@ -178,10 +175,10 @@ new_composite_name (int category, const char *newnames[__LC_LAST])
     if (i != LC_ALL)
       {
        /* Add "CATEGORY=NAME;" to the string.  */
-       const char *name = (category == LC_ALL ? newnames[i] :
-                           category == i ? newnames[0] :
-                           _nl_global_locale.__names[i]);
-       p = __stpcpy (p, _nl_category_names.str + _nl_category_name_idxs[i]);
+       const char *name = (category == LC_ALL ? newnames[i]
+                           : category == i ? newnames[0]
+                           _nl_global_locale.__names[i]);
+       p = __stpcpy (p, _nl_category_names_get (i));
        *p++ = '=';
        p = __stpcpy (p, name);
        *p++ = ';';
@@ -205,7 +202,7 @@ setname (int category, const char *name)
 }
 
 /* Put DATA in *_nl_current[CATEGORY].  */
-static inline void
+static void
 setdata (int category, struct __locale_data *data)
 {
   if (CATEGORY_USED (category))
@@ -273,16 +270,24 @@ setlocale (int category, const char *locale)
         of entries of the form `CATEGORY=VALUE'.  */
       const char *newnames[__LC_LAST];
       struct __locale_data *newdata[__LC_LAST];
+      /* Copy of the locale argument, for in-place splitting.  */
+      char *locale_copy = NULL;
 
       /* Set all name pointers to the argument name.  */
       for (category = 0; category < __LC_LAST; ++category)
        if (category != LC_ALL)
          newnames[category] = (char *) locale;
 
-      if (__builtin_expect (strchr (locale, ';') != NULL, 0))
+      if (__glibc_unlikely (strchr (locale, ';') != NULL))
        {
          /* This is a composite name.  Make a copy and split it up.  */
-         char *np = strdupa (locale);
+         locale_copy = __strdup (locale);
+         if (__glibc_unlikely (locale_copy == NULL))
+           {
+             __libc_rwlock_unlock (__libc_setlocale_lock);
+             return NULL;
+           }
+         char *np = locale_copy;
          char *cp;
          int cnt;
 
@@ -291,8 +296,7 @@ setlocale (int category, const char *locale)
              for (cnt = 0; cnt < __LC_LAST; ++cnt)
                if (cnt != LC_ALL
                    && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
-                   && (memcmp (np, (_nl_category_names.str
-                                    + _nl_category_name_idxs[cnt]), cp - np)
+                   && (memcmp (np, (_nl_category_names_get (cnt)), cp - np)
                        == 0))
                  break;
 
@@ -300,6 +304,7 @@ setlocale (int category, const char *locale)
                {
                error_return:
                  __libc_rwlock_unlock (__libc_setlocale_lock);
+                 free (locale_copy);
 
                  /* Bogus category name.  */
                  ERROR_RETURN;
@@ -392,8 +397,9 @@ setlocale (int category, const char *locale)
       /* Critical section left.  */
       __libc_rwlock_unlock (__libc_setlocale_lock);
 
-      /* Free the resources (the locale path variable).  */
+      /* Free the resources.  */
       free (locale_path);
+      free (locale_copy);
 
       return composite;
     }
@@ -413,7 +419,7 @@ setlocale (int category, const char *locale)
          /* We must not simply free a global locale since we have no
             control over the usage.  So we mark it as un-deletable.
 
-            Note: do not remove the `if', it's necessary to copy with
+            Note: do not remove the `if', it's necessary to cope with
             the builtin locale data.  */
          if (newdata->usage_count != UNDELETABLE)
            newdata->usage_count = UNDELETABLE;
@@ -462,7 +468,7 @@ setlocale (int category, const char *locale)
 }
 libc_hidden_def (setlocale)
 
-static void __libc_freeres_fn_section
+static void
 free_category (int category,
               struct __locale_data *here, struct __locale_data *c_data)
 {
@@ -483,7 +489,7 @@ free_category (int category,
       struct __locale_data *data = (struct __locale_data *) runp->data;
 
       if (data != NULL && data != c_data)
-       _nl_unload_locale (data);
+       _nl_unload_locale (category, data);
       runp = runp->next;
       free ((char *) curr->filename);
       free (curr);
@@ -492,7 +498,7 @@ free_category (int category,
 
 /* This is called from iconv/gconv_db.c's free_mem, as locales must
    be freed before freeing gconv steps arrays.  */
-void __libc_freeres_fn_section
+void
 _nl_locale_subfreeres (void)
 {
 #ifdef NL_CURRENT_INDIRECT