]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - elf/chroot_canon.c
install.texi: Build was tested with binutils 2.41 (just released)
[thirdparty/glibc.git] / elf / chroot_canon.c
index 3ef2fdf08fdc57fce827fa62775f70891230e350..63a1ae6dbba759e7c55dfc540203e45d1b160ff0 100644 (file)
@@ -1,11 +1,11 @@
 /* Return the canonical absolute name of a given file inside chroot.
-   Copyright (C) 1996,1997,1998,1999,2000,2001,2004,2005
-       Free Software Foundation, Inc.
+   Copyright (C) 1996-2023 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License version 2 as
-   published by the Free Software Foundation.
+   it under the terms of the GNU General Public License as published
+   by the Free Software Foundation; version 2 of the License, or
+   (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   along with this program; if not, see <https://www.gnu.org/licenses/>.  */
 
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <limits.h>
-#include <sys/param.h>
 #include <sys/stat.h>
 #include <errno.h>
 #include <stddef.h>
 #include <stdint.h>
 
+#include <eloop-threshold.h>
 #include <ldconfig.h>
 
 #ifndef PATH_MAX
@@ -57,9 +56,7 @@ chroot_canon (const char *chroot, const char *name)
       return NULL;
     }
 
-  rpath = malloc (chroot_len + PATH_MAX);
-  if (rpath == NULL)
-    return NULL;
+  rpath = xmalloc (chroot_len + PATH_MAX);
 
   rpath_limit = rpath + chroot_len + PATH_MAX;
 
@@ -70,8 +67,7 @@ chroot_canon (const char *chroot, const char *name)
 
   for (start = end = name; *start; start = end)
     {
-      struct stat64 st;
-      int n;
+      struct stat st;
 
       /* Skip sequence of multiple path-separators.  */
       while (*start == '/')
@@ -108,9 +104,7 @@ chroot_canon (const char *chroot, const char *name)
                new_size += end - start + 1;
              else
                new_size += PATH_MAX;
-             new_rpath = (char *) realloc (rpath, new_size);
-             if (new_rpath == NULL)
-               goto error;
+             new_rpath = (char *) xrealloc (rpath, new_size);
              rpath = new_rpath;
              rpath_limit = rpath + new_size;
 
@@ -120,7 +114,7 @@ chroot_canon (const char *chroot, const char *name)
          dest = mempcpy (dest, start, end - start);
          *dest = '\0';
 
-         if (lstat64 (rpath, &st) < 0)
+         if (lstat (rpath, &st) < 0)
            {
              if (*end == '\0')
                goto done;
@@ -132,13 +126,13 @@ chroot_canon (const char *chroot, const char *name)
              char *buf = alloca (PATH_MAX);
              size_t len;
 
-             if (++num_links > MAXSYMLINKS)
+             if (++num_links > __eloop_threshold ())
                {
                  __set_errno (ELOOP);
                  goto error;
                }
 
-             n = readlink (rpath, buf, PATH_MAX);
+             ssize_t n = readlink (rpath, buf, PATH_MAX - 1);
              if (n < 0)
                {
                  if (*end == '\0')
@@ -151,7 +145,7 @@ chroot_canon (const char *chroot, const char *name)
                extra_buf = alloca (PATH_MAX);
 
              len = strlen (end);
-             if ((long int) (n + len) >= PATH_MAX)
+             if (len >= PATH_MAX - n)
                {
                  __set_errno (ENAMETOOLONG);
                  goto error;