Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/lib/libc/gen/getcwd.c,v retrieving revision 1.16 retrieving revision 1.24.4.3 diff -u -p -r1.16 -r1.24.4.3 --- src/lib/libc/gen/getcwd.c 1999/03/26 04:04:13 1.16 +++ src/lib/libc/gen/getcwd.c 2004/05/12 04:20:34 1.24.4.3 @@ -1,4 +1,4 @@ -/* $NetBSD: getcwd.c,v 1.16 1999/03/26 04:04:13 sommerfe Exp $ */ +/* $NetBSD: getcwd.c,v 1.24.4.3 2004/05/12 04:20:34 jmc Exp $ */ /* * Copyright (c) 1989, 1991, 1993, 1995 @@ -41,7 +41,7 @@ #if 0 static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95"; #else -__RCSID("$NetBSD: getcwd.c,v 1.16 1999/03/26 04:04:13 sommerfe Exp $"); +__RCSID("$NetBSD: getcwd.c,v 1.24.4.3 2004/05/12 04:20:34 jmc Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -49,6 +49,7 @@ __RCSID("$NetBSD: getcwd.c,v 1.16 1999/0 #include #include +#include #include #include #include @@ -57,9 +58,11 @@ __RCSID("$NetBSD: getcwd.c,v 1.16 1999/0 #include #include +#include "extern.h" + #ifdef __weak_alias -__weak_alias(getcwd,_getcwd); -__weak_alias(realpath,_realpath); +__weak_alias(getcwd,_getcwd) +__weak_alias(realpath,_realpath) #endif #define ISDOT(dp) \ @@ -84,9 +87,12 @@ realpath(path, resolved) char *resolved; { struct stat sb; - int fd, n, rootd, serrno; + int fd, n, rootd, serrno, nlnk = 0; char *p, *q, wbuf[MAXPATHLEN]; + _DIAGASSERT(path != NULL); + _DIAGASSERT(resolved != NULL); + /* Save the starting point. */ if ((fd = open(".", O_RDONLY)) < 0) { (void)strcpy(resolved, "."); @@ -124,7 +130,11 @@ loop: /* Deal with the last component. */ if (lstat(p, &sb) == 0) { if (S_ISLNK(sb.st_mode)) { - n = readlink(p, resolved, MAXPATHLEN); + if (nlnk++ >= MAXSYMLINKS) { + errno = ELOOP; + goto err1; + } + n = readlink(p, resolved, MAXPATHLEN-1); if (n < 0) goto err1; resolved[n] = '\0'; @@ -141,7 +151,7 @@ loop: * Save the last component name and get the full pathname of * the current directory. */ - (void)strncpy(wbuf, p, (sizeof(wbuf) - 1)); + (void)strlcpy(wbuf, p, sizeof(wbuf)); /* * Call the inernal internal version of getcwd which @@ -160,7 +170,8 @@ loop: rootd = 0; if (*wbuf) { - if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) { + if (strlen(resolved) + strlen(wbuf) + (rootd ? 0 : 1) + 1 > + MAXPATHLEN) { errno = ENAMETOOLONG; goto err1; } @@ -302,7 +313,7 @@ getcwd(pt, size) if (!(dp = readdir(dir))) goto notfound; if (dp->d_fileno == ino) { -#if defined(__SVR4) || defined(__svr4__) +#if defined(__SVR4) || defined(__svr4__) || defined(__linux__) dlen = strlen(dp->d_name); #else dlen = dp->d_namlen; @@ -316,7 +327,7 @@ getcwd(pt, size) goto notfound; if (ISDOT(dp)) continue; -#if defined(__SVR4) || defined(__svr4__) +#if defined(__SVR4) || defined(__svr4__) || defined(__linux__) dlen = strlen(dp->d_name); #else dlen = dp->d_namlen; @@ -387,7 +398,8 @@ getcwd(pt, size) char *pt; size_t size; { - int ptsize, bufsize, len; + size_t ptsize, bufsize; + int len; /* * If no buffer specified by the user, allocate one as necessary. @@ -406,15 +418,18 @@ getcwd(pt, size) return (NULL); bufsize = ptsize; } - do { + for (;;) { len = __getcwd(pt, bufsize); if ((len < 0) && (size == 0) && (errno == ERANGE)) { + if (ptsize > (MAXPATHLEN*4)) + return NULL; if ((pt = realloc(pt, ptsize *= 2)) == NULL) return NULL; bufsize = ptsize; continue; } - } while (0); + break; + } if (len < 0) return NULL; else