]> Savannah Git Hosting - gnulib.git/commitdiff
canonicalize-lgpl: simplify merge to glibc
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 14 Dec 2020 19:59:20 +0000 (11:59 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 17 Dec 2020 09:39:45 +0000 (01:39 -0800)
This patch lessens the differences between git glibc
stdlib/canonicalize.c and lib/canonicalize-lgpl.c.
The (perhaps wishful) goal is to make them identical.
* lib/canonicalize-lgpl.c [!_LIBC]:
Include <libc-config.h>, not config.h.
Omit an unnecessary (!HAVE_CANONICALIZE_FILE_NAME ||
!FUNC_REALPATH_WORKS || defined _LIBC) #if.
Do not include alloca.h, since we use malloca now.
[_LIBC]: Include <eloop-threshold.h>, and define dummy macros
FILE_SYSTEM_PREFIX_LEN, IS_ABSOLUTE_FILE_NAME, ISSLASH, malloca,
freea so that the mainline code can be kept #ifdef free.
[!_LIBC]: Remove dummy macros for SHLIB_COMPAT, versioned_symbol,
compat_symbol, weak_alias, __set_errno since libc-config.h does that.
Add redirecting macros __mempcpy, __pathconf, __rawmemchr,
__eloop_threshold.  All uses of their definiens changed.
(SIZE_MAX): Remove; no longer needed.
(alloc_failed): Remove, and remove all instances.
No need for alloc_failed now that free preserves errno.
(__realpath): Default path_max to 1024 instead of 8192, as that’s
the glibc tradition and is safer when the 2nd argument is null.
Use __rawmemchr instead of strchr.
Use __mempcpy where appropriate.
Simplify test for overflow so that it does not need SIZE_MAX.
Do not preserve errno around free or freea calls; no longer needed.
Mark __realpath with libc_hidden_def.
* modules/canonicalize-lgpl (Depends-on): Add free, libc-config,
malloc-posix, mempcpy, realloc-posix, rawmemchr.
* modules/free: Now LGPLv2+, for canonicalize-lgpl.

ChangeLog
lib/canonicalize-lgpl.c
modules/canonicalize-lgpl
modules/free

index e97d82aa6aa3274cf3f2e6da2bfee903f8338c65..85e2b4c873b6e60c816cd3496b8d893714531f3f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,35 @@
 2020-12-17  Paul Eggert  <eggert@cs.ucla.edu>
 
+       canonicalize-lgpl: simplify merge to glibc
+       This patch lessens the differences between git glibc
+       stdlib/canonicalize.c and lib/canonicalize-lgpl.c.
+       The (perhaps wishful) goal is to make them identical.
+       * lib/canonicalize-lgpl.c [!_LIBC]:
+       Include <libc-config.h>, not config.h.
+       Omit an unnecessary (!HAVE_CANONICALIZE_FILE_NAME ||
+       !FUNC_REALPATH_WORKS || defined _LIBC) #if.
+       Do not include alloca.h, since we use malloca now.
+       [_LIBC]: Include <eloop-threshold.h>, and define dummy macros
+       FILE_SYSTEM_PREFIX_LEN, IS_ABSOLUTE_FILE_NAME, ISSLASH, malloca,
+       freea so that the mainline code can be kept #ifdef free.
+       [!_LIBC]: Remove dummy macros for SHLIB_COMPAT, versioned_symbol,
+       compat_symbol, weak_alias, __set_errno since libc-config.h does that.
+       Add redirecting macros __mempcpy, __pathconf, __rawmemchr,
+       __eloop_threshold.  All uses of their definiens changed.
+       (SIZE_MAX): Remove; no longer needed.
+       (alloc_failed): Remove, and remove all instances.
+       No need for alloc_failed now that free preserves errno.
+       (__realpath): Default path_max to 1024 instead of 8192, as that’s
+       the glibc tradition and is safer when the 2nd argument is null.
+       Use __rawmemchr instead of strchr.
+       Use __mempcpy where appropriate.
+       Simplify test for overflow so that it does not need SIZE_MAX.
+       Do not preserve errno around free or freea calls; no longer needed.
+       Mark __realpath with libc_hidden_def.
+       * modules/canonicalize-lgpl (Depends-on): Add free, libc-config,
+       malloc-posix, mempcpy, realloc-posix, rawmemchr.
+       * modules/free: Now LGPLv2+, for canonicalize-lgpl.
+
        free: preserve errno
        * lib/free.c (rpl_free): Preserve errno.  Check for null only if
        CANNOT_FREE_NULL is defined, as an optimization for POSIX 2008
index d090dcda1d62524cb567778e0c097099bc696f74..693044ecce90a51f84825959266f7d1d83e00b69 100644 (file)
@@ -2,18 +2,19 @@
    Copyright (C) 1996-2020 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 as published by
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful,
+   The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
 
 #ifndef _LIBC
 /* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
 # define _GL_ARG_NONNULL(params)
 
 # define _GL_USE_STDLIB_ALLOC 1
-# include <config.h>
+# include <libc-config.h>
 #endif
 
-#if !HAVE_CANONICALIZE_FILE_NAME || !FUNC_REALPATH_WORKS || defined _LIBC
-
 /* Specification.  */
 #include <stdlib.h>
 
-#include <alloca.h>
 #include <string.h>
 #include <unistd.h>
 #include <limits.h>
-#if HAVE_SYS_PARAM_H || defined _LIBC
-# include <sys/param.h>
-#endif
 #include <errno.h>
 #include <stddef.h>
 
 #ifdef _LIBC
+# include <eloop-threshold.h>
 # include <shlib-compat.h>
 typedef ptrdiff_t idx_t;
+# define FILE_SYSTEM_PREFIX_LEN(name) 0
+# define IS_ABSOLUTE_FILE_NAME(name) ISSLASH(*(name))
+# define ISSLASH(c) ((c) == '/')
+# define malloca __alloca
+# define freea(p) ((void) (p))
 #else
-# define SHLIB_COMPAT(lib, introduced, obsoleted) 0
-# define versioned_symbol(lib, local, symbol, version) extern int dummy
-# define compat_symbol(lib, local, symbol, version)
-# define weak_alias(local, symbol)
 # define __canonicalize_file_name canonicalize_file_name
 # define __realpath realpath
 # include "idx.h"
@@ -73,8 +70,10 @@ typedef ptrdiff_t idx_t;
 # else
 #  define __getcwd(buf, max) getwd (buf)
 # endif
+# define __mempcpy mempcpy
+# define __pathconf pathconf
+# define __rawmemchr rawmemchr
 # define __readlink readlink
-# define __set_errno(e) errno = (e)
 # ifndef MAXSYMLINKS
 #  ifdef SYMLOOP_MAX
 #   define MAXSYMLINKS SYMLOOP_MAX
@@ -82,29 +81,15 @@ typedef ptrdiff_t idx_t;
 #   define MAXSYMLINKS 20
 #  endif
 # endif
+# define __eloop_threshold() MAXSYMLINKS
 #endif
 
 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
 #endif
 
-/* Define this independently so that stdint.h is not a prerequisite.  */
-#ifndef SIZE_MAX
-# define SIZE_MAX ((size_t) -1)
-#endif
-
 #if !FUNC_REALPATH_WORKS || defined _LIBC
 
-static void
-alloc_failed (void)
-{
-#if defined _WIN32 && ! defined __CYGWIN__
-  /* Avoid errno problem without using the malloc or realloc modules; see:
-     https://lists.gnu.org/r/bug-gnulib/2016-08/msg00025.html  */
-  errno = ENOMEM;
-#endif
-}
-
 /* Return the canonical absolute name of file NAME.  A canonical name
    does not contain any ".", ".." components nor any repeated path
    separators ('/') or symlinks.  All path components must exist.  If
@@ -146,19 +131,16 @@ __realpath (const char *name, char *resolved)
 #ifdef PATH_MAX
   path_max = PATH_MAX;
 #else
-  path_max = pathconf (name, _PC_PATH_MAX);
+  path_max = __pathconf (name, _PC_PATH_MAX);
   if (path_max <= 0)
-    path_max = 8192;
+    path_max = 1024;
 #endif
 
   if (resolved == NULL)
     {
       rpath = malloc (path_max);
       if (rpath == NULL)
-        {
-          alloc_failed ();
-          return NULL;
-        }
+        return NULL;
     }
   else
     rpath = resolved;
@@ -175,18 +157,13 @@ __realpath (const char *name, char *resolved)
           rpath[0] = '\0';
           goto error;
         }
-      dest = strchr (rpath, '\0');
+      dest = __rawmemchr (rpath, '\0');
       start = name;
       prefix_len = FILE_SYSTEM_PREFIX_LEN (rpath);
     }
   else
     {
-      dest = rpath;
-      if (prefix_len)
-        {
-          memcpy (rpath, name, prefix_len);
-          dest += prefix_len;
-        }
+      dest = __mempcpy (rpath, name, prefix_len);
       *dest++ = '/';
       if (DOUBLE_SLASH_IS_DISTINCT_ROOT)
         {
@@ -247,22 +224,14 @@ __realpath (const char *name, char *resolved)
                 new_size += path_max;
               new_rpath = (char *) realloc (rpath, new_size);
               if (new_rpath == NULL)
-                {
-                  alloc_failed ();
-                  goto error;
-                }
+                goto error;
               rpath = new_rpath;
               rpath_limit = rpath + new_size;
 
               dest = rpath + dest_offset;
             }
 
-#ifdef _LIBC
           dest = __mempcpy (dest, start, end - start);
-#else
-          memcpy (dest, start, end - start);
-          dest += end - start;
-#endif
           *dest = '\0';
 
           char linkbuf[128];
@@ -277,7 +246,7 @@ __realpath (const char *name, char *resolved)
               char *buf;
               size_t len;
 
-              if (++num_links > MAXSYMLINKS)
+              if (++num_links > __eloop_threshold ())
                 {
                   __set_errno (ELOOP);
                   goto error;
@@ -287,10 +256,7 @@ __realpath (const char *name, char *resolved)
                 {
                   extra_buf = malloca (2 * path_max);
                   if (!extra_buf)
-                    {
-                      alloc_failed ();
-                      goto error;
-                    }
+                    goto error;
                 }
               if (n < sizeof linkbuf)
                 buf = linkbuf;
@@ -304,8 +270,7 @@ __realpath (const char *name, char *resolved)
               buf[n] = '\0';
 
               len = strlen (end);
-              /* Check that n + len + 1 doesn't overflow and is <= path_max. */
-              if (n >= SIZE_MAX - len || n + len >= path_max)
+              if (path_max - n <= len)
                 {
                   __set_errno (ENAMETOOLONG);
                   goto error;
@@ -319,9 +284,7 @@ __realpath (const char *name, char *resolved)
                 {
                   size_t pfxlen = FILE_SYSTEM_PREFIX_LEN (buf);
 
-                  if (pfxlen)
-                    memcpy (rpath, buf, pfxlen);
-                  dest = rpath + pfxlen;
+                  dest = __mempcpy (rpath, buf, pfxlen);
                   *dest++ = '/'; /* It's an absolute symlink */
                   if (DOUBLE_SLASH_IS_DISTINCT_ROOT)
                     {
@@ -359,16 +322,13 @@ __realpath (const char *name, char *resolved)
   return rpath;
 
 error:
-  {
-    int saved_errno = errno;
-    if (extra_buf)
-      freea (extra_buf);
-    if (resolved == NULL)
-      free (rpath);
-    __set_errno (saved_errno);
-  }
+  if (extra_buf)
+    freea (extra_buf);
+  if (resolved == NULL)
+    free (rpath);
   return NULL;
 }
+libc_hidden_def (__realpath)
 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
 
@@ -396,11 +356,3 @@ __canonicalize_file_name (const char *name)
   return __realpath (name, NULL);
 }
 weak_alias (__canonicalize_file_name, canonicalize_file_name)
-
-#else
-
-/* This declaration is solely to ensure that after preprocessing
-   this file is never empty.  */
-typedef int dummy;
-
-#endif
index f67ca78aa938300960a487413b85da07e8c4c2d2..83dc8f50550752ff180e071be3b11ddd2f6fd12b 100644 (file)
@@ -14,10 +14,16 @@ alloca-opt        [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONI
 double-slash-root [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
 errno             [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
 filename          [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
-idx           [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
+free              [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
+idx               [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
+libc-config       [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
 malloca           [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
+malloc-posix      [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
 memmove           [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
+mempcpy           [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
+realloc-posix     [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
 pathmax           [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
+rawmemchr         [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
 readlink          [test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1]
 
 configure.ac:
index ea2509821ff14fcbeb89bd8ee1a712342bdac04c..3c06b4756e55698bc88c0a46a3ae8306afb91e37 100644 (file)
@@ -23,7 +23,7 @@ Include:
 <stdlib.h>
 
 License:
-GPL
+LGPLv2+
 
 Maintainer:
 Paul Eggert