Skip to content

Commit 81f6faf

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)
1 parent 639c922 commit 81f6faf

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
@@ -641,8 +641,8 @@ int chase(const char *path, const char *root, ChaseFlags flags, char **ret_path,
641641
* absolute, hence it is not necessary to prefix with the root. When "root" points to
642642
* a non-root directory, the result path is always normalized and relative, hence
643643
* we can simply call path_join() and not necessary to call path_simplify().
644-
* Note that the result of chaseat() may start with "." (more specifically, it may be
645-
* "." or "./"), and we need to drop "." in that case. */
644+
* As a special case, chaseat() may return "." or "./", which are normalized too,
645+
* but we need to drop "." before merging with root. */
646646

647647
if (empty_or_root(root))
648648
assert(path_is_absolute(p));
@@ -651,7 +651,7 @@ int chase(const char *path, const char *root, ChaseFlags flags, char **ret_path,
651651

652652
assert(!path_is_absolute(p));
653653

654-
q = path_join(root, p + (*p == '.'));
654+
q = path_join(root, p + STR_IN_SET(p, ".", "./"));
655655
if (!q)
656656
return -ENOMEM;
657657

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)