Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 29 May 2017 12:59:25 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r319130 - stable/10/lib/libc/stdlib
Message-ID:  <201705291259.v4TCxPcX093721@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Mon May 29 12:59:24 2017
New Revision: 319130
URL: https://svnweb.freebsd.org/changeset/base/319130

Log:
  MFC r318299:
  Simplify cleanup on failure in realpath(3).

Modified:
  stable/10/lib/libc/stdlib/realpath.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/lib/libc/stdlib/realpath.c
==============================================================================
--- stable/10/lib/libc/stdlib/realpath.c	Mon May 29 12:58:30 2017	(r319129)
+++ stable/10/lib/libc/stdlib/realpath.c	Mon May 29 12:59:24 2017	(r319130)
@@ -47,32 +47,16 @@ __FBSDID("$FreeBSD$");
  * 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)
+static char *
+realpath1(const char *path, char *resolved)
 {
 	struct stat sb;
 	char *p, *q;
 	size_t left_len, resolved_len, next_token_len;
 	unsigned symlinks;
-	int m;
 	ssize_t slen;
 	char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
 
-	if (path == NULL) {
-		errno = EINVAL;
-		return (NULL);
-	}
-	if (path[0] == '\0') {
-		errno = ENOENT;
-		return (NULL);
-	}
-	if (resolved == NULL) {
-		resolved = malloc(PATH_MAX);
-		if (resolved == NULL)
-			return (NULL);
-		m = 1;
-	} else
-		m = 0;
 	symlinks = 0;
 	if (path[0] == '/') {
 		resolved[0] = '/';
@@ -83,20 +67,14 @@ realpath(const char * __restrict path, c
 		left_len = strlcpy(left, path + 1, sizeof(left));
 	} else {
 		if (getcwd(resolved, PATH_MAX) == NULL) {
-			if (m)
-				free(resolved);
-			else {
-				resolved[0] = '.';
-				resolved[1] = '\0';
-			}
+			resolved[0] = '.';
+			resolved[1] = '\0';
 			return (NULL);
 		}
 		resolved_len = strlen(resolved);
 		left_len = strlcpy(left, path, sizeof(left));
 	}
 	if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
-		if (m)
-			free(resolved);
 		errno = ENAMETOOLONG;
 		return (NULL);
 	}
@@ -125,8 +103,6 @@ realpath(const char * __restrict path, c
 
 		if (resolved[resolved_len - 1] != '/') {
 			if (resolved_len + 1 >= PATH_MAX) {
-				if (m)
-					free(resolved);
 				errno = ENAMETOOLONG;
 				return (NULL);
 			}
@@ -158,27 +134,18 @@ realpath(const char * __restrict path, c
 		 */
 		resolved_len = strlcat(resolved, next_token, PATH_MAX);
 		if (resolved_len >= PATH_MAX) {
-			if (m)
-				free(resolved);
 			errno = ENAMETOOLONG;
 			return (NULL);
 		}
-		if (lstat(resolved, &sb) != 0) {
-			if (m)
-				free(resolved);
+		if (lstat(resolved, &sb) != 0)
 			return (NULL);
-		}
 		if (S_ISLNK(sb.st_mode)) {
 			if (symlinks++ > MAXSYMLINKS) {
-				if (m)
-					free(resolved);
 				errno = ELOOP;
 				return (NULL);
 			}
 			slen = readlink(resolved, symlink, sizeof(symlink));
 			if (slen <= 0 || slen >= sizeof(symlink)) {
-				if (m)
-					free(resolved);
 				if (slen < 0) {
 					/* keep errno from readlink(2) call */
 				} else if (slen == 0) {
@@ -207,8 +174,6 @@ realpath(const char * __restrict path, c
 			if (p != NULL) {
 				if (symlink[slen - 1] != '/') {
 					if (slen + 1 >= sizeof(symlink)) {
-						if (m)
-							free(resolved);
 						errno = ENAMETOOLONG;
 						return (NULL);
 					}
@@ -218,16 +183,12 @@ realpath(const char * __restrict path, c
 				left_len = strlcat(symlink, left,
 				    sizeof(symlink));
 				if (left_len >= sizeof(symlink)) {
-					if (m)
-						free(resolved);
 					errno = ENAMETOOLONG;
 					return (NULL);
 				}
 			}
 			left_len = strlcpy(left, symlink, sizeof(left));
 		} else if (!S_ISDIR(sb.st_mode) && p != NULL) {
-			if (m)
-				free(resolved);
 			errno = ENOTDIR;
 			return (NULL);
 		}
@@ -241,3 +202,29 @@ realpath(const char * __restrict path, c
 		resolved[resolved_len - 1] = '\0';
 	return (resolved);
 }
+
+char *
+realpath(const char * __restrict path, char * __restrict resolved)
+{
+	char *m, *res;
+
+	if (path == NULL) {
+		errno = EINVAL;
+		return (NULL);
+	}
+	if (path[0] == '\0') {
+		errno = ENOENT;
+		return (NULL);
+	}
+	if (resolved != NULL) {
+		m = NULL;
+	} else {
+		m = resolved = malloc(PATH_MAX);
+		if (resolved == NULL)
+			return (NULL);
+	}
+	res = realpath1(path, resolved);
+	if (res == NULL)
+		free(m);
+	return (res);
+}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201705291259.v4TCxPcX093721>