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.50.4.2 retrieving revision 1.51 diff -u -p -r1.50.4.2 -r1.51 --- src/lib/libc/gen/getcwd.c 2012/10/30 18:58:46 1.50.4.2 +++ src/lib/libc/gen/getcwd.c 2012/03/13 21:13:35 1.51 @@ -1,4 +1,4 @@ -/* $NetBSD: getcwd.c,v 1.50.4.2 2012/10/30 18:58:46 yamt Exp $ */ +/* $NetBSD: getcwd.c,v 1.51 2012/03/13 21:13:35 christos 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.50.4.2 2012/10/30 18:58:46 yamt Exp $"); +__RCSID("$NetBSD: getcwd.c,v 1.51 2012/03/13 21:13:35 christos Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -61,36 +61,30 @@ __weak_alias(realpath,_realpath) #endif /* - * char *realpath(const char *path, char *resolved); + * char *realpath(const char *path, char resolved[MAXPATHLEN]); * * Find the real name of path, by removing all ".", ".." and symlink * components. Returns (resolved) on success, or (NULL) on failure, * in which case the path which caused trouble is left in (resolved). */ char * -realpath(const char * __restrict path, char * __restrict resolved) +realpath(const char *path, char *resolved) { struct stat sb; int idx = 0, nlnk = 0; const char *q; - char *p, wbuf[2][MAXPATHLEN], *fres; + char *p, wbuf[2][MAXPATHLEN]; size_t len; ssize_t n; + _DIAGASSERT(resolved != NULL); + /* POSIX sez we must test for this */ if (path == NULL) { errno = EINVAL; return NULL; } - if (resolved == NULL) { - fres = resolved = malloc(MAXPATHLEN); - if (resolved == NULL) - return NULL; - } else - fres = NULL; - - /* * Build real path one by one with paying an attention to ., * .. and symbolic link. @@ -102,10 +96,10 @@ realpath(const char * __restrict path, c */ p = resolved; - if (*path == '\0') { - *p = '\0'; + if (*path == 0) { + *p = 0; errno = ENOENT; - goto out; + return (NULL); } /* If relative path, start from current working directory. */ @@ -113,8 +107,8 @@ realpath(const char * __restrict path, c /* check for resolved pointer to appease coverity */ if (resolved && getcwd(resolved, MAXPATHLEN) == NULL) { p[0] = '.'; - p[1] = '\0'; - goto out; + p[1] = 0; + return (NULL); } len = strlen(resolved); if (len > 1) @@ -126,18 +120,18 @@ loop: while (*path == '/') path++; - if (*path == '\0') { + if (*path == 0) { if (p == resolved) *p++ = '/'; - *p = '\0'; - return resolved; + *p = 0; + return (resolved); } /* Find the end of this component. */ q = path; do q++; - while (*q != '/' && *q != '\0'); + while (*q != '/' && *q != 0); /* Test . or .. */ if (path[0] == '.') { @@ -149,7 +143,7 @@ loop: /* Trim the last component. */ if (p != resolved) while (*--p != '/') - continue; + ; path = q; goto loop; } @@ -160,39 +154,39 @@ loop: errno = ENAMETOOLONG; if (p == resolved) *p++ = '/'; - *p = '\0'; - goto out; + *p = 0; + return (NULL); } p[0] = '/'; memcpy(&p[1], path, /* LINTED We know q > path. */ q - path); - p[1 + q - path] = '\0'; + p[1 + q - path] = 0; /* * If this component is a symlink, toss it and prepend link * target to unresolved path. */ - if (lstat(resolved, &sb) == -1) - goto out; - + if (lstat(resolved, &sb) == -1) { + return (NULL); + } if (S_ISLNK(sb.st_mode)) { if (nlnk++ >= MAXSYMLINKS) { errno = ELOOP; - goto out; + return (NULL); } n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1); if (n < 0) - goto out; + return (NULL); if (n == 0) { errno = ENOENT; - goto out; + return (NULL); } /* Append unresolved path to link target and switch to it. */ if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0])) { errno = ENAMETOOLONG; - goto out; + return (NULL); } memcpy(&wbuf[idx][n], q, len + 1); path = wbuf[idx]; @@ -205,16 +199,13 @@ loop: } if (*q == '/' && !S_ISDIR(sb.st_mode)) { errno = ENOTDIR; - goto out; + return (NULL); } /* Advance both resolved and unresolved path. */ p += 1 + q - path; path = q; goto loop; -out: - free(fres); - return NULL; } char *