From 8832e50f46f78f20abe2f4d038cf381f4981d560 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Mon, 6 Apr 1998 02:48:32 +0000 Subject: [PATCH] use MAX* not MAX* + 1 always run pwd as using getwd() defeats the purpose --- getcwd.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/getcwd.c b/getcwd.c index 6717eab4f..ff9f206c2 100644 --- a/getcwd.c +++ b/getcwd.c @@ -1,5 +1,5 @@ /* - * CU sudo version 1.3.1 + * CU sudo version 1.5.5 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,7 +15,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * - * Please send bugs, changes, problems to sudo-bugs@cs.colorado.edu + * Please send bugs, changes, problems to sudo-bugs@courtesan.com * ******************************************************************* * @@ -45,16 +45,16 @@ static char rcsid[] = "$Id$"; #ifdef HAVE_STRINGS_H #include #endif /* HAVE_STRINGS_H */ -#ifdef HAVE_MALLOC_H -#include -#endif /* HAVE_MALLOC_H */ +#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS) +# include +#endif /* HAVE_MALLOC_H && !STDC_HEADERS */ #include #include #include #include #include -#include "pathnames.h" +#include #include "compat.h" #ifndef STDC_HEADERS @@ -94,7 +94,8 @@ char * getcwd(path, len) char * path; /* path to copy into */ size_t len; /* length of path */ { - char buf[MAXPATHLEN+1]; /* temp buffer */ + char buf[MAXPATHLEN+1]; /* +1 for the newline */ + size_t blen; /* length of buf */ #ifndef HAVE_GETWD FILE * pwd; /* for popen */ #endif /* HAVE_GETWD */ @@ -104,10 +105,6 @@ char * getcwd(path, len) return(NULL); } -#ifdef HAVE_GETWD - if (!getwd(buf)) - return(NULL); -#else /* * open a pipe to pwd and read a line */ @@ -121,16 +118,21 @@ char * getcwd(path, len) } pclose(pwd); - buf[strlen(buf)-1] = '\0'; /* remove newline */ -#endif /* HAVE_GETWD */ + blen = strlen(buf); + if (buf[blen - 1] == '\n') + buf[--blen] = '\0'; /* remove newline */ + else if (blen >= MAXPATHLEN) { + errno = ENAMETOOLONG; /* only possible w/o newline */ + return(NULL); + } - if (len < strlen(buf) + 1) { + if (len < blen + 1) { errno = ERANGE; return(NULL); } if (path == NULL) { - if (!(path = (char *) malloc(MAXPATHLEN+1))) { + if (!(path = (char *) malloc(MAXPATHLEN))) { errno = ENOMEM; return(NULL); } -- 2.40.0