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. --- lib-src/emacsclient.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'lib-src/emacsclient.c') 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); -- cgit v1.2.1