path-util: some updates to path_make_relative()
Don't miscount number of "../" to generate, if we "." is included in an input path. Also, refuse if we encounter "../" since we can't possibly follow that up properly, without file system access. Some other modernizations.
This commit is contained in:
parent
091e9efed3
commit
2a5beb669f
|
@ -131,8 +131,7 @@ int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
|
||||||
|
|
||||||
/* Skip the common part. */
|
/* Skip the common part. */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
size_t a;
|
size_t a, b;
|
||||||
size_t b;
|
|
||||||
|
|
||||||
from_dir += strspn(from_dir, "/");
|
from_dir += strspn(from_dir, "/");
|
||||||
to_path += strspn(to_path, "/");
|
to_path += strspn(to_path, "/");
|
||||||
|
@ -144,7 +143,6 @@ int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
|
||||||
else
|
else
|
||||||
/* from_dir is a parent directory of to_path. */
|
/* from_dir is a parent directory of to_path. */
|
||||||
r = strdup(to_path);
|
r = strdup(to_path);
|
||||||
|
|
||||||
if (!r)
|
if (!r)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
|
@ -175,21 +173,32 @@ int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
|
||||||
|
|
||||||
/* Count the number of necessary ".." elements. */
|
/* Count the number of necessary ".." elements. */
|
||||||
for (n_parents = 0;;) {
|
for (n_parents = 0;;) {
|
||||||
|
size_t w;
|
||||||
|
|
||||||
from_dir += strspn(from_dir, "/");
|
from_dir += strspn(from_dir, "/");
|
||||||
|
|
||||||
if (!*from_dir)
|
if (!*from_dir)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
from_dir += strcspn(from_dir, "/");
|
w = strcspn(from_dir, "/");
|
||||||
|
|
||||||
|
/* If this includes ".." we can't do a simple series of "..", refuse */
|
||||||
|
if (w == 2 && from_dir[0] == '.' && from_dir[1] == '.')
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
/* Count number of elements, except if they are "." */
|
||||||
|
if (w != 1 || from_dir[0] != '.')
|
||||||
n_parents++;
|
n_parents++;
|
||||||
|
|
||||||
|
from_dir += w;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = malloc(n_parents * 3 + strlen(to_path) + 1);
|
r = new(char, n_parents * 3 + strlen(to_path) + 1);
|
||||||
if (!r)
|
if (!r)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
for (p = r; n_parents > 0; n_parents--, p += 3)
|
for (p = r; n_parents > 0; n_parents--)
|
||||||
memcpy(p, "../", 3);
|
p = mempcpy(p, "../", 3);
|
||||||
|
|
||||||
strcpy(p, to_path);
|
strcpy(p, to_path);
|
||||||
path_kill_slashes(r);
|
path_kill_slashes(r);
|
||||||
|
|
|
@ -261,6 +261,7 @@ static void test_make_relative(void) {
|
||||||
|
|
||||||
assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
|
assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
|
||||||
assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
|
assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
|
||||||
|
assert_se(path_make_relative("/some/dotdot/../path", "/some/path", &result) < 0);
|
||||||
|
|
||||||
#define test(from_dir, to_path, expected) { \
|
#define test(from_dir, to_path, expected) { \
|
||||||
_cleanup_free_ char *z = NULL; \
|
_cleanup_free_ char *z = NULL; \
|
||||||
|
@ -274,6 +275,7 @@ static void test_make_relative(void) {
|
||||||
test("/some/path", "/some/path/in/subdir", "in/subdir");
|
test("/some/path", "/some/path/in/subdir", "in/subdir");
|
||||||
test("/some/path", "/", "../..");
|
test("/some/path", "/", "../..");
|
||||||
test("/some/path", "/some/other/path", "../other/path");
|
test("/some/path", "/some/other/path", "../other/path");
|
||||||
|
test("/some/path/./dot", "/some/further/path", "../../further/path");
|
||||||
test("//extra/////slashes///won't////fool///anybody//", "////extra///slashes////are/just///fine///", "../../../are/just/fine");
|
test("//extra/////slashes///won't////fool///anybody//", "////extra///slashes////are/just///fine///", "../../../are/just/fine");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue