From 824f78418783ee0af1c804b0decb037a13a4365e Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 25 Jun 2019 15:54:37 -0700 Subject: 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. --- admin/merge-gnulib | 5 +++-- lib-src/emacsclient.c | 22 ++++++++++++++++------ lib/gnulib.mk.in | 4 ++-- m4/gnulib-comp.m4 | 13 +------------ src/sysdep.c | 7 ++++--- 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/admin/merge-gnulib b/admin/merge-gnulib index 6cf29589f71..42e9c0c8645 100755 --- a/admin/merge-gnulib +++ b/admin/merge-gnulib @@ -27,9 +27,9 @@ GNULIB_URL=git://git.savannah.gnu.org/gnulib.git GNULIB_MODULES=' alloca-opt binary-io byteswap c-ctype c-strcase + canonicalize-lgpl careadlinkat close-stream copy-file-range count-leading-zeros count-one-bits count-trailing-zeros - canonicalize-lgpl crypto/md5-buffer crypto/sha1-buffer crypto/sha256-buffer crypto/sha512-buffer d-type diffseq dosname dtoastr dtotimespec dup2 environ execinfo explicit_bzero faccessat @@ -38,7 +38,8 @@ GNULIB_MODULES=' getloadavg getopt-gnu gettime gettimeofday gitlog-to-changelog ieee754-h ignore-value intprops largefile lstat 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 stpcpy strnlen strtoimax symlink sys_stat sys_time tempname time time_r time_rz timegm timer-time timespec-add timespec-sub diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c index 4da532b42de..6c806fb5830 100644 --- a/lib-src/emacsclient.c +++ b/lib-src/emacsclient.c @@ -74,6 +74,7 @@ char *w32_getenv (const char *); #include #include #include +#include #include #include #include @@ -82,6 +83,7 @@ char *w32_getenv (const char *); #include #include #include +#include #include /* Work around GCC bug 88251. */ @@ -238,6 +240,17 @@ char *get_current_dir_name (void); char * 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; struct stat dotstat, pwdstat; /* If PWD is accurate, use it instead of calling getcwd. PWD is @@ -245,15 +258,12 @@ get_current_dir_name (void) parent directory is searchable but not readable. */ char const *pwd = egetenv ("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 (".", &dotstat) == 0 && dotstat.st_ino == pwdstat.st_ino - && dotstat.st_dev == pwdstat.st_dev -# ifdef MAXPATHLEN - && strlen (pwd) < MAXPATHLEN -# endif - ) + && dotstat.st_dev == pwdstat.st_dev) { buf = xmalloc (strlen (pwd) + 1); strcpy (buf, pwd); diff --git a/lib/gnulib.mk.in b/lib/gnulib.mk.in index b4d510bd62e..6f978aeb0cc 100644 --- a/lib/gnulib.mk.in +++ b/lib/gnulib.mk.in @@ -120,6 +120,7 @@ # mkostemp \ # mktime \ # nstrftime \ +# pathmax \ # pipe2 \ # pselect \ # pthread_sigmask \ @@ -1076,6 +1077,7 @@ gl_GNULIB_ENABLED_getdtablesize = @gl_GNULIB_ENABLED_getdtablesize@ gl_GNULIB_ENABLED_getgroups = @gl_GNULIB_ENABLED_getgroups@ gl_GNULIB_ENABLED_malloca = @gl_GNULIB_ENABLED_malloca@ 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_LIBOBJS = @gl_LIBOBJS@ gl_LTLIBOBJS = @gl_LTLIBOBJS@ @@ -2110,9 +2112,7 @@ endif ## begin gnulib module pathmax ifeq (,$(OMIT_GNULIB_MODULE_pathmax)) -ifneq (,$(gl_GNULIB_ENABLED_pathmax)) -endif EXTRA_DIST += pathmax.h endif diff --git a/m4/gnulib-comp.m4 b/m4/gnulib-comp.m4 index 7e724fe3c4e..d6b2009123d 100644 --- a/m4/gnulib-comp.m4 +++ b/m4/gnulib-comp.m4 @@ -347,6 +347,7 @@ AC_DEFUN([gl_INIT], gl_TIME_MODULE_INDICATOR([mktime]) gl_MULTIARCH gl_FUNC_GNU_STRFTIME + gl_PATHMAX gl_FUNC_PIPE2 gl_UNISTD_MODULE_INDICATOR([pipe2]) gl_FUNC_PSELECT @@ -468,7 +469,6 @@ AC_DEFUN([gl_INIT], gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31=false gl_gnulib_enabled_open=false gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7=false - gl_gnulib_enabled_pathmax=false gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c=false gl_gnulib_enabled_strtoll=false gl_gnulib_enabled_682e609604ccaac6be382e4ee3a4eaec=false @@ -620,13 +620,6 @@ AC_DEFUN([gl_INIT], gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7=true 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 () { if ! $gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c; then @@ -654,9 +647,6 @@ AC_DEFUN([gl_INIT], if test $HAVE_CANONICALIZE_FILE_NAME = 0 || test $REPLACE_CANONICALIZE_FILE_NAME = 1; then func_gl_gnulib_m4code_malloca 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 func_gl_gnulib_m4code_260941c0e5dc67ec9e87d1fb321c300b fi @@ -720,7 +710,6 @@ AC_DEFUN([gl_INIT], 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_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_strtoll], [$gl_gnulib_enabled_strtoll]) AM_CONDITIONAL([gl_GNULIB_ENABLED_682e609604ccaac6be382e4ee3a4eaec], [$gl_gnulib_enabled_682e609604ccaac6be382e4ee3a4eaec]) diff --git a/src/sysdep.c b/src/sysdep.c index b702bae5818..f7fc99f147f 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -30,6 +30,7 @@ along with GNU Emacs. If not, see . */ #include #include +#include #include #include "lisp.h" @@ -262,10 +263,10 @@ get_current_dir_name_or_unreachable (void) 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 MAXPATHLEN, if available. */ + terminating NUL. This is bounded by PATH_MAX, if available. */ ptrdiff_t bufsize_max = dirsize_max; -#ifdef MAXPATHLEN - bufsize_max = min (bufsize_max, MAXPATHLEN); +#ifdef PATH_MAX + bufsize_max = min (bufsize_max, PATH_MAX); #endif # if HAVE_GET_CURRENT_DIR_NAME && !BROKEN_GET_CURRENT_DIR_NAME -- cgit v1.2.1