chiark / gitweb /
fs-util: chase_symlinks(): remove unnecessary slash at the head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 30 Nov 2017 17:19:44 +0000 (02:19 +0900)
committerSven Eden <yamakuzure@gmx.net>
Thu, 30 Nov 2017 17:19:44 +0000 (02:19 +0900)
Before this, chase_symlinks("/../../foo/bar",...) returns //foo/bar.
This removes the unnecessary slash at the head.

src/basic/fs-util.c

index d63eeb1d2df4ed32e18112cd4d53e62ad8bf148e..df8ed643a0f3c6c9d6a98268e6449e9059c05548 100644 (file)
@@ -728,6 +728,10 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
                                  * what we got so far. But don't allow this if the remaining path contains "../ or "./"
                                  * or something else weird. */
 
+                                /* If done is "/", as first also contains slash at the head, then remove this redundant slash. */
+                                if (streq_ptr(done, "/"))
+                                        *done = '\0';
+
                                 if (!strextend(&done, first, todo, NULL))
                                         return -ENOMEM;
 
@@ -800,6 +804,10 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
                         done = first;
                         first = NULL;
                 } else {
+                        /* If done is "/", as first also contains slash at the head, then remove this redundant slash. */
+                        if (streq(done, "/"))
+                                *done = '\0';
+
                         if (!strextend(&done, first, NULL))
                                 return -ENOMEM;
                 }