Use PATH_MAX rather than MAXPATHLEN. Also fix a possible off by one

error caused by the -1 being on the wrong side of the comparison.
This would not cause an overflow, as near as I can tell, because we
truncate later anyway.  We'd just fail to get a diagnostic for 1024
and 1025 byte file names.
This commit is contained in:
Warner Losh 2001-05-29 18:03:14 +00:00
parent 99e8005137
commit 9842e24c34
3 changed files with 11 additions and 16 deletions

View File

@ -28,11 +28,9 @@
* $FreeBSD$ * $FreeBSD$
*/ */
#include <sys/param.h> /* for MAXPATHLEN */ #include <sys/param.h>
#include <errno.h> #include <errno.h>
#if defined(sun) || defined(__NetBSD__) #include <limits.h>
# include <limits.h>
#endif
#include <regex.h> #include <regex.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
@ -44,10 +42,6 @@
#define EMOD (-3) #define EMOD (-3)
#define FATAL (-4) #define FATAL (-4)
#ifndef MAXPATHLEN
# define MAXPATHLEN 255 /* _POSIX_PATH_MAX */
#endif
#define MINBUFSZ 512 /* minimum buffer size - must be > 0 */ #define MINBUFSZ 512 /* minimum buffer size - must be > 0 */
#define SE_MAX 30 /* max subexpressions in a regular expression */ #define SE_MAX 30 /* max subexpressions in a regular expression */
#ifdef INT_MAX #ifdef INT_MAX

View File

@ -96,7 +96,7 @@ int scripted = 0; /* if set, suppress diagnostics */
int sigflags = 0; /* if set, signals received while mutex set */ int sigflags = 0; /* if set, signals received while mutex set */
int sigactive = 0; /* if set, signal handlers are enabled */ int sigactive = 0; /* if set, signal handlers are enabled */
char old_filename[MAXPATHLEN + 1] = ""; /* default filename */ char old_filename[PATH_MAX] = ""; /* default filename */
long current_addr; /* current address in editor buffer */ long current_addr; /* current address in editor buffer */
long addr_last; /* last address in editor buffer */ long addr_last; /* last address in editor buffer */
int lineno; /* script line number */ int lineno; /* script line number */
@ -948,9 +948,10 @@ get_filename()
ibufp++; ibufp++;
if ((n = get_shell_command()) < 0) if ((n = get_shell_command()) < 0)
return NULL; return NULL;
if (n) printf("%s\n", shcmd + 1); if (n)
printf("%s\n", shcmd + 1);
return shcmd; return shcmd;
} else if (n - 1 > MAXPATHLEN) { } else if (n > PATH_MAX - 1) {
sprintf(errmsg, "filename too long"); sprintf(errmsg, "filename too long");
return NULL; return NULL;
} }
@ -961,7 +962,7 @@ get_filename()
return NULL; return NULL;
} }
#endif #endif
REALLOC(file, filesz, MAXPATHLEN + 1, NULL); REALLOC(file, filesz, PATH_MAX, NULL);
for (n = 0; *ibufp != '\n';) for (n = 0; *ibufp != '\n';)
file[n++] = *ibufp++; file[n++] = *ibufp++;
file[n] = '\0'; file[n] = '\0';
@ -1338,7 +1339,7 @@ has_trailing_escape(s, t)
} }
/* strip_escapes: return copy of escaped string of at most length MAXPATHLEN */ /* strip_escapes: return copy of escaped string of at most length PATH_MAX */
char * char *
strip_escapes(s) strip_escapes(s)
char *s; char *s;
@ -1348,7 +1349,7 @@ strip_escapes(s)
int i = 0; int i = 0;
REALLOC(file, filesz, MAXPATHLEN + 1, NULL); REALLOC(file, filesz, PATH_MAX, NULL);
while (i < filesz - 1 /* Worry about a possible trailing escape */ while (i < filesz - 1 /* Worry about a possible trailing escape */
&& (file[i++] = (*s == '\\') ? *++s : *s)) && (file[i++] = (*s == '\\') ? *++s : *s))
s++; s++;
@ -1391,7 +1392,7 @@ handle_hup(signo)
sigflags &= ~(1 << (signo - 1)); sigflags &= ~(1 << (signo - 1));
if (addr_last && write_file("ed.hup", "w", 1, addr_last) < 0 && if (addr_last && write_file("ed.hup", "w", 1, addr_last) < 0 &&
(s = getenv("HOME")) != NULL && (s = getenv("HOME")) != NULL &&
(n = strlen(s)) + 8 <= MAXPATHLEN && /* "ed.hup" + '/' */ (n = strlen(s)) + 8 <= PATH_MAX && /* "ed.hup" + '/' */
(hup = (char *) malloc(n + 10)) != NULL) { (hup = (char *) malloc(n + 10)) != NULL) {
strcpy(hup, s); strcpy(hup, s);
if (hup[n - 1] != '/') if (hup[n - 1] != '/')

View File

@ -40,7 +40,7 @@ static char * const rcsid =
extern int patlock; extern int patlock;
char errmsg[MAXPATHLEN + 40] = ""; char errmsg[PATH_MAX + 40] = "";
/* get_compiled_pattern: return pointer to compiled pattern from command /* get_compiled_pattern: return pointer to compiled pattern from command
buffer */ buffer */