[vlc-devel] [PATCH 2/2] url: Remove duplicated path separators

Hugo Beauzée-Luyssen hugo at beauzee.fr
Fri Feb 21 12:09:21 CET 2020


Otherwise the doted segments removal may be confused.
For instance vlc_uri_resolve("scheme:///a/b/c//file.ext", "../folder")
would return scheme:///a/b/c/folder instead of scheme:///a/b/folder
---
 src/test/url.c | 20 ++++++++++++++++++++
 src/text/url.c | 23 +++++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/src/test/url.c b/src/test/url.c
index a3a7c77847..3912e1a78c 100644
--- a/src/test/url.c
+++ b/src/test/url.c
@@ -164,6 +164,16 @@ static void test_rfc3986(const char *reference, const char *expected)
     test(vlc_uri_resolve_rfc3986_test, reference, expected);
 }
 
+static char *vlc_uri_resolve_separators_test(const char *in)
+{
+    return vlc_uri_resolve("file:///a/b/c//d.ext", in);
+}
+
+static void test_separators(const char *reference, const char *expected)
+{
+    test(vlc_uri_resolve_separators_test, reference, expected);
+}
+
 int main (void)
 {
     (void)setvbuf (stdout, NULL, _IONBF, 0);
@@ -375,6 +385,16 @@ int main (void)
 
     for (size_t i = 0; i < ARRAY_SIZE(rfc3986_cases); i += 2)
         test_rfc3986(rfc3986_cases[i], rfc3986_cases[i + 1]);
+    static const char* separators_patterns[] = {
+        "../",                      "file:///a/b/",
+        "./",                       "file:///a/b/c/",
+        "../../../../../../../",    "file:///",
+        "..///////////////",        "file:///a/b/",
+        ".///////////////",         "file:///a/b/c/",
+        "..//..//",                 "file:///a/",
+    };
+    for (size_t i = 0; i < ARRAY_SIZE(separators_patterns); i += 2)
+        test_separators(separators_patterns[i], separators_patterns[i + 1]);
 
     /* Check that fixup does not mangle valid URIs */
     static const char *valid_uris[] =
diff --git a/src/text/url.c b/src/text/url.c
index 0c15a9bcb7..fd21d19936 100644
--- a/src/text/url.c
+++ b/src/text/url.c
@@ -407,6 +407,28 @@ static bool vlc_uri_path_validate(const char *str)
     return vlc_uri_component_validate(str, "/@:");
 }
 
+static void vlc_uri_fixup_duplicated_separators(char *str)
+{
+    if (str == NULL)
+        return;
+    fprintf(stderr, "Input: %s\n", str);
+    char *input = str, *output = str;
+    while (*input)
+    {
+        assert(input >= output);
+        if (*input == '/')
+        {
+            *output++ = *input;
+            while (*input == '/')
+                input++;
+        }
+        else
+            *output++ = *input++;
+    }
+    *output = 0;
+    fprintf(stderr, "Output: %s\n", str);
+}
+
 static int vlc_UrlParseInner(vlc_url_t *restrict url, const char *str)
 {
     url->psz_protocol = NULL;
@@ -551,6 +573,7 @@ static int vlc_UrlParseInner(vlc_url_t *restrict url, const char *str)
     {
         url->psz_path = cur;
     }
+    vlc_uri_fixup_duplicated_separators(url->psz_path);
 
     return ret;
 }
-- 
2.20.1



More information about the vlc-devel mailing list