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.
This commit is contained in:
Paul Eggert 2020-12-25 01:38:31 -08:00
parent b8b17038e1
commit ec8a17e938
7 changed files with 31 additions and 67 deletions

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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;
}

View file

@ -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 ()

View file

@ -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 */
}

View file

@ -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;
}
}
}