From patchwork Tue Dec 3 17:09:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16562 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 08A7C44ACC9 for ; Tue, 3 Dec 2019 19:09:27 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E554C68B08E; Tue, 3 Dec 2019 19:09:26 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 49A6768A641 for ; Tue, 3 Dec 2019 19:09:20 +0200 (EET) Received: by mail-wm1-f65.google.com with SMTP id f129so4402789wmf.2 for ; Tue, 03 Dec 2019 09:09:20 -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=hql+AbPS7KLiTdPyDsUN9mKjqyQf6d+pOmJ/fqP+s1g=; b=W97ODrBJ8RbZZcclxwveUnmpwPf0bVgV6NngawNF6DMAWSijrsFWS1CvASon+UmWGW nPiiwqP5pi2rKT5sF96GQpn0m+odLhwuFlU0o5FVlGUyPsvYlTyLKuN9speD0fsYeDHj E6AgM5K7F7akadO7dYHk3kaPOWfPPEFIy/R5vOaaqpTraKwCmwGfgqToeez6RRO9gT8k 4DAcH0/AtUKrg6pKGHoO/Ys+pd5778sAXYOcJ7mNEq9dW6G5uEFDsjI49Hp1bqtDluCb +g/U2VHfpj/sJDgjkQXRQDLz7CaVq85PSROdMfqcqjgzUknUHC56PYzb+4P+ZYb6EdXp PhZA== 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=hql+AbPS7KLiTdPyDsUN9mKjqyQf6d+pOmJ/fqP+s1g=; b=Uxx7bNQBBZbou8wRjP00OGlF+it8slFjrrf2VGW5G5E5Ux4XlKAGl5rj8sC/t3/OM8 RPjqrRxEK7/+DMrVmnnBOuccEFG2piQ923Pu/k9Pg+CeyZECxtD/7CbRiauj1ZLJPPcp 15SLL2lvwTSRSKeVrhspzNf9V/5bJLRj6B8+N8ZQtreIpzI6Ecxj5g+XOUL45QsXOApU OErNSJyNkALwmpHJcdFBmxqKd+om/WfN4SNtOiDAihmDkhRzoQr9t75qEiXwu03rr2D3 nDRCWPwWGrgIoYHq86qK0iGC2dJ8x4jy1iv8pv1g/4RqGcyDgEXNYnqkz/7zSjYH8ntt NaxQ== X-Gm-Message-State: APjAAAWz3FQu2x0hH7qyLUImv/nLxguSADN0ftwAAo9TcaOb8wtRztsO LkP3AeGUGZQClGC4SGP/7VxkQwT6 X-Google-Smtp-Source: APXvYqyjRlIqY5LdVkLLuTl0T9lLFxaONVm6A+bhkGErIs9h4DIbuob5jMMgQ4dD3S05zEqjJHfKWA== X-Received: by 2002:a7b:c956:: with SMTP id i22mr19351758wml.65.1575392959594; Tue, 03 Dec 2019 09:09:19 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08e23.dynamic.kabel-deutschland.de. [188.192.142.35]) by smtp.gmail.com with ESMTPSA id x21sm3513630wmj.44.2019.12.03.09.09.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Dec 2019 09:09:18 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 3 Dec 2019 18:09:03 +0100 Message-Id: <20191203170910.5310-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/8] avformat/matroskadec: Check before allocations 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" That way one doesn't have to free later. In this case (concerning TTA extradata), this also fixes a memleak when the output samplerate is invalid. Signed-off-by: Andreas Rheinhardt --- libavformat/matroskadec.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index bd6f08575b..3ce1d399e9 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -2545,30 +2545,29 @@ static int matroska_parse_tracks(AVFormatContext *s) memcpy(&extradata[12], track->codec_priv.data, track->codec_priv.size); } else if (codec_id == AV_CODEC_ID_TTA) { - extradata_size = 30; - extradata = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); - if (!extradata) - return AVERROR(ENOMEM); - ffio_init_context(&b, extradata, extradata_size, 1, - NULL, NULL, NULL, NULL); - avio_write(&b, "TTA1", 4); - avio_wl16(&b, 1); if (track->audio.channels > UINT16_MAX || track->audio.bitdepth > UINT16_MAX) { av_log(matroska->ctx, AV_LOG_WARNING, "Too large audio channel number %"PRIu64 " or bitdepth %"PRIu64". Skipping track.\n", track->audio.channels, track->audio.bitdepth); - av_freep(&extradata); if (matroska->ctx->error_recognition & AV_EF_EXPLODE) return AVERROR_INVALIDDATA; else continue; } - avio_wl16(&b, track->audio.channels); - avio_wl16(&b, track->audio.bitdepth); if (track->audio.out_samplerate < 0 || track->audio.out_samplerate > INT_MAX) return AVERROR_INVALIDDATA; + extradata_size = 30; + extradata = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!extradata) + return AVERROR(ENOMEM); + ffio_init_context(&b, extradata, extradata_size, 1, + NULL, NULL, NULL, NULL); + avio_write(&b, "TTA1", 4); + avio_wl16(&b, 1); + avio_wl16(&b, track->audio.channels); + avio_wl16(&b, track->audio.bitdepth); avio_wl32(&b, track->audio.out_samplerate); avio_wl32(&b, av_rescale((matroska->duration * matroska->time_scale), track->audio.out_samplerate,