Loading...
stdlib/FreeBSD/realpath.c.patch Libc-763.13 Libc-594.9.1
--- Libc/Libc-763.13/stdlib/FreeBSD/realpath.c.patch
+++ Libc/Libc-594.9.1/stdlib/FreeBSD/realpath.c.patch
@@ -1,5 +1,5 @@
---- realpath.c.orig	2010-06-24 17:32:55.000000000 -0700
-+++ realpath.c	2010-06-25 13:46:50.000000000 -0700
+--- realpath.c.orig	2008-04-04 14:39:39.000000000 -0700
++++ realpath.c	2008-04-04 19:59:19.000000000 -0700
 @@ -35,13 +35,41 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
  #include "namespace.h"
  #include <sys/param.h>
@@ -42,7 +42,7 @@
  /*
   * char *realpath(const char *path, char resolved[PATH_MAX]);
   *
-@@ -50,36 +78,89 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
+@@ -50,26 +78,67 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
   * in which case the path which caused trouble is left in (resolved).
   */
  char *
@@ -77,23 +77,18 @@
 +#endif /* __DARWIN_UNIX03 */
 +	/*
 +	 * Extension to the standard; if inresolved == NULL, allocate memory
++	 * (first on the stack, then use strdup())
 +	 */
 +	if (!inresolved) {
-+	    if ((resolved = malloc(PATH_MAX)) == NULL) return (NULL);
++	    if ((resolved = alloca(PATH_MAX)) == NULL) return (NULL);
 +	} else {
 +	    resolved = inresolved;
 +	}
 +	if (!rootdev_inited) {
 +		rootdev_inited = 1;
 +		if (stat("/", &sb) < 0) {
-+error_return:
-+			if (!inresolved) {
-+				int e = errno;
-+				free(resolved);
-+				errno = e;
-+			}
 +			return (NULL);
-+		}
++			}
 +		rootdev = sb.st_dev;
 +	}
  	serrno = errno;
@@ -118,20 +113,15 @@
 +#endif /* !VARIANT_DARWINEXTSN && __DARWIN_UNIX03 */
 +		{
  			strlcpy(resolved, ".", PATH_MAX);
--			return (NULL);
-+			goto error_return;
- 		}
- 		resolved_len = strlen(resolved);
- 		left_len = strlcpy(left, path, sizeof(left));
- 	}
- 	if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
+ 			return (NULL);
+ 		}
+@@ -80,6 +149,13 @@ realpath(const char *path, char resolved
  		errno = ENAMETOOLONG;
--		return (NULL);
-+		goto error_return;
+ 		return (NULL);
  	}
 +	if (resolved_len > 1) {
 +		if (stat(resolved, &sb) < 0) {
-+			goto error_return;
++			return (NULL);
 +		}
 +		lastdev = sb.st_dev;
 +	} else
@@ -139,25 +129,7 @@
  
  	/*
  	 * Iterate over path components in `left'.
-@@ -93,7 +174,7 @@ realpath(const char *path, char resolved
- 		s = p ? p : left + left_len;
- 		if (s - left >= sizeof(next_token)) {
- 			errno = ENAMETOOLONG;
--			return (NULL);
-+			goto error_return;
- 		}
- 		memcpy(next_token, left, s - left);
- 		next_token[s - left] = '\0';
-@@ -103,7 +184,7 @@ realpath(const char *path, char resolved
- 		if (resolved[resolved_len - 1] != '/') {
- 			if (resolved_len + 1 >= PATH_MAX) {
- 				errno = ENAMETOOLONG;
--				return (NULL);
-+				goto error_return;
- 			}
- 			resolved[resolved_len++] = '/';
- 			resolved[resolved_len] = '\0';
-@@ -127,6 +208,13 @@ realpath(const char *path, char resolved
+@@ -127,6 +203,13 @@ realpath(const char *path, char resolved
  		}
  
  		/*
@@ -171,12 +143,9 @@
  		 * Append the next path component and lstat() it. If
  		 * lstat() fails we still can return successfully if
  		 * there are no more path components left.
-@@ -134,27 +222,89 @@ realpath(const char *path, char resolved
- 		resolved_len = strlcat(resolved, next_token, PATH_MAX);
- 		if (resolved_len >= PATH_MAX) {
+@@ -136,25 +219,87 @@ realpath(const char *path, char resolved
  			errno = ENAMETOOLONG;
--			return (NULL);
-+			goto error_return;
+ 			return (NULL);
  		}
 -		if (lstat(resolved, &sb) != 0) {
 +		if (getattrlist(resolved, &_rp_alist, &attrs, sizeof(attrs), FSOPT_NOFOLLOW) == 0) {
@@ -198,9 +167,8 @@
  				errno = serrno;
  				return (resolved);
  			}
--			return (NULL);
 +#endif /* !__DARWIN_UNIX03 */
-+			goto error_return;
+ 			return (NULL);
  		}
 -		if (S_ISLNK(sb.st_mode)) {
 +		if (dev != lastdev) {
@@ -251,14 +219,12 @@
 +		if (islink) {
  			if (symlinks++ > MAXSYMLINKS) {
  				errno = ELOOP;
--				return (NULL);
-+				goto error_return;
+ 				return (NULL);
  			}
  			slen = readlink(resolved, symlink, sizeof(symlink) - 1);
 -			if (slen < 0)
--				return (NULL);
 +			if (slen < 0) {
-+				goto error_return;
+ 				return (NULL);
 +			}
  			symlink[slen] = '\0';
  			if (symlink[0] == '/') {
@@ -268,21 +234,7 @@
  			} else if (resolved_len > 1) {
  				/* Strip the last path component. */
  				resolved[resolved_len - 1] = '\0';
-@@ -172,7 +322,7 @@ realpath(const char *path, char resolved
- 				if (symlink[slen - 1] != '/') {
- 					if (slen + 1 >= sizeof(symlink)) {
- 						errno = ENAMETOOLONG;
--						return (NULL);
-+						goto error_return;
- 					}
- 					symlink[slen] = '/';
- 					symlink[slen + 1] = 0;
-@@ -180,11 +330,34 @@ realpath(const char *path, char resolved
- 				left_len = strlcat(symlink, left, sizeof(left));
- 				if (left_len >= sizeof(left)) {
- 					errno = ENAMETOOLONG;
--					return (NULL);
-+					goto error_return;
+@@ -184,7 +329,30 @@ realpath(const char *path, char resolved
  				}
  			}
  			left_len = strlcpy(left, symlink, sizeof(left));
@@ -295,7 +247,7 @@
 +			resolved_len = strlcat(resolved, (const char *)&attrs.name + attrs.name.attr_dataoffset, PATH_MAX);
 +			if (resolved_len >= PATH_MAX) {
 +				errno = ENAMETOOLONG;
-+				goto error_return;
++				return (NULL);
 +			}
  		}
 +		/*
@@ -313,3 +265,10 @@
  	}
  
  	/*
+@@ -193,5 +361,6 @@ realpath(const char *path, char resolved
+ 	 */
+ 	if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
+ 		resolved[resolved_len - 1] = '\0';
++	if (!inresolved) resolved = strdup(resolved);
+ 	return (resolved);
+ }