emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master ec8a17e 2/2: Adjust to recent Gnulib changes


From: Paul Eggert
Subject: master ec8a17e 2/2: Adjust to recent Gnulib changes
Date: Fri, 25 Dec 2020 04:40:47 -0500 (EST)

branch: master
commit ec8a17e938c3ef213709ea6b6b3e565333a9c508
Author: Paul Eggert <eggert@cs.ucla.edu>
Commit: Paul Eggert <eggert@cs.ucla.edu>

    Adjust to recent Gnulib changes
    
    The latest Gnulib merge brought in free-posix, which causes 'free'
    to preserve errno.  This lets us simplify some Emacs code that
    calls 'free'.
    * admin/merge-gnulib (GNULIB_MODULES): Add free-posix.
    This module is pulled in by canonicalize-lgpl anyway,
    so we might as well rely on it.
    * lib-src/emacsclient.c (get_current_dir_name):
    Sync better with src/sysdep.c.
    * lib-src/etags.c (process_file_name, etags_mktmp):
    * lib-src/update-game-score.c (unlock_file):
    * src/fileio.c (file_accessible_directory_p):
    * src/sysdep.c (get_current_dir_name_or_unreachable):
    Simplify by assuming that 'free' preserves errno.
    * src/alloc.c (malloc_unblock_input):
    Preserve errno, so that xfree preserves errno.
    * src/sysdep.c (get_current_dir_name_or_unreachable):
    Simplify by using strdup instead of malloc+memcpy.
    No need for realloc (and the old code leaked memory anyway on
    failure); just use free+malloc.
---
 admin/merge-gnulib          |  3 ++-
 lib-src/emacsclient.c       | 39 ++++++++++++---------------------------
 lib-src/etags.c             | 17 +++--------------
 lib-src/update-game-score.c |  4 ++--
 src/alloc.c                 |  6 +++++-
 src/fileio.c                |  3 ---
 src/sysdep.c                | 26 +++++++-------------------
 7 files changed, 31 insertions(+), 67 deletions(-)

diff --git a/admin/merge-gnulib b/admin/merge-gnulib
index 164300e..880dc5e 100755
--- a/admin/merge-gnulib
+++ b/admin/merge-gnulib
@@ -34,7 +34,8 @@ GNULIB_MODULES='
   d-type diffseq double-slash-root dtoastr dtotimespec dup2
   environ execinfo explicit_bzero faccessat
   fchmodat fcntl fcntl-h fdopendir
-  filemode filename filevercmp flexmember fpieee fstatat fsusage fsync futimens
+  filemode filename filevercmp flexmember fpieee
+  free-posix fstatat fsusage fsync futimens
   getloadavg getopt-gnu getrandom gettime gettimeofday gitlog-to-changelog
   ieee754-h ignore-value intprops largefile libgmp lstat
   manywarnings memmem-simple mempcpy memrchr minmax mkostemp mktime nstrftime
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 871fa7a..8d184e2 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -251,7 +251,6 @@ get_current_dir_name (void)
   bufsize_max = min (bufsize_max, PATH_MAX);
 #endif
 
