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)