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.17 retrieving revision 1.25 diff -u -p -r1.17 -r1.25 --- src/lib/libc/gen/getcwd.c 1999/03/26 22:23:57 1.17 +++ src/lib/libc/gen/getcwd.c 2002/04/16 19:08:43 1.25 @@ -1,4 +1,4 @@ -/* $NetBSD: getcwd.c,v 1.17 1999/03/26 22:23:57 sommerfe Exp $ */ +/* $NetBSD: getcwd.c,v 1.25 2002/04/16 19:08:43 groo 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.17 1999/03/26 22:23:57 sommerfe Exp $"); +__RCSID("$NetBSD: getcwd.c,v 1.25 2002/04/16 19:08:43 groo Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -49,6 +49,7 @@ __RCSID("$NetBSD: getcwd.c,v 1.17 1999/0 #include #include +#include #include #include #include @@ -60,8 +61,8 @@ __RCSID("$NetBSD: getcwd.c,v 1.17 1999/0 #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) \ @@ -86,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, "."); @@ -126,6 +130,10 @@ loop: /* Deal with the last component. */ if (lstat(p, &sb) == 0) { if (S_ISLNK(sb.st_mode)) { + if (nlnk++ >= MAXSYMLINKS) { + errno = ELOOP; + goto err1; + } n = readlink(p, resolved, MAXPATHLEN); if (n < 0) goto err1; @@ -143,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 @@ -304,7 +312,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; @@ -318,7 +326,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; @@ -389,7 +397,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. @@ -408,15 +417,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