Commit 824f7841 authored by Paul Eggert's avatar Paul Eggert

Prefer PATH_MAX to MAXPATHLEN

PATH_MAX is standardized, MAXPATHLEN is not.
Also, the Gnulib pathmax module fixes some rare bugs with PATH_MAX.
So prefer PATH_MAX to MAXPATHLEN unless we know the latter is
also correct (for some platform-specific code).
* admin/merge-gnulib (GNULIB_MODULES): Add pathmax.
This module was already present, as a dependency of canonicalize-lgpl,
but now Emacs is using it directly.  Sort.
* lib-src/emacsclient.c: Include stdint.h, pathmax.h.
(get_current_dir_name): Sync to current src/sysdep.c.
* lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
* src/sysdep.c: Include pathmax.h.
(get_current_dir_name_or_unreachable):
Use PATH_MAX instead of MAXPATHLEN.
parent 7dcefa7a
...@@ -27,9 +27,9 @@ GNULIB_URL=git://git.savannah.gnu.org/gnulib.git ...@@ -27,9 +27,9 @@ GNULIB_URL=git://git.savannah.gnu.org/gnulib.git
GNULIB_MODULES=' GNULIB_MODULES='
alloca-opt binary-io byteswap c-ctype c-strcase alloca-opt binary-io byteswap c-ctype c-strcase
canonicalize-lgpl
careadlinkat close-stream copy-file-range careadlinkat close-stream copy-file-range
count-leading-zeros count-one-bits count-trailing-zeros count-leading-zeros count-one-bits count-trailing-zeros
canonicalize-lgpl
crypto/md5-buffer crypto/sha1-buffer crypto/sha256-buffer crypto/sha512-buffer crypto/md5-buffer crypto/sha1-buffer crypto/sha256-buffer crypto/sha512-buffer
d-type diffseq dosname dtoastr dtotimespec dup2 d-type diffseq dosname dtoastr dtotimespec dup2
environ execinfo explicit_bzero faccessat environ execinfo explicit_bzero faccessat
...@@ -38,7 +38,8 @@ GNULIB_MODULES=' ...@@ -38,7 +38,8 @@ GNULIB_MODULES='
getloadavg getopt-gnu gettime gettimeofday gitlog-to-changelog getloadavg getopt-gnu gettime gettimeofday gitlog-to-changelog
ieee754-h ignore-value intprops largefile lstat ieee754-h ignore-value intprops largefile lstat
manywarnings memmem-simple memrchr minmax mkostemp mktime nstrftime manywarnings memmem-simple memrchr minmax mkostemp mktime nstrftime
pipe2 pselect pthread_sigmask putenv qcopy-acl readlink readlinkat regex pathmax pipe2 pselect pthread_sigmask putenv
qcopy-acl readlink readlinkat regex
sig2str socklen stat-time std-gnu11 stdalign stddef stdio sig2str socklen stat-time std-gnu11 stdalign stddef stdio
stpcpy strnlen strtoimax symlink sys_stat sys_time stpcpy strnlen strtoimax symlink sys_stat sys_time
tempname time time_r time_rz timegm timer-time timespec-add timespec-sub tempname time time_r time_rz timegm timer-time timespec-add timespec-sub
......
...@@ -74,6 +74,7 @@ char *w32_getenv (const char *); ...@@ -74,6 +74,7 @@ char *w32_getenv (const char *);
#include <signal.h> #include <signal.h>
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -82,6 +83,7 @@ char *w32_getenv (const char *); ...@@ -82,6 +83,7 @@ char *w32_getenv (const char *);
#include <dosname.h> #include <dosname.h>
#include <intprops.h> #include <intprops.h>
#include <min-max.h> #include <min-max.h>
#include <pathmax.h>
#include <unlocked-io.h> #include <unlocked-io.h>
/* Work around GCC bug 88251. */ /* Work around GCC bug 88251. */
...@@ -238,6 +240,17 @@ char *get_current_dir_name (void); ...@@ -238,6 +240,17 @@ char *get_current_dir_name (void);
char * char *
get_current_dir_name (void) get_current_dir_name (void)
{ {
/* The maximum size of a directory name, including the terminating NUL.
Leave room so that the caller can append a trailing slash. */
ptrdiff_t dirsize_max = min (PTRDIFF_MAX, SIZE_MAX) - 1;
/* The maximum size of a buffer for a file name, including the
terminating NUL. This is bounded by PATH_MAX, if available. */
ptrdiff_t bufsize_max = dirsize_max;
#ifdef PATH_MAX
bufsize_max = min (bufsize_max, PATH_MAX);
#endif
char *buf; char *buf;
struct stat dotstat, pwdstat; struct stat dotstat, pwdstat;
/* If PWD is accurate, use it instead of calling getcwd. PWD is /* If PWD is accurate, use it instead of calling getcwd. PWD is
...@@ -245,15 +258,12 @@ get_current_dir_name (void) ...@@ -245,15 +258,12 @@ get_current_dir_name (void)
parent directory is searchable but not readable. */ parent directory is searchable but not readable. */
char const *pwd = egetenv ("PWD"); char const *pwd = egetenv ("PWD");
if (pwd if (pwd
&& (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1]))) && (pwdlen = strnlen (pwd, bufsize_max)) < bufsize_max
&& IS_DIRECTORY_SEP (pwd[pwdlen && IS_DEVICE_SEP (pwd[1]) ? 2 : 0])
&& stat (pwd, &pwdstat) == 0 && stat (pwd, &pwdstat) == 0
&& stat (".", &dotstat) == 0 && stat (".", &dotstat) == 0
&& dotstat.st_ino == pwdstat.st_ino && dotstat.st_ino == pwdstat.st_ino
&& dotstat.st_dev == pwdstat.st_dev && dotstat.st_dev == pwdstat.st_dev)
# ifdef MAXPATHLEN
&& strlen (pwd) < MAXPATHLEN
# endif
)
{ {
buf = xmalloc (strlen (pwd) + 1); buf = xmalloc (strlen (pwd) + 1);
strcpy (buf, pwd); strcpy (buf, pwd);
......
...@@ -120,6 +120,7 @@ ...@@ -120,6 +120,7 @@
# mkostemp \ # mkostemp \
# mktime \ # mktime \
# nstrftime \ # nstrftime \
# pathmax \
# pipe2 \ # pipe2 \
# pselect \ # pselect \
# pthread_sigmask \ # pthread_sigmask \
...@@ -1076,6 +1077,7 @@ gl_GNULIB_ENABLED_getdtablesize = @gl_GNULIB_ENABLED_getdtablesize@ ...@@ -1076,6 +1077,7 @@ gl_GNULIB_ENABLED_getdtablesize = @gl_GNULIB_ENABLED_getdtablesize@
gl_GNULIB_ENABLED_getgroups = @gl_GNULIB_ENABLED_getgroups@ gl_GNULIB_ENABLED_getgroups = @gl_GNULIB_ENABLED_getgroups@
gl_GNULIB_ENABLED_malloca = @gl_GNULIB_ENABLED_malloca@ gl_GNULIB_ENABLED_malloca = @gl_GNULIB_ENABLED_malloca@
gl_GNULIB_ENABLED_open = @gl_GNULIB_ENABLED_open@ gl_GNULIB_ENABLED_open = @gl_GNULIB_ENABLED_open@
gl_GNULIB_ENABLED_pathmax = @gl_GNULIB_ENABLED_pathmax@
gl_GNULIB_ENABLED_strtoll = @gl_GNULIB_ENABLED_strtoll@ gl_GNULIB_ENABLED_strtoll = @gl_GNULIB_ENABLED_strtoll@
gl_LIBOBJS = @gl_LIBOBJS@ gl_LIBOBJS = @gl_LIBOBJS@
gl_LTLIBOBJS = @gl_LTLIBOBJS@ gl_LTLIBOBJS = @gl_LTLIBOBJS@
...@@ -2110,9 +2112,7 @@ endif ...@@ -2110,9 +2112,7 @@ endif
## begin gnulib module pathmax ## begin gnulib module pathmax
ifeq (,$(OMIT_GNULIB_MODULE_pathmax)) ifeq (,$(OMIT_GNULIB_MODULE_pathmax))
ifneq (,$(gl_GNULIB_ENABLED_pathmax))
endif
EXTRA_DIST += pathmax.h EXTRA_DIST += pathmax.h
endif endif
......
...@@ -347,6 +347,7 @@ AC_DEFUN([gl_INIT], ...@@ -347,6 +347,7 @@ AC_DEFUN([gl_INIT],
gl_TIME_MODULE_INDICATOR([mktime]) gl_TIME_MODULE_INDICATOR([mktime])
gl_MULTIARCH gl_MULTIARCH
gl_FUNC_GNU_STRFTIME gl_FUNC_GNU_STRFTIME
gl_PATHMAX
gl_FUNC_PIPE2 gl_FUNC_PIPE2
gl_UNISTD_MODULE_INDICATOR([pipe2]) gl_UNISTD_MODULE_INDICATOR([pipe2])
gl_FUNC_PSELECT gl_FUNC_PSELECT
...@@ -468,7 +469,6 @@ AC_DEFUN([gl_INIT], ...@@ -468,7 +469,6 @@ AC_DEFUN([gl_INIT],
gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31=false gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31=false
gl_gnulib_enabled_open=false gl_gnulib_enabled_open=false
gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7=false gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7=false
gl_gnulib_enabled_pathmax=false
gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c=false gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c=false
gl_gnulib_enabled_strtoll=false gl_gnulib_enabled_strtoll=false
gl_gnulib_enabled_682e609604ccaac6be382e4ee3a4eaec=false gl_gnulib_enabled_682e609604ccaac6be382e4ee3a4eaec=false
...@@ -620,13 +620,6 @@ AC_DEFUN([gl_INIT], ...@@ -620,13 +620,6 @@ AC_DEFUN([gl_INIT],
gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7=true gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7=true
fi fi
} }
func_gl_gnulib_m4code_pathmax ()
{
if ! $gl_gnulib_enabled_pathmax; then
gl_PATHMAX
gl_gnulib_enabled_pathmax=true
fi
}
func_gl_gnulib_m4code_6099e9737f757db36c47fa9d9f02e88c () func_gl_gnulib_m4code_6099e9737f757db36c47fa9d9f02e88c ()
{ {
if ! $gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c; then if ! $gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c; then
...@@ -654,9 +647,6 @@ AC_DEFUN([gl_INIT], ...@@ -654,9 +647,6 @@ AC_DEFUN([gl_INIT],
if test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1; then if test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1; then
func_gl_gnulib_m4code_malloca func_gl_gnulib_m4code_malloca
fi fi
if test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1; then
func_gl_gnulib_m4code_pathmax
fi
if test $HAVE_FACCESSAT = 0 || test $REPLACE_FACCESSAT = 1; then if test $HAVE_FACCESSAT = 0 || test $REPLACE_FACCESSAT = 1; then
func_gl_gnulib_m4code_260941c0e5dc67ec9e87d1fb321c300b func_gl_gnulib_m4code_260941c0e5dc67ec9e87d1fb321c300b
fi fi
...@@ -720,7 +710,6 @@ AC_DEFUN([gl_INIT], ...@@ -720,7 +710,6 @@ AC_DEFUN([gl_INIT],
AM_CONDITIONAL([gl_GNULIB_ENABLED_5264294aa0a5557541b53c8c741f7f31], [$gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31]) AM_CONDITIONAL([gl_GNULIB_ENABLED_5264294aa0a5557541b53c8c741f7f31], [$gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31])
AM_CONDITIONAL([gl_GNULIB_ENABLED_open], [$gl_gnulib_enabled_open]) AM_CONDITIONAL([gl_GNULIB_ENABLED_open], [$gl_gnulib_enabled_open])
AM_CONDITIONAL([gl_GNULIB_ENABLED_03e0aaad4cb89ca757653bd367a6ccb7], [$gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7]) AM_CONDITIONAL([gl_GNULIB_ENABLED_03e0aaad4cb89ca757653bd367a6ccb7], [$gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7])
AM_CONDITIONAL([gl_GNULIB_ENABLED_pathmax], [$gl_gnulib_enabled_pathmax])
AM_CONDITIONAL([gl_GNULIB_ENABLED_6099e9737f757db36c47fa9d9f02e88c], [$gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c]) AM_CONDITIONAL([gl_GNULIB_ENABLED_6099e9737f757db36c47fa9d9f02e88c], [$gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c])
AM_CONDITIONAL([gl_GNULIB_ENABLED_strtoll], [$gl_gnulib_enabled_strtoll]) AM_CONDITIONAL([gl_GNULIB_ENABLED_strtoll], [$gl_gnulib_enabled_strtoll])
AM_CONDITIONAL([gl_GNULIB_ENABLED_682e609604ccaac6be382e4ee3a4eaec], [$gl_gnulib_enabled_682e609604ccaac6be382e4ee3a4eaec]) AM_CONDITIONAL([gl_GNULIB_ENABLED_682e609604ccaac6be382e4ee3a4eaec], [$gl_gnulib_enabled_682e609604ccaac6be382e4ee3a4eaec])
......
...@@ -30,6 +30,7 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */ ...@@ -30,6 +30,7 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <unistd.h> #include <unistd.h>
#include <c-ctype.h> #include <c-ctype.h>
#include <pathmax.h>
#include <utimens.h> #include <utimens.h>
#include "lisp.h" #include "lisp.h"
...@@ -262,10 +263,10 @@ get_current_dir_name_or_unreachable (void) ...@@ -262,10 +263,10 @@ get_current_dir_name_or_unreachable (void)
ptrdiff_t dirsize_max = min (PTRDIFF_MAX, SIZE_MAX) - 1; ptrdiff_t dirsize_max = min (PTRDIFF_MAX, SIZE_MAX) - 1;
/* The maximum size of a buffer for a file name, including the /* The maximum size of a buffer for a file name, including the
terminating NUL. This is bounded by MAXPATHLEN, if available. */ terminating NUL. This is bounded by PATH_MAX, if available. */
ptrdiff_t bufsize_max = dirsize_max; ptrdiff_t bufsize_max = dirsize_max;
#ifdef MAXPATHLEN #ifdef PATH_MAX
bufsize_max = min (bufsize_max, MAXPATHLEN); bufsize_max = min (bufsize_max, PATH_MAX);
#endif #endif
# if HAVE_GET_CURRENT_DIR_NAME && !BROKEN_GET_CURRENT_DIR_NAME # if HAVE_GET_CURRENT_DIR_NAME && !BROKEN_GET_CURRENT_DIR_NAME
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment