From patchwork Sat Aug 29 09:02:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 21979 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 ADADB44B04F for ; Sat, 29 Aug 2020 13:12:09 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 75FF968A039; Sat, 29 Aug 2020 13:12:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f68.google.com (mail-ej1-f68.google.com [209.85.218.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 33690680821 for ; Sat, 29 Aug 2020 13:12:02 +0300 (EEST) Received: by mail-ej1-f68.google.com with SMTP id a26so2369578ejc.2 for ; Sat, 29 Aug 2020 03:12:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=wNCx/sFDtGuw1viXkst9dij0sShQwe4+Q+bEDcuvXms=; b=Cv1u/qu9FFtUdsDgNL8Ov6NKl3hhA3IMnXGm2tEmpL18W0jjDpbMUaqkZQCUTBvzXt 032Swiz3tbSIOdFADqTOifaVlOHUCb8Vmng5cfEeSf8oU3rRrE8sLoyuajQQ5D9kh2ib 2+sfyAbCiUhzwiqGJI/P5plxREWFJuvdYX2XHh3EdAZfLU8t/kosaYavN78+ycUXBP0a mzxIxlJzFHm9ami7BHPnF4IzGZqOz8NjSdtt2zsxdYtgVTrAuBkgP+YxSZV8UNPYfMBi /vor/z2Hooq05nZCCWk2tM4s+WL2uCPBUYbnSN+kWs0C1SUB/2kHp/y/82BjIiJwROHV qKvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=wNCx/sFDtGuw1viXkst9dij0sShQwe4+Q+bEDcuvXms=; b=UHTln9lVYXoAh5QNgjQXHkRY9LzJOWJC1UUxk2jhMUrWMOXoav3lGYNcMUjVBW2DiR DxyFUu20wdpkN3aBV1LmiT8wMoFqDG8GrMDm6yEl+uAA5O8KR9V2/PHVXFCMDSnPIuVP AErrhSTN22WQxfYZcuMmUKMlvnptvYJ0ZZOiNDV6DFRwLNISma1RsGZo3bzF/NVDmXgX xoFMWv8JeeYvHfzDoKRkOTbZ2TSkTIElr2SWB4GhylLSCan6g9AYrS47yBVVriGHh9Be 1Q6flvOdJyjg1UgrmH5JrzbtQYUHCjb/ku99YsarN1Vx/3qA1gilqKmWcDaI9TopKYwW QCCA== X-Gm-Message-State: AOAM530P+ivkCD4vr1oUGf/SfK8Tc8o0xE9BbQwSCI7eDdGn3WPzGa7P ezJxW7Jz2C1nDFYLn/WJmx+ZhKLAXow= X-Google-Smtp-Source: ABdhPJxJoDGc4QnuEmQnIuMgw8ZGRVblxM7pPUsippp9XHtA/ypHoDxUeUYtEVE8u2SvtMXc+J8AXA== X-Received: by 2002:adf:ca06:: with SMTP id o6mr2602913wrh.181.1598691746526; Sat, 29 Aug 2020 02:02:26 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id u16sm2575696wmc.7.2020.08.29.02.02.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Aug 2020 02:02:25 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 29 Aug 2020 11:02:16 +0200 Message-Id: <20200829090216.14117-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avcodec/truemotion2: Avoid duplicating array, fix memleak 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" TrueMotion 2.0 uses Huffmann trees. To parse them, the decoder allocates arrays for the codes, their lengths and their value; afterwards a VLC table is initialized using these values. If everything up to this point succeeds, a new buffer of the same size as the already allocated arrays for the values is allocated and upon success the values are copied into the new array; all the old arrays are then freed. Yet if allocating the new array fails, the old arrays get freed, but the VLC table doesn't. This leak is fixed by not allocating a new array at all; instead the old array is simply reused, ensuring that nothing can fail after the creation of the VLC table. Signed-off-by: Andreas Rheinhardt --- libavcodec/truemotion2.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/libavcodec/truemotion2.c b/libavcodec/truemotion2.c index d90a8baff3..a1d4eea340 100644 --- a/libavcodec/truemotion2.c +++ b/libavcodec/truemotion2.c @@ -200,8 +200,6 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) /* convert codes to vlc_table */ if (res >= 0) { - int i; - res = init_vlc(&code->vlc, huff.max_bits, huff.max_num, huff.lens, sizeof(int), sizeof(int), huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0); @@ -210,13 +208,8 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code) else { code->bits = huff.max_bits; code->length = huff.max_num; - code->recode = av_malloc_array(code->length, sizeof(int)); - if (!code->recode) { - res = AVERROR(ENOMEM); - goto out; - } - for (i = 0; i < code->length; i++) - code->recode[i] = huff.nums[i]; + code->recode = huff.nums; + huff.nums = NULL; } }