From patchwork Mon Jul 27 13:07:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Steven X-Patchwork-Id: 21288 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 43B0244A60F for ; Mon, 27 Jul 2020 16:08:17 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1A81668B81C; Mon, 27 Jul 2020 16:08:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtpbguseast2.qq.com (smtpbguseast2.qq.com [54.204.34.130]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5468168B6E6 for ; Mon, 27 Jul 2020 16:08:09 +0300 (EEST) X-QQ-mid: bizesmtp24t1595855282troeqgm8 Received: from localhost (unknown [103.107.216.232]) by esmtp10.qq.com (ESMTP) with id ; Mon, 27 Jul 2020 21:08:01 +0800 (CST) X-QQ-SSF: 01100000002000Z0ZXF0B00A0000000 X-QQ-FEAT: gXZG3PbKWBvUZ1Qiu9pKWQW+ySIwrRSPYZaiOJS1bw9+wDpY7qe1yn5B3jtF9 5glEq38TcUOYeRw5qscxPL5waT96DywLeQ02vWvFTufZDS4dZBaD3Gw7HzKgam6zJMW3HQB W9m/icScwTdBB/8/A5gKNRV16259X36Ao9PpflZIQf52E8S9SYljYQjDc3sitSkxZRyPEcB d+S3A8OkTASUlSq42jsYpOvWN1KD7b8xHXYgHaHVrbPFjZtWgsw0dyZi0e6SaOjfF+gMGf2 WN85kLHwvf+sc6YHHzHUFmkgMn1dusznKDi+3hNXjJ++0tsYUbMM+7/TI= X-QQ-GoodBg: 0 From: Steven Liu To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Jul 2020 21:07:47 +0800 Message-Id: <20200727130748.90906-1-lq@chinaffmpeg.org> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:chinaffmpeg.org:qybgforeign:qybgforeign5 X-QQ-Bgrelay: 1 Subject: [FFmpeg-devel] [PATCH v3 1/2] avformat/url: rework for trim_double_dot_url X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Steven Liu Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" use two buffer for storage the path and node of URI remove the last node of the path if the next node is .. change the static strings to dynamic alloc space by size argument. Signed-off-by: Steven Liu --- libavformat/url.c | 83 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 19 deletions(-) diff --git a/libavformat/url.c b/libavformat/url.c index 20463a6674..4fb703de78 100644 --- a/libavformat/url.c +++ b/libavformat/url.c @@ -82,41 +82,78 @@ static void trim_double_dot_url(char *buf, const char *rel, int size) { const char *p = rel; const char *root = rel; - char tmp_path[MAX_URL_SIZE] = {0, }; + char *tmp_path = NULL; + char *second_buf = NULL; + char *first_buf = NULL; char *sep; char *node; + tmp_path = av_mallocz(size); + if (!tmp_path) + return; + + second_buf = av_mallocz(size); + if (!second_buf) + goto fail; + + first_buf = av_mallocz(size); + if (!first_buf) + goto fail; + /* Get the path root of the url which start by "://" */ if (p && (sep = strstr(p, "://"))) { sep += 3; root = strchr(sep, '/'); if (!root) - return; + goto fail; } + av_strlcat(tmp_path, p, root - p + 1); /* set new current position if the root node is changed */ p = root; - while (p && (node = strstr(p, ".."))) { - av_strlcat(tmp_path, p, node - p + strlen(tmp_path)); - p = node + 3; - sep = strrchr(tmp_path, '/'); + if (*p == '/') + p++; + while (p && (node = strchr(p, '/'))) { + av_strlcpy(second_buf, p, node - p + 1); + p = node + 1; + if (!strcmp(second_buf, "..")) { + if (strlen(first_buf) <= 0) + continue; + sep = strrchr(first_buf, '/'); + if (sep) + sep[0] = '\0'; + else + memset(first_buf, 0, size); + + memset(second_buf, 0, size); + } else { + av_strlcat(first_buf, "/", size); + av_strlcat(first_buf, second_buf, size); + } + } + if (!strcmp(p, "..")) { + sep = strrchr(first_buf, '/'); if (sep) sep[0] = '\0'; else - tmp_path[0] = '\0'; + memset(first_buf, 0, size); + } else { + av_strlcat(first_buf, "/", size); + av_strlcat(first_buf, p, size); } - if (!av_stristart(p, "/", NULL) && root != rel) - av_strlcat(tmp_path, "/", size); - - av_strlcat(tmp_path, p, size); - /* start set buf after temp path process. */ - av_strlcpy(buf, rel, root - rel + 1); - - if (!av_stristart(tmp_path, "/", NULL) && root != rel) - av_strlcat(buf, "/", size); + if (p > root) { + av_strlcat(tmp_path, first_buf, size); + } else { + av_strlcat(tmp_path, p, size); + } av_strlcat(buf, tmp_path, size); + +fail: + av_free(second_buf); + av_free(first_buf); + av_free(tmp_path); } void ff_make_absolute_url(char *buf, int size, const char *base, @@ -124,9 +161,11 @@ void ff_make_absolute_url(char *buf, int size, const char *base, { char *sep, *path_query; char *root, *p; - char tmp_path[MAX_URL_SIZE]; + char *tmp_path = av_mallocz(size); + + if (!tmp_path) + return; - memset(tmp_path, 0, sizeof(tmp_path)); /* Absolute path, relative to the current server */ if (base && strstr(base, "://") && rel[0] == '/') { if (base != buf) @@ -148,12 +187,14 @@ void ff_make_absolute_url(char *buf, int size, const char *base, trim_double_dot_url(tmp_path, buf, size); memset(buf, 0, size); av_strlcpy(buf, tmp_path, size); + av_free(tmp_path); return; } /* If rel actually is an absolute url, just copy it */ if (!base || strstr(rel, "://") || rel[0] == '/') { memset(buf, 0, size); trim_double_dot_url(buf, rel, size); + av_free(tmp_path); return; } if (base != buf) @@ -170,6 +211,7 @@ void ff_make_absolute_url(char *buf, int size, const char *base, trim_double_dot_url(tmp_path, buf, size); memset(buf, 0, size); av_strlcpy(buf, tmp_path, size); + av_free(tmp_path); return; } @@ -180,8 +222,10 @@ void ff_make_absolute_url(char *buf, int size, const char *base, if (sep) { sep += 3; root = strchr(sep, '/'); - if (!root) + if (!root) { + av_free(tmp_path); return; + } } } @@ -218,6 +262,7 @@ void ff_make_absolute_url(char *buf, int size, const char *base, trim_double_dot_url(tmp_path, buf, size); memset(buf, 0, size); av_strlcpy(buf, tmp_path, size); + av_free(tmp_path); } AVIODirEntry *ff_alloc_dir_entry(void) From patchwork Mon Jul 27 13:07:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Steven X-Patchwork-Id: 21289 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 33CE944A60F for ; Mon, 27 Jul 2020 16:08:36 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1EA0468B837; Mon, 27 Jul 2020 16:08:36 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtpbguseast2.qq.com (smtpbguseast2.qq.com [54.204.34.130]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4272268B6E6 for ; Mon, 27 Jul 2020 16:08:29 +0300 (EEST) X-QQ-mid: bizesmtp6t1595855287t3wsr8weq Received: from localhost (unknown [103.107.216.232]) by esmtp6.qq.com (ESMTP) with id ; Mon, 27 Jul 2020 21:08:07 +0800 (CST) X-QQ-SSF: 01100000002000Z0ZXF0B00A0000000 X-QQ-FEAT: WX1oBb0zbX23tqlV0NVVrM9VFJD8hs9MUMJ5WonaZe6s64wfWH8zE24BCzh7b 5uMTuq6HxacoII+LqRRHAey91zHVKChSAZICkpcAcz0U5TI10J1teqbKPeBcVIqsk01K2Ma h/ZGEtpYBMKW4dDaSUPGF8EF9a01VnF++XqS4riViaC2floczegU3QAJIliNRPeTuMY+H79 +7EdJjR7kmxya8gDZGb4Ko01zUgA1JUAEtTpbRlSYdNTK13E6ps8rZgVYmSMqgBLTVkSMbV h15IyqNrtqsdS3GUx3kAfADrd6SJ81602/nGi9VL4qfg6WN1juDHXbEYSYApqcGJ15tjG/S MOuoe3A X-QQ-GoodBg: 0 From: Steven Liu To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Jul 2020 21:07:48 +0800 Message-Id: <20200727130748.90906-2-lq@chinaffmpeg.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200727130748.90906-1-lq@chinaffmpeg.org> References: <20200727130748.90906-1-lq@chinaffmpeg.org> MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:chinaffmpeg.org:qybgforeign:qybgforeign5 X-QQ-Bgrelay: 1 Subject: [FFmpeg-devel] [PATCH v3 2/2] avformat/tests/url: add test cases for .. and last node is .. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Steven Liu Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Steven Liu --- libavformat/tests/url.c | 14 ++++++++++++++ tests/ref/fate/url | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/libavformat/tests/url.c b/libavformat/tests/url.c index 1d961a1b43..0de511caf9 100644 --- a/libavformat/tests/url.c +++ b/libavformat/tests/url.c @@ -24,6 +24,8 @@ static void test(const char *base, const char *rel) { char buf[200], buf2[200]; + memset(buf, 0, 200); + memset(buf2, 0, 200); ff_make_absolute_url(buf, sizeof(buf), base, rel); printf("%50s %-20s => %s\n", base, rel, buf); if (base) { @@ -66,7 +68,19 @@ int main(void) test("http://server/foo/bar?param=value/with/slashes", "/baz"); test("http://server/foo/bar?param&otherparam", "?someparam"); test("http://server/foo/bar", "//other/url"); + test("http://server/foo/bar", "../other/url"); + test("http://server/foo/bar", "other/url"); test("http://server/foo/bar", "../../../../../other/url"); + test("http://server/foo/bar", "../../../../../other/url/test..mp3"); + test("http://server/foo/bar", "../../../../../other/url/test.."); + test("http://server/foo/bar", "../../../../../other/url/test/..."); + test("http://server/foo/bar", "../../../../../other/url/.../test/out"); + test("http://server/foo/bar", "../../../../../other/url/.../../test/out"); + test("http://server/foo/bar", "../../../../../other/url/.../..test/out"); + test("http://server/foo/bar", "../../../../../other/url/.."); + test("http://server/foo/bar", "../../../../../other/url/..mp3"); + test("http://server/foo/bar", "../../../../../other/url/..test/mp3"); + test("http://server/foo/bar", "../../../../../other/url/test../mp3"); test("http://server/foo/bar", "/../../../../../other/url"); test("http://server/foo/bar", "/test/../../../../../other/url"); test("http://server/foo/bar", "/test/../../test/../../../other/url"); diff --git a/tests/ref/fate/url b/tests/ref/fate/url index 533ba2cb1e..35eee25f4a 100644 --- a/tests/ref/fate/url +++ b/tests/ref/fate/url @@ -13,7 +13,19 @@ Testing ff_make_absolute_url: http://server/foo/bar?param=value/with/slashes /baz => http://server/baz http://server/foo/bar?param&otherparam ?someparam => http://server/foo/bar?someparam http://server/foo/bar //other/url => http://other/url + http://server/foo/bar ../other/url => http://server/other/url + http://server/foo/bar other/url => http://server/foo/other/url http://server/foo/bar ../../../../../other/url => http://server/other/url + http://server/foo/bar ../../../../../other/url/test..mp3 => http://server/other/url/test..mp3 + http://server/foo/bar ../../../../../other/url/test.. => http://server/other/url/test.. + http://server/foo/bar ../../../../../other/url/test/... => http://server/other/url/test/... + http://server/foo/bar ../../../../../other/url/.../test/out => http://server/other/url/.../test/out + http://server/foo/bar ../../../../../other/url/.../../test/out => http://server/other/url/test/out + http://server/foo/bar ../../../../../other/url/.../..test/out => http://server/other/url/.../..test/out + http://server/foo/bar ../../../../../other/url/.. => http://server/other + http://server/foo/bar ../../../../../other/url/..mp3 => http://server/other/url/..mp3 + http://server/foo/bar ../../../../../other/url/..test/mp3 => http://server/other/url/..test/mp3 + http://server/foo/bar ../../../../../other/url/test../mp3 => http://server/other/url/test../mp3 http://server/foo/bar /../../../../../other/url => http://server/other/url http://server/foo/bar /test/../../../../../other/url => http://server/other/url http://server/foo/bar /test/../../test/../../../other/url => http://server/other/url