Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 13 Mar 2015 11:26:03 +0000 (UTC)
From:      Edward Tomasz Napierala <trasz@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r279953 - head/usr.sbin/autofs
Message-ID:  <201503131126.t2DBQ3GA028690@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: trasz
Date: Fri Mar 13 11:26:02 2015
New Revision: 279953
URL: https://svnweb.freebsd.org/changeset/base/279953

Log:
  Rework the concat() algorithm to be correct in all cases.
  
  MFC after:	1 month
  Sponsored by:	The FreeBSD Foundation

Modified:
  head/usr.sbin/autofs/common.c

Modified: head/usr.sbin/autofs/common.c
==============================================================================
--- head/usr.sbin/autofs/common.c	Fri Mar 13 09:50:29 2015	(r279952)
+++ head/usr.sbin/autofs/common.c	Fri Mar 13 11:26:02 2015	(r279953)
@@ -92,6 +92,7 @@ char *
 concat(const char *s1, char separator, const char *s2)
 {
 	char *result;
+	char s1last, s2first;
 	int ret;
 
 	if (s1 == NULL)
@@ -99,14 +100,22 @@ concat(const char *s1, char separator, c
 	if (s2 == NULL)
 		s2 = "";
 
-	/*
-	 * If s2 starts with separator - skip it; otherwise concatenating
-	 * "/" and "/foo" would end up returning "//foo".
-	 */
-	if (s2[0] == separator)
-		s2++;
+	if (s1[0] == '\0')
+		s1last = '\0';
+	else
+		s1last = s1[strlen(s1) - 1];
 
-	if (s1[0] == '\0' || s2[0] == '\0' || s1[strlen(s1) - 1] == separator) {
+	s2first = s2[0];
+
+	if (s1last == separator && s2first == separator) {
+		/*
+		 * If s1 ends with the separator and s2 begins with
+		 * it - skip the latter; otherwise concatenating "/"
+		 * and "/foo" would end up returning "//foo".
+		 */
+		ret = asprintf(&result, "%s%s", s1, s2 + 1);
+	} else if (s1last == separator || s2first == separator ||
+	    s1[0] == '\0' || s2[0] == '\0') {
 		ret = asprintf(&result, "%s%s", s1, s2);
 	} else {
 		ret = asprintf(&result, "%s%c%s", s1, separator, s2);



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