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.37 retrieving revision 1.49 diff -u -p -r1.37 -r1.49 --- src/lib/libc/gen/getcwd.c 2005/07/04 20:44:47 1.37 +++ src/lib/libc/gen/getcwd.c 2011/02/16 20:20:25 1.49 @@ -1,4 +1,4 @@ -/* $NetBSD: getcwd.c,v 1.37 2005/07/04 20:44:47 elad Exp $ */ +/* $NetBSD: getcwd.c,v 1.49 2011/02/16 20:20:25 tron Exp $ */ /* * Copyright (c) 1989, 1991, 1993, 1995 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95"; #else -__RCSID("$NetBSD: getcwd.c,v 1.37 2005/07/04 20:44:47 elad Exp $"); +__RCSID("$NetBSD: getcwd.c,v 1.49 2011/02/16 20:20:25 tron Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -54,8 +54,18 @@ __RCSID("$NetBSD: getcwd.c,v 1.37 2005/0 #include "extern.h" #ifdef __weak_alias -__weak_alias(getcwd,_getcwd) +__weak_alias(getcwd,_sys_getcwd) +__weak_alias(_getcwd,_sys_getcwd) __weak_alias(realpath,_realpath) + +#if !defined(lint) +#undef getcwd +#define getcwd _sys_getcwd +#if !defined(_FORTIFY_SOURCE) +char *_sys_getcwd(char *, size_t); +#endif + +#endif #endif /* @@ -69,14 +79,19 @@ char * realpath(const char *path, char *resolved) { struct stat sb; - int idx = 0, n, nlnk = 0, serrno = errno; + int idx = 0, n, nlnk = 0; const char *q; char *p, wbuf[2][MAXPATHLEN]; size_t len; - _DIAGASSERT(path != NULL); _DIAGASSERT(resolved != NULL); + /* POSIX sez we must test for this */ + if (path == NULL) { + errno = EINVAL; + return NULL; + } + /* * Build real path one by one with paying an attention to ., * .. and symbolic link. @@ -96,7 +111,8 @@ realpath(const char *path, char *resolve /* If relative path, start from current working directory. */ if (*path != '/') { - if (getcwd(resolved, MAXPATHLEN) == NULL) { + /* check for resolved pointer to appease coverity */ + if (resolved && getcwd(resolved, MAXPATHLEN) == NULL) { p[0] = '.'; p[1] = 0; return (NULL); @@ -159,16 +175,6 @@ loop: * target to unresolved path. */ if (lstat(resolved, &sb) == -1) { - /* Allow nonexistent component if this is the last one. */ - if ((*q == 0 || (*q == '/' && *(q + 1) == 0)) - && errno == ENOENT) { - errno = serrno; - return (resolved); - } - return (NULL); - } - if (*q == '/' && !S_ISDIR(sb.st_mode)) { - errno = ENOTDIR; return (NULL); } if (S_ISLNK(sb.st_mode)) { @@ -198,6 +204,10 @@ loop: p = resolved; goto loop; } + if (*q == '/' && !S_ISDIR(sb.st_mode)) { + errno = ENOTDIR; + return (NULL); + } /* Advance both resolved and unresolved path. */ p += 1 + q - path;