-  char *buf;
   struct stat dotstat, pwdstat;
   size_t pwdlen;
   /* If PWD is accurate, use it instead of calling getcwd.  PWD is
@@ -265,37 +264,23 @@ get_current_dir_name (void)
       && stat (".", &dotstat) == 0
       && dotstat.st_ino == pwdstat.st_ino
       && dotstat.st_dev == pwdstat.st_dev)
-    {
-      buf = xmalloc (strlen (pwd) + 1);
-      strcpy (buf, pwd);
-    }
+    return strdup (pwd);
   else
     {
-      size_t buf_size = 1024;
+      ptrdiff_t buf_size = min (bufsize_max, 1024);
       for (;;)
-        {
-         int tmp_errno;
-         buf = malloc (buf_size);
-         if (! buf)
-           break;
-          if (getcwd (buf, buf_size) == buf)
-            break;
-         tmp_errno = errno;
+       {
+         char *buf = malloc (buf_size);
+         if (!buf)
+           return NULL;
+         if (getcwd (buf, buf_size) == buf)
+           return buf;
          free (buf);
-         if (tmp_errno != ERANGE)
-            {
-              errno = tmp_errno;
-              return NULL;
-            }
-          buf_size *= 2;
-         if (! buf_size)
-           {
-             errno = ENOMEM;
-             return NULL;
-           }
-        }
+         if (errno != ERANGE || buf_size == bufsize_max)
+           return NULL;
+         buf_size = buf_size <= bufsize_max / 2 ? 2 * buf_size : bufsize_max;
+       }
     }
-  return buf;
 }
 #endif
 
diff --git a/lib-src/etags.c b/lib-src/etags.c
index a1c6837..071892e 100644
--- a/lib-src/etags.c
+++ b/lib-src/etags.c
@@ -1643,19 +1643,10 @@ process_file_name (char *file, language *lang)
          char *cmd = concat (cmd1, "' > ", tmp_name);
 #endif
          free (cmd1);
-         int tmp_errno;
-         if (system (cmd) == -1)
-           {
-             inf = NULL;
-             tmp_errno = EINVAL;
-           }
-         else
-           {
-             inf = fopen (tmp_name, "r" FOPEN_BINARY);
-             tmp_errno = errno;
-           }
+         inf = (system (cmd) == -1
+                ? NULL
+                : fopen (tmp_name, "r" FOPEN_BINARY));
          free (cmd);
-         errno = tmp_errno;
        }
 
       if (!inf)
@@ -7068,9 +7059,7 @@ etags_mktmp (void)
   int fd = mkostemp (templt, O_CLOEXEC);
   if (fd < 0 || close (fd) != 0)
     {
-      int temp_errno = errno;
       free (templt);
-      errno = temp_errno;
       templt = NULL;
     }
 #if defined (DOS_NT)
diff --git a/lib-src/update-game-score.c b/lib-src/update-game-score.c
index 93aa039..fc6e728 100644
--- a/lib-src/update-game-score.c
+++ b/lib-src/update-game-score.c
@@ -499,9 +499,9 @@ unlock_file (const char *filename, void *state)
   char *lockpath = (char *) state;
   int saved_errno = errno;
   int ret = unlink (lockpath);
-  int unlink_errno = errno;
+  if (0 <= ret)
+    errno = saved_errno;
   free (lockpath);
-  errno = ret < 0 ? unlink_errno : saved_errno;
   return ret;
 }
 
diff --git a/src/alloc.c b/src/alloc.c
index adbfa18..0b387dd 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -732,7 +732,11 @@ static void
 malloc_unblock_input (void)
 {
   if (block_input_in_memory_allocators)
-    unblock_input ();
+    {
+      int err = errno;
+      unblock_input ();
+      errno = err;
+    }
 }
 # define MALLOC_BLOCK_INPUT malloc_block_input ()
 # define MALLOC_UNBLOCK_INPUT malloc_unblock_input ()
diff --git a/src/fileio.c b/src/fileio.c
index 651e765..23b4523 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -3050,7 +3050,6 @@ file_accessible_directory_p (Lisp_Object file)
   ptrdiff_t len = SBYTES (file);
   char const *dir;
   bool ok;
-  int saved_errno;
   USE_SAFE_ALLOCA;
 
   /* Normally a file "FOO" is an accessible directory if "FOO/." exists.
@@ -3075,9 +3074,7 @@ file_accessible_directory_p (Lisp_Object file)
     }
 
   ok = file_access_p (dir, F_OK);
-  saved_errno = errno;
   SAFE_FREE ();
-  errno = saved_errno;
   return ok;
 #endif /* !DOS_NT */
 }
diff --git a/src/sysdep.c b/src/sysdep.c
index 29c88f5..eeb9d18 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -314,33 +314,21 @@ get_current_dir_name_or_unreachable (void)
       && emacs_fstatat (AT_FDCWD, ".", &dotstat, 0) == 0
       && dotstat.st_ino == pwdstat.st_ino
       && dotstat.st_dev == pwdstat.st_dev)
-    {
-      char *buf = malloc (pwdlen + 1);
-      if (!buf)
-        return NULL;
-      return memcpy (buf, pwd, pwdlen + 1);
-    }
+    return strdup (pwd);
   else
     {
       ptrdiff_t buf_size = min (bufsize_max, 1024);
-      char *buf = malloc (buf_size);
-      if (!buf)
-        return NULL;
       for (;;)
         {
+         char *buf = malloc (buf_size);
+         if (!buf)
+           return NULL;
           if (getcwd (buf, buf_size) == buf)
            return buf;
-         int getcwd_errno = errno;
-         if (getcwd_errno != ERANGE || buf_size == bufsize_max)
-            {
-              free (buf);
-             errno = getcwd_errno;
-              return NULL;
-            }
+         free (buf);
+         if (errno != ERANGE || buf_size == bufsize_max)
+           return NULL;
          buf_size = buf_size <= bufsize_max / 2 ? 2 * buf_size : bufsize_max;
-          buf = realloc (buf, buf_size);
-          if (!buf)
-            return NULL;
         }
     }
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]