summaryrefslogtreecommitdiff
path: root/lib-src
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2019-06-25 15:54:37 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2019-06-25 15:56:58 -0700
commit824f78418783ee0af1c804b0decb037a13a4365e (patch)
tree7a4127256fa1117b609b1d5c50c8490b391708e1 /lib-src
parent7dcefa7a2bb4d2531d23cbf51eb98ce7727b366c (diff)
downloademacs-824f78418783ee0af1c804b0decb037a13a4365e.tar.gz
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.
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/emacsclient.c22
1 files changed, 16 insertions, 6 deletions
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 <signal.h>
#include <stdarg.h>
#include <stddef.h>
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@@ -82,6 +83,7 @@ char *w32_getenv (const char *);
#include <dosname.h>
#include <intprops.h>
#include <min-max.h>
+#include <pathmax.h>
#include <unlocked-io.h>
/* 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);