]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: Simplify canonicalize_absolute_path()
authorVolker Lendecke <vl@samba.org>
Tue, 5 Jul 2022 11:48:18 +0000 (13:48 +0200)
committerRalph Boehme <slow@samba.org>
Mon, 25 Jul 2022 12:04:33 +0000 (12:04 +0000)
We don't need the separate "wrote_slash" boolean variable, we can just
look at what we wrote into p[-1]

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
source3/lib/util_path.c

index c34b734384cfc75fd7244fc474d5ae3622b43cbb..3591589cb8e15f8da0d65b095a8ae0a5f2f3faa1 100644 (file)
@@ -116,7 +116,6 @@ char *canonicalize_absolute_path(TALLOC_CTX *ctx, const char *pathname_in)
        char *pathname = talloc_array(ctx, char, strlen(pathname_in)+2);
        const char *s = pathname_in;
        char *p = pathname;
-       bool wrote_slash = false;
 
        if (pathname == NULL) {
                return NULL;
@@ -124,7 +123,6 @@ char *canonicalize_absolute_path(TALLOC_CTX *ctx, const char *pathname_in)
 
        /* Always start with a '/'. */
        *p++ = '/';
-       wrote_slash = true;
 
        while (*s) {
                /* Deal with '/' or multiples of '/'. */
@@ -134,13 +132,12 @@ char *canonicalize_absolute_path(TALLOC_CTX *ctx, const char *pathname_in)
                                s++;
                        }
                        /* Update target with one '/' */
-                       if (!wrote_slash) {
+                       if (p[-1] != '/') {
                                *p++ = '/';
-                               wrote_slash = true;
                        }
                        continue;
                }
-               if (wrote_slash) {
+               if (p[-1] == '/') {
                        /* Deal with "./" or ".\0" */
                        if (s[0] == '.' &&
                                        (s[1] == '/' || s[1] == '\0')) {
@@ -151,7 +148,6 @@ char *canonicalize_absolute_path(TALLOC_CTX *ctx, const char *pathname_in)
                                        s++;
                                }
                                /* Don't write anything to target. */
-                               /* wrote_slash is still true. */
                                continue;
                        }
                        /* Deal with "../" or "..\0" */
@@ -164,9 +160,9 @@ char *canonicalize_absolute_path(TALLOC_CTX *ctx, const char *pathname_in)
                                        s++;
                                }
                                /*
-                                * As wrote_slash is true, we go back
-                                * one character to point p at the slash
-                                * we just saw.
+                                * As we're on the slash, we go back
+                                * one character to point p at the
+                                * slash we just saw.
                                 */
                                if (p > pathname) {
                                        p--;
@@ -188,15 +184,13 @@ char *canonicalize_absolute_path(TALLOC_CTX *ctx, const char *pathname_in)
                                p++;
 
                                /* Don't write anything to target. */
-                               /* wrote_slash is still true. */
                                continue;
                        }
                }
                /* Non-separator character, just copy. */
                *p++ = *s++;
-               wrote_slash = false;
        }
-       if (wrote_slash) {
+       if (p[-1] == '/') {
                /*
                 * We finished on a '/'.
                 * Remove the trailing '/', but not if it's