Skip to content

Commit 1b12eca

Browse files
DaanDeMeyerbluca
authored andcommitted
chase: Tighten "." and "./" check
Currently the check also succeeds if the input path starts with a dot, whereas we only want it to succeed for "." and "./". Tighten the check and add a test. (cherry picked from commit 7efaab4) (cherry picked from commit 81f6faf)
1 parent e2d6762 commit 1b12eca

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/basic/chase.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,8 @@ int chase(const char *path, const char *root, ChaseFlags flags, char **ret_path,
621621
* absolute, hence it is not necessary to prefix with the root. When "root" points to
622622
* a non-root directory, the result path is always normalized and relative, hence
623623
* we can simply call path_join() and not necessary to call path_simplify().
624-
* Note that the result of chaseat() may start with "." (more specifically, it may be
625-
* "." or "./"), and we need to drop "." in that case. */
624+
* As a special case, chaseat() may return "." or "./", which are normalized too,
625+
* but we need to drop "." before merging with root. */
626626

627627
if (empty_or_root(root))
628628
assert(path_is_absolute(p));
@@ -631,7 +631,7 @@ int chase(const char *path, const char *root, ChaseFlags flags, char **ret_path,
631631

632632
assert(!path_is_absolute(p));
633633

634-
q = path_join(root, p + (*p == '.'));
634+
q = path_join(root, p + STR_IN_SET(p, ".", "./"));
635635
if (!q)
636636
return -ENOMEM;
637637

src/test/test-chase.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ TEST(chase) {
236236
assert_se(streq(result, "/test-chase.fsldajfl"));
237237
result = mfree(result);
238238

239+
r = chase("/.path/with/dot", temp, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &result, NULL);
240+
assert_se(r == 0);
241+
q = strjoina(temp, "/.path/with/dot");
242+
assert_se(streq(result, q));
243+
result = mfree(result);
244+
239245
r = chase("/etc/machine-id/foo", NULL, 0, &result, NULL);
240246
assert_se(IN_SET(r, -ENOTDIR, -ENOENT));
241247
result = mfree(result);

0 commit comments

Comments
 (0)