1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-12-18 07:16:23 -05:00

Fix potential overflow during file look-up.

This commit is contained in:
Renaud 2015-03-26 15:56:33 +08:00
parent 8597e3adcd
commit e3b6d1e076

32
flook.c
View File

@ -77,18 +77,26 @@ boolean fexist( const char *fname)
char *flook( const char *fname, boolean hflag) char *flook( const char *fname, boolean hflag)
{ {
unsigned i ; /* index */ unsigned i ; /* index */
int len ;
static char fspec[NSTRING]; /* full path spec to search */ static char fspec[NSTRING]; /* full path spec to search */
#if ENVFUNC #if ENVFUNC
char *path; /* environmental PATH variable */ char *path; /* environmental PATH variable */
#endif
len = sizeof fspec - strlen( fname) - 1 ;
if( len < 0)
return NULL ;
#if ENVFUNC
if (hflag) { if (hflag) {
char *home; /* path to home directory */ char *home; /* path to home directory */
home = getenv("HOME"); home = getenv("HOME");
if (home != NULL) { if (home != NULL) {
if( len > (int) strlen( home) + 1) {
/* build home dir file spec */ /* build home dir file spec */
strcpy(fspec, home); strcpy( fspec, home) ;
strcat(fspec, "/"); strcat(fspec, "/");
strcat(fspec, fname); strcat(fspec, fname);
@ -97,12 +105,15 @@ char *flook( const char *fname, boolean hflag)
return fspec ; return fspec ;
} }
} }
}
#endif #endif
/* always try the current directory first */ /* always try the current directory first */
if( len >= 0) {
strcpy( fspec, fname) ; strcpy( fspec, fname) ;
if( fexist( fspec)) if( fexist( fspec))
return fspec ; return fspec ;
}
#if ENVFUNC #if ENVFUNC
#if V7 | USG | BSD #if V7 | USG | BSD
@ -116,12 +127,19 @@ char *flook( const char *fname, boolean hflag)
if (path != NULL) if (path != NULL)
while (*path) { while (*path) {
char *sp; /* pointer into path spec */ char *sp; /* pointer into path spec */
int cnt ;
cnt = len ;
/* build next possible file spec */ /* build next possible file spec */
sp = fspec; sp = fspec;
while (*path && (*path != PATHCHR)) while( *path && (*path != PATHCHR)) {
*sp++ = *path++; if( cnt-- > 0)
*sp++ = *path ;
path += 1 ;
}
if( cnt >= 0) {
/* add a terminating dir separator if we need it */ /* add a terminating dir separator if we need it */
if (sp != fspec) if (sp != fspec)
*sp++ = '/'; *sp++ = '/';
@ -131,6 +149,7 @@ char *flook( const char *fname, boolean hflag)
/* and try it out */ /* and try it out */
if( fexist( fspec)) if( fexist( fspec))
return fspec ; return fspec ;
}
if (*path == PATHCHR) if (*path == PATHCHR)
++path; ++path;
@ -138,9 +157,10 @@ char *flook( const char *fname, boolean hflag)
#endif #endif
/* look it up via the old table method */ /* look it up via the old table method */
for( i = 2; i < PATHNAME_SIZE ; i++) { for( i = 2; i < PATHNAME_SIZE ; i++)
strcpy(fspec, pathname[i]); if( len >= (int) strlen( pathname[ i])) {
strcat(fspec, fname); strcpy( fspec, pathname[ i]) ;
strcat( fspec, fname);
/* and try it out */ /* and try it out */
if( fexist( fspec)) if( fexist( fspec))