From patchwork Tue Sep 13 19:46:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 37906 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp906475pzh; Tue, 13 Sep 2022 12:46:44 -0700 (PDT) X-Google-Smtp-Source: AA6agR5uRoDqwNz1rKC40/ip3OXro2RQ4H5u+6sFXlW54zSU3p7WKCi50NKkYuB/yqwvSkymuVMm X-Received: by 2002:a17:907:1623:b0:77e:9455:b4d9 with SMTP id hb35-20020a170907162300b0077e9455b4d9mr6396837ejc.491.1663098404186; Tue, 13 Sep 2022 12:46:44 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id fj4-20020a1709069c8400b007317756bc04si9685021ejc.1006.2022.09.13.12.46.42; Tue, 13 Sep 2022 12:46:44 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@outlook.com header.s=selector1 header.b=Ye0oMxbH; arc=fail (body hash mismatch); spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 951F668BAAA; Tue, 13 Sep 2022 22:46:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-oln040092073070.outbound.protection.outlook.com [40.92.73.70]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1A33168B940 for ; Tue, 13 Sep 2022 22:46:33 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gew/My/OnMXsoayHD98MKM5iRanTNhoxwjllRiLQJxZqqt6orzk0FClWdvdetcBX5mrOWZkWUnF6JD2Cn/8MVL5fOqD/zasPc5Czy6CMmKxDS9bvaL4cDxRIztUc/2ClJVniuYtFf3AIr2BI/3h8tjo2IvxWa0c2DpkzxbyILnca1M0S/28TMJh1TTR4vfi8fyhGuEEaUR7q786fNeVyo1fal20Zq8LystLqy6Cd2mdPf3EcMe3TRKqLbc2FVNDAOSf4EP0D8qq7AUvMXNLptJ/liRHU+EOwJkmXA76xws+lwYbWEPPkDATZtYGti7/fVLjS3AqLHHvLQshKJZOsnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=pXvMuhoTxFT9Js6Uxe8UUrIkyRsBEvVWSuBEiOv1u2Y=; b=JBIC9W4HyKyjQxDdIZeChcbVA3XFi8e9OzBF0C/sjBUlkg8muJDIn0TMUr5VJXczEzxPs5nHZTy7iWtY87t+a/YWlY0zy33cQNfRjxlKCtxfC4p271nF6ZsctqCEPv20+GOcGuWofPm3kPogcuhqyQkSEY4RHY3LB5dC3hBrh5rbutNw6YGdG9zljCSdei0CbQQWB5hj7BD4DwK4oOMceQKDYp7EjAvCmLW9ezuVgt4OiCjQFq9QlbUDiMM+1NlwxRUt8lmPWdUWIehGNzWNkpQo9R21sHQNTBaxw+g8jofi0FxTJrpwMhPfBiyZO+xLk7jmpazIwCbpIYxWTQtJTQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=pXvMuhoTxFT9Js6Uxe8UUrIkyRsBEvVWSuBEiOv1u2Y=; b=Ye0oMxbH0CFE970nKexQJZXynneOYkfyz71VaPpG/ww5kzBct21p1+1lyRzG0LEDMzhB0VUplZQN6GXqGLqj8uo5ytOrAuymcYPNjcDEc3vaTXFue4cm2+PUSqyvUtnSERo25WjruwE5vMjI43yvj/+MGDmtq2sB6jv8tBYfQC76Cs3d6pVA/puO1qKPDVryD+Rd/yM4fIRoObMTms7qiLagepxO0PMNuM2huTdkJAwZBNYJkMkzEdpz1UeDoSknAJ5MGen1dbL4rPYK25ErqDIHGWwXPk6xTqS0uxIRKBW7FzO7AkUXuNwofxuUIRKPv56SUHZnOqfvOtzjJrsoMg== Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) by PAXP250MB0446.EURP250.PROD.OUTLOOK.COM (2603:10a6:102:276::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5612.22; Tue, 13 Sep 2022 19:46:31 +0000 Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::51a4:ed23:ea45:dfeb]) by GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::51a4:ed23:ea45:dfeb%8]) with mapi id 15.20.5612.022; Tue, 13 Sep 2022 19:46:31 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 13 Sep 2022 21:46:27 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-TMN: [d4yRNiEYrwokV3a++o9qqQz1qE0bvq52JF7unBBuIlk=] X-ClientProxiedBy: ZR0P278CA0115.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:20::12) To GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) X-Microsoft-Original-Message-ID: <20220913194628.922446-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV1P250MB0737:EE_|PAXP250MB0446:EE_ X-MS-Office365-Filtering-Correlation-Id: 31bdd4b9-ce8a-44a9-43da-08da95c0a840 X-MS-Exchange-SLBlob-MailProps: AZnQBsB9XmpEumVcsW5TnTvXPpg27e8v6Y3/HUwb76MXFyeGcSeWgeRKvazMioolJS7udSERk0ttLP+wf+xiwdAAD67QCAKZ2m8xZOuzTO6NWAVi76bZMOgYqvTNjvGDRrbE3R/ySAq5Fy84VqbLgSJqzx7TDjjPA9UR5zfyEYmVKqU+C0NEElkwWEVSYRfIWafC/oNOAcpsDZFiZvJWE+Mrcn5WIpv6Kmo+Qqrr+Ms9eIdximOPC3DipVs73aB/5kwcN1C0q4D49PC9CtfMbRQhKj9aA7Vc02FgHRP+S6VshvPgQaZ3Bq70TdvhsRtqmwFMy+veFJwRLAdLvxDzc/LpeUp5zbxiRd2/B85Qh8S6KsdcGLmkyPyuueC4rjn5mVZQDfaQLRCVGbl4JdLDS4mPWzKHM6GWyOQWkaY7brf8E3UmerRmzSvH/QuqLUFzY3pMNBVOdYNzqMZ6OC5073awNIRpnPU4oR+FX6rsfD4Wf8uGDVoVM4tl1ZaPcctLvRTc5lRottoFa9O1p4i0kmpFC2NtbW2wruzsPD5/P+b7cqIbxFb/7Sp9RRxLts+E9KRJNE8b16PUwWs1vTwWgCukI4sPclZO1q1htgbe3XguIQdeHcXh5JvpWungHpq6SQTdxnSScz8jV6KYubW5jMb6abzCym4UBQub0nG0IsbUlouLtxfE9UaCmQ3vh081tqnNA6acEUCYdGfRpk/s4UBCfXUWUn3o1tU5i0PU0+r+JIHGmV8jxDOL1/aFc5Yj7WXsUqBAx04= X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MOpkwUUtu1cQ/6O6ymfLTPhPXxD/Ai4IpSuLiTUlglH82J/NzgpVJIFUKw5Exi0z2+DjECrZJRn+GVfOkrJzR4N2We/t/cmRiSROx7PYCCngtpTHFilf6FToX2viL+s7SDToz9pspQQ08dDzajGSxMctxFxMgZojnkRVK+uIOh3TCZ8RvWpDnj1km7NIkg886+RYS4r2F+x+TdQ/bpYjfvPqd5dKQth0EoWTsGLEyFkPZHiQpJe3hr/3AqOB/NLz5hesZUsAK1g4nTCNdz+74B4vh1ay3GG5EpaO/240pIdggY6xFvr3KqIRUFtVV74nQfm3bQKDhCPxeDHKxg3FkeL2O0un0qkm92ikMbEeDHgBIhwxM77rXLDs8eFW0mCs1DMCGj9WLVplqQtebjObrukN+xUKR0Q9oSKurvmllBfHU69NnwUyT95ED5fRB1mGVDiDn8rZQZ8Dj66oXUo1Mf9UIe06zkpz0ZRbeQyXH7oSPMGF7FnEassluF/4H4lqDtPu9jq8cWrgqI/nC27unN8DnmhbD+HK/WPbN3w/DCiiKJUagYSUYFxc1UDsnhKDU4Fh6tWl/kIFufVixzfL7+jLFKcY2OMS+8tN90ya/dzqkdDW2WAUAplT+DyAU3AWWh6UB7M+fQt9fN6/+xHSOw== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: W33sdHOquLpMvzupQc9+muixypZczfnJaPWf0pul1tS6aQ0tGPuzq8bL4dwV4ZnE0fRV1sAGbS/bqe5Jf8rZuZ+5SD1TQ9GtpWu5Y8ihOCahigQnNgIBPEjV8ncxSgeAZScHpUTxgLpOhhnXTvyKBPOT0uRColagrdDnu7xDAd0YpdFd3HTXRxO032Z+wNwAf3HOB92vbQVhdaanabqWQkePQCOk5ysW1tXZn2hv54H50hE49BScIvzu0RDu9yxpqpXaV9gAoejFHozxHNUc/tsrva/lCCKv/wn++OuHfF/bEPQOAiaDRBLt7CQWypmo5mTZameIG2AAEJyMQ7bFELyrUMbxLsxpiaD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MS-Exchange-CrossTenant-AuthSource: GV1P250MB0737.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Sep 2022 19:46:31.7108 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXP250MB0446 Subject: [FFmpeg-devel] [PATCH 2/3] avutil/dict: Improve appending values X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: BzuCx/xgyxoS When appending two values (due to AV_DICT_APPEND), the earlier code would first zero-allocate a buffer of the required size and then copy both parts into it via av_strlcat(). This is problematic, as it leads to quadratic performance in case of frequent enlargements. Fix this by using av_realloc() (which is hopefully designed to handle such cases in a better way than simply throwing the buffer we already have away) and by copying the string via memcpy() (after all, we already calculated the strlen of both strings). Signed-off-by: Andreas Rheinhardt --- libavutil/dict.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/libavutil/dict.c b/libavutil/dict.c index c01165634e..1968063b0b 100644 --- a/libavutil/dict.c +++ b/libavutil/dict.c @@ -98,16 +98,17 @@ int av_dict_set(AVDictionary **pm, const char *key, const char *value, return 0; } if (copy_value && flags & AV_DICT_APPEND) { - size_t len = strlen(tag->value) + strlen(copy_value) + 1; - char *newval = av_mallocz(len); + size_t oldlen = strlen(tag->value); + size_t new_part_len = strlen(copy_value); + size_t len = oldlen + new_part_len + 1; + char *newval = av_realloc(tag->value, len); if (!newval) goto err_out; - av_strlcat(newval, tag->value, len); - av_strlcat(newval, copy_value, len); + memcpy(newval + oldlen, copy_value, new_part_len + 1); av_freep(©_value); copy_value = newval; - } - av_free(tag->value); + } else + av_free(tag->value); av_free(tag->key); *tag = m->elems[--m->count]; } else if (copy_value) {