From: Lennart Poettering Date: Mon, 13 Jan 2025 12:20:29 +0000 (+0100) Subject: chase: move appending of trailing slash out of loop X-Git-Tag: v258-rc1~1548^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3351c0a510483790c0d11b5d9b219a3e0f085583;p=thirdparty%2Fsystemd.git chase: move appending of trailing slash out of loop let's move final processing of the filename out of the loop, and apply it in all cases, uniformly, even if we are asked to only return the final filename. --- diff --git a/src/basic/chase.c b/src/basic/chase.c index 2022e7a169c..5d064921b5b 100644 --- a/src/basic/chase.c +++ b/src/basic/chase.c @@ -91,7 +91,6 @@ int chaseat(int dir_fd, const char *path, ChaseFlags flags, char **ret_path, int assert(!FLAGS_SET(flags, CHASE_PREFIX_ROOT)); assert(!FLAGS_SET(flags, CHASE_MUST_BE_DIRECTORY|CHASE_MUST_BE_REGULAR)); assert(!FLAGS_SET(flags, CHASE_STEP|CHASE_EXTRACT_FILENAME)); - assert(!FLAGS_SET(flags, CHASE_TRAIL_SLASH|CHASE_EXTRACT_FILENAME)); assert(dir_fd >= 0 || dir_fd == AT_FDCWD); /* Either the file may be missing, or we return an fd to the final object, but both make no sense */ @@ -263,12 +262,8 @@ int chaseat(int dir_fd, const char *path, ChaseFlags flags, char **ret_path, int r = path_find_first_component(&todo, /* accept_dot_dot= */ true, &e); if (r < 0) return r; - if (r == 0) { /* We reached the end. */ - if (append_trail_slash) - if (!strextend(&done, "/")) - return -ENOMEM; + if (r == 0) /* We reached the end. */ break; - } first = strndup(e, r); if (!first) @@ -510,11 +505,15 @@ int chaseat(int dir_fd, const char *path, ChaseFlags flags, char **ret_path, int if (!done) { assert(!need_absolute || FLAGS_SET(flags, CHASE_EXTRACT_FILENAME)); - done = strdup(append_trail_slash ? "./" : "."); + done = strdup("."); if (!done) return -ENOMEM; } + if (append_trail_slash) + if (!strextend(&done, "/")) + return -ENOMEM; + *ret_path = TAKE_PTR(done); }