]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - catgets/open_catalog.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / catgets / open_catalog.c
index bc44f98247d31816d43bc2eb80b6ac80c5826382..5acca1399d56413e0236243bc6764e63c6e3a24e 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996-2014 Free Software Foundation, Inc.
+/* Copyright (C) 1996-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper, <drepper@gnu.org>.
 
@@ -14,7 +14,7 @@
 
    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 <byteswap.h>
 #include <endian.h>
@@ -47,33 +47,34 @@ __open_catalog (const char *cat_name, const char *nlspath, const char *env_var,
   size_t tab_size;
   const char *lastp;
   int result = -1;
+  char *buf = NULL;
 
   if (strchr (cat_name, '/') != NULL || nlspath == NULL)
-    fd = open_not_cancel_2 (cat_name, O_RDONLY);
+    fd = __open_nocancel (cat_name, O_RDONLY);
   else
     {
       const char *run_nlspath = nlspath;
 #define ENOUGH(n)                                                            \
-  if (__builtin_expect (bufact + (n) >= bufmax, 0))                          \
+  if (__glibc_unlikely (bufact + (n) >= bufmax))                             \
     {                                                                        \
       char *old_buf = buf;                                                   \
-      bufmax += 256 + (n);                                                   \
-      buf = (char *) alloca (bufmax);                                        \
-      memcpy (buf, old_buf, bufact);                                         \
+      bufmax += (bufmax < 256 + (n)) ? 256 + (n) : bufmax;                   \
+      buf = realloc (buf, bufmax);                                           \
+      if (__glibc_unlikely (buf == NULL))                                    \
+       {                                                                     \
+         free (old_buf);                                                     \
+         return -1;                                                          \
+       }                                                                     \
     }
 
       /* The RUN_NLSPATH variable contains a colon separated list of
         descriptions where we expect to find catalogs.  We have to
         recognize certain % substitutions and stop when we found the
         first existing file.  */
-      char *buf;
       size_t bufact;
-      size_t bufmax;
+      size_t bufmax = 0;
       size_t len;
 
-      buf = NULL;
-      bufmax = 0;
-
       fd = -1;
       while (*run_nlspath != '\0')
        {
@@ -177,7 +178,7 @@ __open_catalog (const char *cat_name, const char *nlspath, const char *env_var,
 
          if (bufact != 0)
            {
-             fd = open_not_cancel_2 (buf, O_RDONLY);
+             fd = __open_nocancel (buf, O_RDONLY);
              if (fd >= 0)
                break;
            }
@@ -188,7 +189,10 @@ __open_catalog (const char *cat_name, const char *nlspath, const char *env_var,
 
   /* Avoid dealing with directories and block devices */
   if (__builtin_expect (fd, 0) < 0)
-    return -1;
+    {
+      free (buf);
+      return -1;
+    }
 
   if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
     goto close_unlock_return;
@@ -233,7 +237,7 @@ __open_catalog (const char *cat_name, const char *nlspath, const char *env_var,
       /* Save read, handle partial reads.  */
       do
        {
-         size_t now = read_not_cancel (fd, (((char *) catalog->file_ptr)
+         size_t now = __read_nocancel (fd, (((char *) catalog->file_ptr)
                                             + (st.st_size - todo)), todo);
          if (now == 0 || now == (size_t) -1)
            {
@@ -253,7 +257,7 @@ __open_catalog (const char *cat_name, const char *nlspath, const char *env_var,
   /* Determine whether the file is a catalog file and if yes whether
      it is written using the correct byte order.  Else we have to swap
      the values.  */
-  if (__builtin_expect (catalog->file_ptr->magic == CATGETS_MAGIC, 1))
+  if (__glibc_likely (catalog->file_ptr->magic == CATGETS_MAGIC))
     swapping = 0;
   else if (catalog->file_ptr->magic == SWAPU32 (CATGETS_MAGIC))
     swapping = 1;
@@ -324,7 +328,8 @@ __open_catalog (const char *cat_name, const char *nlspath, const char *env_var,
 
   /* Release the lock again.  */
  close_unlock_return:
-  close_not_cancel_no_status (fd);
+  __close_nocancel_nostatus (fd);
+  free (buf);
 
   return result;
 }