vm: fix a null dereference on out-of-memory
[minix.git] / lib / libc / gen / getcwd.c
blob75a8bb6a15c4c613c1130e41a42cb25cedc91a10
1 /* $NetBSD: getcwd.c,v 1.47 2011/01/20 02:57:00 christos Exp $ */
3 /*
4 * Copyright (c) 1989, 1991, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Jan-Simon Pendry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95";
39 #else
40 __RCSID("$NetBSD: getcwd.c,v 1.47 2011/01/20 02:57:00 christos Exp $");
41 #endif
42 #endif /* LIBC_SCCS and not lint */
44 #include "namespace.h"
45 #include <sys/param.h>
46 #include <sys/stat.h>
48 #include <assert.h>
49 #include <errno.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
54 #include "extern.h"
56 #ifdef __weak_alias
57 #ifdef __minix
58 __weak_alias(getcwd,_getcwd)
59 #else
60 __weak_alias(getcwd,_sys_getcwd)
61 __weak_alias(_getcwd,_sys_getcwd)
62 #endif
63 __weak_alias(realpath,_realpath)
65 #if defined(_FORTIFY_SOURCE) && !defined(__lint__)
66 #undef getcwd
67 #define getcwd _sys_getcwd
68 #endif
70 #endif
73 * char *realpath(const char *path, char resolved[MAXPATHLEN]);
75 * Find the real name of path, by removing all ".", ".." and symlink
76 * components. Returns (resolved) on success, or (NULL) on failure,
77 * in which case the path which caused trouble is left in (resolved).
79 char *
80 realpath(const char *path, char *resolved)
82 struct stat sb;
83 int idx = 0, n, nlnk = 0;
84 const char *q;
85 char *p, wbuf[2][MAXPATHLEN];
86 size_t len;
88 _DIAGASSERT(resolved != NULL);
90 /* POSIX sez we must test for this */
91 if (path == NULL) {
92 errno = EINVAL;
93 return NULL;
97 * Build real path one by one with paying an attention to .,
98 * .. and symbolic link.
102 * `p' is where we'll put a new component with prepending
103 * a delimiter.
105 p = resolved;
107 if (*path == 0) {
108 *p = 0;
109 errno = ENOENT;
110 return (NULL);
113 /* If relative path, start from current working directory. */
114 if (*path != '/') {
115 /* check for resolved pointer to appease coverity */
116 if (resolved && getcwd(resolved, MAXPATHLEN) == NULL) {
117 p[0] = '.';
118 p[1] = 0;
119 return (NULL);
121 len = strlen(resolved);
122 if (len > 1)
123 p += len;
126 loop:
127 /* Skip any slash. */
128 while (*path == '/')
129 path++;
131 if (*path == 0) {
132 if (p == resolved)
133 *p++ = '/';
134 *p = 0;
135 return (resolved);
138 /* Find the end of this component. */
139 q = path;
141 q++;
142 while (*q != '/' && *q != 0);
144 /* Test . or .. */
145 if (path[0] == '.') {
146 if (q - path == 1) {
147 path = q;
148 goto loop;
150 if (path[1] == '.' && q - path == 2) {
151 /* Trim the last component. */
152 if (p != resolved)
153 while (*--p != '/')
155 path = q;
156 goto loop;
160 /* Append this component. */
161 if (p - resolved + 1 + q - path + 1 > MAXPATHLEN) {
162 errno = ENAMETOOLONG;
163 if (p == resolved)
164 *p++ = '/';
165 *p = 0;
166 return (NULL);
168 p[0] = '/';
169 memcpy(&p[1], path,
170 /* LINTED We know q > path. */
171 q - path);
172 p[1 + q - path] = 0;
175 * If this component is a symlink, toss it and prepend link
176 * target to unresolved path.
178 if (lstat(resolved, &sb) == -1) {
179 return (NULL);
181 if (S_ISLNK(sb.st_mode)) {
182 if (nlnk++ >= MAXSYMLINKS) {
183 errno = ELOOP;
184 return (NULL);
186 n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1);
187 if (n < 0)
188 return (NULL);
189 if (n == 0) {
190 errno = ENOENT;
191 return (NULL);
194 /* Append unresolved path to link target and switch to it. */
195 if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0])) {
196 errno = ENAMETOOLONG;
197 return (NULL);
199 memcpy(&wbuf[idx][n], q, len + 1);
200 path = wbuf[idx];
201 idx ^= 1;
203 /* If absolute symlink, start from root. */
204 if (*path == '/')
205 p = resolved;
206 goto loop;
208 if (*q == '/' && !S_ISDIR(sb.st_mode)) {
209 errno = ENOTDIR;
210 return (NULL);
213 /* Advance both resolved and unresolved path. */
214 p += 1 + q - path;
215 path = q;
216 goto loop;
219 char *
220 getcwd(char *pt, size_t size)
222 char *npt;
225 * If a buffer is specified, the size has to be non-zero.
227 if (pt != NULL) {
228 if (size == 0) {
229 /* __getcwd(pt, 0) results ERANGE. */
230 errno = EINVAL;
231 return (NULL);
233 if (__getcwd(pt, size) >= 0)
234 return (pt);
235 return (NULL);
239 * If no buffer specified by the user, allocate one as necessary.
241 size = 1024 >> 1;
242 do {
243 if ((npt = realloc(pt, size <<= 1)) == NULL)
244 break;
245 pt = npt;
246 if (__getcwd(pt, size) >= 0)
247 return (pt);
248 } while (size <= MAXPATHLEN * 4 && errno == ERANGE);
250 free(pt);
251 return (NULL);