diff options
| author | Paul Eggert | 2019-06-25 15:54:37 -0700 |
|---|---|---|
| committer | Paul Eggert | 2019-06-25 15:56:58 -0700 |
| commit | 824f78418783ee0af1c804b0decb037a13a4365e (patch) | |
| tree | 7a4127256fa1117b609b1d5c50c8490b391708e1 /lib-src | |
| parent | 7dcefa7a2bb4d2531d23cbf51eb98ce7727b366c (diff) | |
| download | emacs-824f78418783ee0af1c804b0decb037a13a4365e.tar.gz emacs-824f78418783ee0af1c804b0decb037a13a4365e.zip | |
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.c | 22 |
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 *); | |||
| 74 | #include <signal.h> | 74 | #include <signal.h> |
| 75 | #include <stdarg.h> | 75 | #include <stdarg.h> |
| 76 | #include <stddef.h> | 76 | #include <stddef.h> |
| 77 | #include <stdint.h> | ||
| 77 | #include <stdlib.h> | 78 | #include <stdlib.h> |
| 78 | #include <string.h> | 79 | #include <string.h> |
| 79 | #include <sys/stat.h> | 80 | #include <sys/stat.h> |
| @@ -82,6 +83,7 @@ char *w32_getenv (const char *); | |||
| 82 | #include <dosname.h> | 83 | #include <dosname.h> |
| 83 | #include <intprops.h> | 84 | #include <intprops.h> |
| 84 | #include <min-max.h> | 85 | #include <min-max.h> |
| 86 | #include <pathmax.h> | ||
| 85 | #include <unlocked-io.h> | 87 | #include <unlocked-io.h> |
| 86 | 88 | ||
| 87 | /* Work around GCC bug 88251. */ | 89 | /* Work around GCC bug 88251. */ |
| @@ -238,6 +240,17 @@ char *get_current_dir_name (void); | |||
| 238 | char * | 240 | char * |
| 239 | get_current_dir_name (void) | 241 | get_current_dir_name (void) |
| 240 | { | 242 | { |
| 243 | /* The maximum size of a directory name, including the terminating NUL. | ||
| 244 | Leave room so that the caller can append a trailing slash. */ | ||
| 245 | ptrdiff_t dirsize_max = min (PTRDIFF_MAX, SIZE_MAX) - 1; | ||
| 246 | |||
| 247 | /* The maximum size of a buffer for a file name, including the | ||
| 248 | terminating NUL. This is bounded by PATH_MAX, if available. */ | ||
| 249 | ptrdiff_t bufsize_max = dirsize_max; | ||
| 250 | #ifdef PATH_MAX | ||
| 251 | bufsize_max = min (bufsize_max, PATH_MAX); | ||
| 252 | #endif | ||
| 253 | |||
| 241 | char *buf; | 254 | char *buf; |
| 242 | struct stat dotstat, pwdstat; | 255 | struct stat dotstat, pwdstat; |
| 243 | /* If PWD is accurate, use it instead of calling getcwd. PWD is | 256 | /* If PWD is accurate, use it instead of calling getcwd. PWD is |
| @@ -245,15 +258,12 @@ get_current_dir_name (void) | |||
| 245 | parent directory is searchable but not readable. */ | 258 | parent directory is searchable but not readable. */ |
| 246 | char const *pwd = egetenv ("PWD"); | 259 | char const *pwd = egetenv ("PWD"); |
| 247 | if (pwd | 260 | if (pwd |
| 248 | && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1]))) | 261 | && (pwdlen = strnlen (pwd, bufsize_max)) < bufsize_max |
| 262 | && IS_DIRECTORY_SEP (pwd[pwdlen && IS_DEVICE_SEP (pwd[1]) ? 2 : 0]) | ||
| 249 | && stat (pwd, &pwdstat) == 0 | 263 | && stat (pwd, &pwdstat) == 0 |
| 250 | && stat (".", &dotstat) == 0 | 264 | && stat (".", &dotstat) == 0 |
| 251 | && dotstat.st_ino == pwdstat.st_ino | 265 | && dotstat.st_ino == pwdstat.st_ino |
| 252 | && dotstat.st_dev == pwdstat.st_dev | 266 | && dotstat.st_dev == pwdstat.st_dev) |
| 253 | # ifdef MAXPATHLEN | ||
| 254 | && strlen (pwd) < MAXPATHLEN | ||
| 255 | # endif | ||
| 256 | ) | ||
| 257 | { | 267 | { |
| 258 | buf = xmalloc (strlen (pwd) + 1); | 268 | buf = xmalloc (strlen (pwd) + 1); |
| 259 | strcpy (buf, pwd); | 269 | strcpy (buf, pwd); |