diff options
author | Stephen Gallagher <sgallagh@redhat.com> | 2012-03-06 11:34:41 -0500 |
---|---|---|
committer | Stephen Gallagher <sgallagh@redhat.com> | 2012-03-06 15:46:21 -0500 |
commit | bb90be1e23e419e92eff404208ecf175796f4eba (patch) | |
tree | f0203d37ef69cb9694d4b3d940c293cd69fa491c /path_utils | |
parent | 7b1d48d4e1cbd83a2d228e500f376c516b1c93b0 (diff) | |
download | ding-libs-bb90be1e23e419e92eff404208ecf175796f4eba.tar.gz ding-libs-bb90be1e23e419e92eff404208ecf175796f4eba.tar.xz ding-libs-bb90be1e23e419e92eff404208ecf175796f4eba.zip |
path_utils: Handle "/" in path_concat
Diffstat (limited to 'path_utils')
-rw-r--r-- | path_utils/path_utils.c | 12 | ||||
-rw-r--r-- | path_utils/path_utils_ut.c | 9 |
2 files changed, 19 insertions, 2 deletions
diff --git a/path_utils/path_utils.c b/path_utils/path_utils.c index 360d499..25ca426 100644 --- a/path_utils/path_utils.c +++ b/path_utils/path_utils.c @@ -202,14 +202,22 @@ int path_concat(char *path, size_t path_size, const char *head, const char *tail if (head && *head) { for (p = head; *p; p++); /* walk to end of head */ - for (p--; p >= head && *p == '/'; p--); /* skip any trailing slashes in head */ + for (p--; p > head && *p == '/'; p--); /* skip any trailing slashes in head */ if ((p - head) > path_size-1) return ENOBUFS; for (src = head; src <= p && dst < dst_end;) *dst++ = *src++; /* copy head */ } if (tail && *tail) { for (p = tail; *p && *p == '/'; p++); /* skip any leading slashes in tail */ if (dst > path) - if (dst < dst_end) *dst++ = '/'; /* insert single slash between head & tail */ + /* insert single slash between head & tail + * Making sure not to add an extra if the + * preceding character is also a slash + * (such as the case where head was the + * special-case "/". + */ + if (dst < dst_end && *(dst-1) != '/') { + *dst++ = '/'; + } for (src = p; *src && dst < dst_end;) *dst++ = *src++; /* copy tail */ if (*src) return ENOBUFS; /* failed to copy everything */ } diff --git a/path_utils/path_utils_ut.c b/path_utils/path_utils_ut.c index fbefcab..34462cf 100644 --- a/path_utils/path_utils_ut.c +++ b/path_utils/path_utils_ut.c @@ -229,6 +229,15 @@ START_TEST(test_path_concat) fail_unless(path_concat(p, PATH_MAX, "", "foo") == SUCCESS); fail_unless_str_equal(p, "foo"); + fail_unless(path_concat(p, PATH_MAX, "/", "foo") == SUCCESS); + fail_unless_str_equal(p, "/foo"); + + fail_unless(path_concat(p, PATH_MAX, "/foo", "/") == SUCCESS); + fail_unless_str_equal(p, "/foo/"); + + fail_unless(path_concat(p, PATH_MAX, "/foo", "bar/") == SUCCESS); + fail_unless_str_equal(p, "/foo/bar/"); + fail_unless(path_concat(p, PATH_MAX, NULL, "foo") == SUCCESS); fail_unless_str_equal(p, "foo"); |