From patchwork Sat Oct 24 13:02:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23196 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 76DB644ABEE for ; Sat, 24 Oct 2020 16:03:27 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 519AB68A7D8; Sat, 24 Oct 2020 16:03:27 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5E18A68A71D for ; Sat, 24 Oct 2020 16:03:20 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id y12so5808404wrp.6 for ; Sat, 24 Oct 2020 06:03:20 -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=6RBoz2NdtWj5rbOpS14Z7g5PcLLOZQBxQXKJSbt+bkQ=; b=n3LdHtDhh6S2/UkRijYvcFjCaKQ8HM0RCF4izePlsNMNGtS5uo+9tGdwQohliTrFym 4K+IMyd+Nqf9k5abnZ3GWoYd5zBHKaothFtBv0AxRlzfscVAWJjVfzjq29ea6c951kIM 9si3rbRqFyMTCgnTbt9NNsFblMnS4M5yTP9xX+4SXP/WJxpiXC0t4uJ4BT6nUITUq2jE 3/Z8ifW1/44luYcPtmYUWUITM1cW8QsWi0Q4CauCGT33KaKnBoSelMWXBGbRFZQPNnbM Y9BKNA/oWf/iMJvqyHSyocHAt/wkL4uVPE5j3Go6pM2RybFP8ciQePEes9zyI9f+Rsg/ zOIg== 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=6RBoz2NdtWj5rbOpS14Z7g5PcLLOZQBxQXKJSbt+bkQ=; b=UWkT5cL46zjdExfEyNcMhq0hozyWLQVJnvV1bj6WSRgpGVg8Cb6chySmF4xPgqlRza 8NjN+6+/I2k7Dg0sTScJWh/TO2HLAMoHqpiDIc2XD4CEz1s+Fued36qadX5k0jZgFuoN LJuqz10L/0Cs7nXHJtXO+9Jw39XK/0g0TYGTOLSaWYvkOgRWl1Aoj/akZTfpZtg8++7x hU3J3azpFFFpmh0v5mdsRHYAQ+KbqGOqB5nA/ShUgCZuLJ5J0TWQyBxpY13/OX7tZGOV 3VYZOza/ZTosH5gTTTZA1UhQtqDVCgWu8DRtOWKjZqNzBbyf65CbSQYNukGiB5ulEfbF PgWQ== X-Gm-Message-State: AOAM530R4vQVjFgfe4BFdUHszwlZ9F4hYo6sglgd0nZp+ky7BX+Yts+j avSNV8CI4Fs/gwrOFbUUITE+W/UMqIc= X-Google-Smtp-Source: ABdhPJwNp9of5vB9hz3aPfuZkXipoVsz1ky4wep0Gl+QlJseu7tnnetGjic+9qtOSi8zvgOP4FYQvA== X-Received: by 2002:adf:ea4d:: with SMTP id j13mr7786859wrn.345.1603544599362; Sat, 24 Oct 2020 06:03:19 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d129sm10978479wmd.5.2020.10.24.06.03.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Oct 2020 06:03:18 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 24 Oct 2020 15:02:59 +0200 Message-Id: <20201024130300.45644-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/bitstream: Check code length before truncating to uint8_t 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" Signed-off-by: Andreas Rheinhardt --- The main motivation for this patch is actually the second patch and not the improved check. libavcodec/bitstream.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libavcodec/bitstream.c b/libavcodec/bitstream.c index 77c2b9ce05..ffa352b28b 100644 --- a/libavcodec/bitstream.c +++ b/libavcodec/bitstream.c @@ -302,15 +302,17 @@ int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, j = 0; #define COPY(condition)\ for (i = 0; i < nb_codes; i++) { \ - GET_DATA(buf[j].bits, bits, i, bits_wrap, bits_size); \ + unsigned len; \ + GET_DATA(len, bits, i, bits_wrap, bits_size); \ if (!(condition)) \ continue; \ - if (buf[j].bits > 3*nb_bits || buf[j].bits>32) { \ - av_log(NULL, AV_LOG_ERROR, "Too long VLC (%d) in init_vlc\n", buf[j].bits);\ + if (len > 3*nb_bits || len > 32) { \ + av_log(NULL, AV_LOG_ERROR, "Too long VLC (%u) in init_vlc\n", len);\ if (buf != localbuf) \ av_free(buf); \ return AVERROR(EINVAL); \ } \ + buf[j].bits = len; \ GET_DATA(buf[j].code, codes, i, codes_wrap, codes_size); \ if (buf[j].code >= (1LL< nb_bits); + COPY(len > nb_bits); // qsort is the slowest part of init_vlc, and could probably be improved or avoided AV_QSORT(buf, j, struct VLCcode, compare_vlcspec); - COPY(buf[j].bits && buf[j].bits <= nb_bits); + COPY(len && len <= nb_bits); nb_codes = j; ret = build_table(vlc, nb_bits, nb_codes, buf, flags); From patchwork Sat Oct 24 13:03:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23197 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 A75F344B5BB for ; Sat, 24 Oct 2020 16:04:04 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 87F3F68AE41; Sat, 24 Oct 2020 16:04:04 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B0FD0688368 for ; Sat, 24 Oct 2020 16:03:57 +0300 (EEST) Received: by mail-wm1-f67.google.com with SMTP id v5so5182339wmh.1 for ; Sat, 24 Oct 2020 06:03:57 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=J5ZFKGpPwhsvKwRpgDzBj2cybAzaVEKvBWJ8Tuw1QQo=; b=USGRjoLjsZblhbkPsYfAYJMWhzH/FJJOjutW7+J20a5mn7MqAkLd48PBGfmCDuMPcC kYhNQZbdMt9doXlFY0lP/r7xVQMGs69cZfU6xwmDDCnZ5qzOtuy4Lf2nJATT0mcnasij Ldk6OK8K6fSKt0dP6fyc4zg+WLzzi8iGmC8CxWEhStXG1CXPP78ikV9iu/kQ6c+DDRgF T8OcMq6Q3Fk+IpVueXtCcOfFPQMmCPtxuyOdIjdiYKTpUG0qbin1Ys/6Ji090TOACEn2 99iRv+ksd4H+G3CSvuqliBxEYEQggUoyfPOgokuK+X4EhbCCYqzlsQ4TpG/Dq2RgIi45 0Sdw== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=J5ZFKGpPwhsvKwRpgDzBj2cybAzaVEKvBWJ8Tuw1QQo=; b=Inj8kMtr4Kl41XKffu9gG7IfLM8hf8K5IzK0YaPxLn+r9EHA6p4vCmqfXI4Qskjlj1 kgLTIWfH9v4xLG4qsu0hpFQs5ytsOPfC1LKEr61ppmJMYKX7vgEL6QHD8h4g0QLQ51It 0WIg1pTGkGOsXbTYLq+kNrKbAT8lGeLn5mXjKHKkgKIoh2IrPPX90Yh/xwqCttsqzTkz /MBGeUxt+puY8zux0oagChDo/Pw2gR3CAN7jRgGD085akPZng2WAHD7jWyMwgOFxn0Q4 AR6HZpNfDpbfJr4lXCsE4S9jfjn3aGU3+AO6qxsmPz+XRRK/bxkIsNfJcwRwGiNPGbyy an9w== X-Gm-Message-State: AOAM5319JP1N6Qq0AKFNkKfjjvCb4XElv+OXE9Z50XEj8RvLC6ybPIn6 HQ97RwSK7slJKImpDt8B8StlR8CnsCo= X-Google-Smtp-Source: ABdhPJyc2SGC8c8QSXGecXKcA2cp1RaZWIFXj5P8SdgvDgszLZssh2UekSB88guE3LXSqy0o16/UOQ== X-Received: by 2002:a1c:7d49:: with SMTP id y70mr6965568wmc.103.1603544631314; Sat, 24 Oct 2020 06:03:51 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id d129sm10978479wmd.5.2020.10.24.06.03.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Oct 2020 06:03:49 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 24 Oct 2020 15:03:00 +0200 Message-Id: <20201024130300.45644-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201024130300.45644-1-andreas.rheinhardt@gmail.com> References: <20201024130300.45644-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] avcodec/bitstream: Stop allocating one VLCcode more than needed 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" Allocating one temporary entry more than needed was made necessary by the COPY loop below writing an element before having checked that it should be written at all. But given that this behaviour changed, the need for overallocating is gone. Signed-off-by: Andreas Rheinhardt --- libavcodec/bitstream.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libavcodec/bitstream.c b/libavcodec/bitstream.c index ffa352b28b..39749c6092 100644 --- a/libavcodec/bitstream.c +++ b/libavcodec/bitstream.c @@ -281,7 +281,7 @@ int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, vlc = vlc_arg; vlc->bits = nb_bits; if (flags & INIT_VLC_USE_NEW_STATIC) { - av_assert0(nb_codes + 1 <= FF_ARRAY_ELEMS(localbuf)); + av_assert0(nb_codes <= FF_ARRAY_ELEMS(localbuf)); localvlc = *vlc_arg; vlc = &localvlc; vlc->table_size = 0; @@ -290,8 +290,8 @@ int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, vlc->table_allocated = 0; vlc->table_size = 0; } - if (nb_codes + 1 > FF_ARRAY_ELEMS(localbuf)) { - buf = av_malloc_array((nb_codes + 1), sizeof(VLCcode)); + if (nb_codes > FF_ARRAY_ELEMS(localbuf)) { + buf = av_malloc_array(nb_codes, sizeof(VLCcode)); if (!buf) return AVERROR(ENOMEM); } else