diff options
author | trasz <trasz@FreeBSD.org> | 2015-03-13 11:26:02 +0000 |
---|---|---|
committer | trasz <trasz@FreeBSD.org> | 2015-03-13 11:26:02 +0000 |
commit | 29dcbee98852106f4ff6e079ccf20bd4d574923a (patch) | |
tree | abfa0fab813cc3618efd638ba70d1d8ce4a65b8d /usr.sbin | |
parent | c2cd0e4f5c8dd9dc60df404e9effc20787ed6d81 (diff) | |
download | FreeBSD-src-29dcbee98852106f4ff6e079ccf20bd4d574923a.zip FreeBSD-src-29dcbee98852106f4ff6e079ccf20bd4d574923a.tar.gz |
Rework the concat() algorithm to be correct in all cases.
MFC after: 1 month
Sponsored by: The FreeBSD Foundation
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/autofs/common.c | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/usr.sbin/autofs/common.c b/usr.sbin/autofs/common.c index 4df177c..eae118f 100644 --- a/usr.sbin/autofs/common.c +++ b/usr.sbin/autofs/common.c @@ -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, const char *s2) 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); |