From patchwork Fri Feb 5 12:08:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 25453 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 0CFD144AF4B for ; Fri, 5 Feb 2021 14:08:25 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EC8A368A558; Fri, 5 Feb 2021 14:08:24 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f49.google.com (mail-ed1-f49.google.com [209.85.208.49]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7EBBB689818 for ; Fri, 5 Feb 2021 14:08:18 +0200 (EET) Received: by mail-ed1-f49.google.com with SMTP id y18so8498326edw.13 for ; Fri, 05 Feb 2021 04:08:18 -0800 (PST) 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=ZaMb8ewE2SYeRzlTyaCBFrIftmO3/ZsD9rtynGsq2aI=; b=fwCIgltIhd7W/ZSpSxpobWL8TfeRGIQf/u7lRMjl1qODEqfGgmJCVbCZVKTav1l0vI KvINKbKBK0nVKbgMpfgwZiV6vLbiQNZwATQDWlWKzcwfgp1UKTAwLp25MM7NtF7geWwT tZR71OPbbk2YZoWRxc7ZKTE7yYamzdENP53Z4uIunV0gXsRTpywtM1XM7lB78JywJAYT 5Tvd9ASIZ26V306BDR/EiW7skqoL/h9TOzp7rwF6JX38I5drK1cNANK74/Z7NpX4CCoj t7urEKtK0obChqIA+qcNx6lR4iBES62qHXGUUiZdWljQNSZfcUkPWHioNf5AZgGBjUeN +1yw== 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=ZaMb8ewE2SYeRzlTyaCBFrIftmO3/ZsD9rtynGsq2aI=; b=Q9zp2m3PQv15mgV7Vy6nlrwwUGdDWVmEFvEYJolpph6OzTy6R5/8k8sAugs2WWzoZ6 AiJbObbUT2rczt7sry6fJIO473z/M2F/4s1/Y/ScRjoTIseO2Sam5PaaQiKVZHJI50jU 7wPmbrdnUkhB4zDWK2Fc1pOmjWuWmWRzQbCLRRNb4IWpHNLo9UbDDsta2uid5RdB4MYd h+bRA3oLwjlqzbU346+f+cf0+z/4uFeGVq7HW7GdwCo1dTzAEhbj59BfcM2x2AHEvlI8 hax43/hUXy7Tdb2D9gPvD/UfRcsqVnTIWNuZt/C9iA4Oj7UXFkGzfyeNOlXzvyN+FbEG yLCQ== X-Gm-Message-State: AOAM531TjL0QvSobv5LMkYdPZdKCuumNc6tvASXkzGyowBOxLYnCvf4j gks0wAd5NYYPaNtB7dzmA39tICLg4jI= X-Google-Smtp-Source: ABdhPJw1ZMWtIjvgbHPCTq0Vjnl+nEUcX+jZlPvzuhldbIFBOGLvASwWodWoATZRxcTzuJlxNJMBHA== X-Received: by 2002:aa7:c2c7:: with SMTP id m7mr3204260edp.134.1612526897811; Fri, 05 Feb 2021 04:08:17 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id bd27sm3237226edb.37.2021.02.05.04.08.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Feb 2021 04:08:17 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 5 Feb 2021 13:08:11 +0100 Message-Id: <20210205120811.1618152-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avcodec/g722enc: Validate parameters before using them 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" In case trellis is outside of 0..23, an invalid shift and/or a signed integer overflow happens; furthermore, it can lead to the request to allocate nonsense amounts of memory. So validate first. Signed-off-by: Andreas Rheinhardt --- libavcodec/g722enc.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/libavcodec/g722enc.c b/libavcodec/g722enc.c index 9357f170fe..9e2ebf67c5 100644 --- a/libavcodec/g722enc.c +++ b/libavcodec/g722enc.c @@ -64,19 +64,6 @@ static av_cold int g722_encode_init(AVCodecContext * avctx) c->band[1].scale_factor = 2; c->prev_samples_pos = 22; - if (avctx->trellis) { - int frontier = 1 << avctx->trellis; - int max_paths = frontier * FREEZE_INTERVAL; - int i; - for (i = 0; i < 2; i++) { - c->paths[i] = av_mallocz_array(max_paths, sizeof(**c->paths)); - c->node_buf[i] = av_mallocz_array(frontier, 2 * sizeof(**c->node_buf)); - c->nodep_buf[i] = av_mallocz_array(frontier, 2 * sizeof(**c->nodep_buf)); - if (!c->paths[i] || !c->node_buf[i] || !c->nodep_buf[i]) - return AVERROR(ENOMEM); - } - } - if (avctx->frame_size) { /* validate frame size */ if (avctx->frame_size & 1 || avctx->frame_size > MAX_FRAME_SIZE) { @@ -110,6 +97,18 @@ static av_cold int g722_encode_init(AVCodecContext * avctx) avctx->trellis); avctx->trellis = new_trellis; } + if (avctx->trellis) { + int frontier = 1 << avctx->trellis; + int max_paths = frontier * FREEZE_INTERVAL; + + for (int i = 0; i < 2; i++) { + c->paths[i] = av_calloc(max_paths, sizeof(**c->paths)); + c->node_buf[i] = av_calloc(frontier, 2 * sizeof(**c->node_buf)); + c->nodep_buf[i] = av_calloc(frontier, 2 * sizeof(**c->nodep_buf)); + if (!c->paths[i] || !c->node_buf[i] || !c->nodep_buf[i]) + return AVERROR(ENOMEM); + } + } } ff_g722dsp_init(&c->dsp);