]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - locale/loadlocale.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / locale / loadlocale.c
index 32faad57e9bc7f3f8bc465aec3ba0b4494d7957d..2b589eeb8c6f205b2c5e9685e5eed9476dcb7efa 100644 (file)
@@ -1,5 +1,5 @@
 /* Functions to read locale data files.
-   Copyright (C) 1996-2001, 2002 Free Software Foundation, Inc.
+   Copyright (C) 1996-2016 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
 
@@ -14,9 +14,8 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
 #include <assert.h>
 #include <errno.h>
@@ -30,6 +29,7 @@
 #endif
 #include <sys/stat.h>
 
+#include <not-cancel.h>
 #include "localeinfo.h"
 
 
@@ -51,7 +51,7 @@ static const enum value_type _nl_value_type_##category[] = { NO_PAREN items };
 #include "categories.def"
 #undef DEFINE_CATEGORY
 
-static const enum value_type *_nl_value_types[] =
+static const enum value_type *const _nl_value_types[] =
 {
 #define DEFINE_CATEGORY(category, category_name, items, a) \
   [category] = _nl_value_type_##category,
@@ -60,7 +60,7 @@ static const enum value_type *_nl_value_types[] =
 };
 
 
-struct locale_data *
+struct __locale_data *
 internal_function
 _nl_intern_locale_data (int category, const void *data, size_t datasize)
 {
@@ -70,7 +70,7 @@ _nl_intern_locale_data (int category, const void *data, size_t datasize)
       unsigned int nstrings;
       unsigned int strindex[0];
     } *const filedata = data;
-  struct locale_data *newdata;
+  struct __locale_data *newdata;
   size_t cnt;
 
   if (__builtin_expect (datasize < sizeof *filedata, 0)
@@ -107,22 +107,53 @@ _nl_intern_locale_data (int category, const void *data, size_t datasize)
   for (cnt = 0; cnt < newdata->nstrings; ++cnt)
     {
       size_t idx = filedata->strindex[cnt];
-      if (__builtin_expect (idx > (size_t) newdata->filesize, 0))
+      if (__glibc_unlikely (idx > (size_t) newdata->filesize))
        {
        puntdata:
          free (newdata);
          __set_errno (EINVAL);
          return NULL;
        }
-      if (__builtin_expect (_nl_value_types[category][cnt] == word, 0))
+
+      /* Determine the type.  There is one special case: the LC_CTYPE
+        category can have more elements than there are in the
+        _nl_value_type_LC_XYZ array.  There are all pointers.  */
+      switch (category)
+       {
+#define CATTEST(cat) \
+       case LC_##cat:                                          \
+         if (cnt >= (sizeof (_nl_value_type_LC_##cat)          \
+                     / sizeof (_nl_value_type_LC_##cat[0])))   \
+           goto puntdata;                                      \
+         break
+         CATTEST (NUMERIC);
+         CATTEST (TIME);
+         CATTEST (COLLATE);
+         CATTEST (MONETARY);
+         CATTEST (MESSAGES);
+         CATTEST (PAPER);
+         CATTEST (NAME);
+         CATTEST (ADDRESS);
+         CATTEST (TELEPHONE);
+         CATTEST (MEASUREMENT);
+         CATTEST (IDENTIFICATION);
+       default:
+         assert (category == LC_CTYPE);
+         break;
+       }
+
+      if ((category == LC_CTYPE
+          && cnt >= (sizeof (_nl_value_type_LC_CTYPE)
+                     / sizeof (_nl_value_type_LC_CTYPE[0])))
+         || __builtin_expect (_nl_value_types[category][cnt] != word, 1))
+       newdata->values[cnt].string = newdata->filedata + idx;
+      else
        {
-         if (idx % __alignof__ (u_int32_t) != 0)
+         if (!LOCFILE_ALIGNED_P (idx))
            goto puntdata;
          newdata->values[cnt].word =
            *((const u_int32_t *) (newdata->filedata + idx));
        }
-      else
-       newdata->values[cnt].string = newdata->filedata + idx;
     }
 
   return newdata;
@@ -135,14 +166,14 @@ _nl_load_locale (struct loaded_l10nfile *file, int category)
   int fd;
   void *filedata;
   struct stat64 st;
-  struct locale_data *newdata;
+  struct __locale_data *newdata;
   int save_err;
   int alloc = ld_mapped;
 
   file->decided = 1;
   file->data = NULL;
 
-  fd = __open (file->filename, O_RDONLY);
+  fd = open_not_cancel_2 (file->filename, O_RDONLY | O_CLOEXEC);
   if (__builtin_expect (fd, 0) < 0)
     /* Cannot open the file.  */
     return;
@@ -150,27 +181,27 @@ _nl_load_locale (struct loaded_l10nfile *file, int category)
   if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
     {
     puntfd:
-      __close (fd);
+      close_not_cancel_no_status (fd);
       return;
     }
-  if (__builtin_expect (S_ISDIR (st.st_mode), 0))
+  if (__glibc_unlikely (S_ISDIR (st.st_mode)))
     {
       /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
-           instead.  */
+          instead.  */
       char *newp;
       size_t filenamelen;
 
-      __close (fd);
+      close_not_cancel_no_status (fd);
 
       filenamelen = strlen (file->filename);
       newp = (char *) alloca (filenamelen
                              + 5 + _nl_category_name_sizes[category] + 1);
       __mempcpy (__mempcpy (__mempcpy (newp, file->filename, filenamelen),
                            "/SYS_", 5),
-                _nl_category_names[category],
+                _nl_category_names.str + _nl_category_name_idxs[category],
                 _nl_category_name_sizes[category] + 1);
 
-      fd = __open (newp, O_RDONLY);
+      fd = open_not_cancel_2 (newp, O_RDONLY | O_CLOEXEC);
       if (__builtin_expect (fd, 0) < 0)
        return;
 
@@ -191,8 +222,9 @@ _nl_load_locale (struct loaded_l10nfile *file, int category)
 # endif
   filedata = __mmap ((caddr_t) 0, st.st_size,
                     PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
-  if (__builtin_expect (filedata == MAP_FAILED, 0))
+  if (__glibc_unlikely (filedata == MAP_FAILED))
     {
+      filedata = NULL;
       if (__builtin_expect (errno, ENOSYS) == ENOSYS)
        {
 #endif /* _POSIX_MAPPED_FILES */
@@ -206,7 +238,7 @@ _nl_load_locale (struct loaded_l10nfile *file, int category)
              char *p = (char *) filedata;
              while (to_read > 0)
                {
-                 nread = __read (fd, p, to_read);
+                 nread = read_not_cancel (fd, p, to_read);
                  if (__builtin_expect (nread, 1) <= 0)
                    {
                      free (filedata);
@@ -225,14 +257,14 @@ _nl_load_locale (struct loaded_l10nfile *file, int category)
 #endif /* _POSIX_MAPPED_FILES */
 
   /* We have mapped the data, so we no longer need the descriptor.  */
-  __close (fd);
+  close_not_cancel_no_status (fd);
 
-  if (__builtin_expect (filedata == NULL, 0))
+  if (__glibc_unlikely (filedata == NULL))
     /* We failed to map or read the data.  */
     return;
 
   newdata = _nl_intern_locale_data (category, filedata, st.st_size);
-  if (__builtin_expect (newdata == NULL, 0))
+  if (__glibc_unlikely (newdata == NULL))
     /* Bad data.  */
     {
 #ifdef _POSIX_MAPPED_FILES
@@ -251,7 +283,7 @@ _nl_load_locale (struct loaded_l10nfile *file, int category)
 
 void
 internal_function
-_nl_unload_locale (struct locale_data *locale)
+_nl_unload_locale (struct __locale_data *locale)
 {
   if (locale->private.cleanup)
     (*locale->private.cleanup) (locale);