From patchwork Wed Dec 4 12:37:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16588 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 F1F9F449DB0 for ; Wed, 4 Dec 2019 14:37:43 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D88B968B1DE; Wed, 4 Dec 2019 14:37:43 +0200 (EET) 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 BF62C68B1A4 for ; Wed, 4 Dec 2019 14:37:37 +0200 (EET) Received: by mail-wr1-f68.google.com with SMTP id b6so8466264wrq.0 for ; Wed, 04 Dec 2019 04:37:37 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=Sxtaeza5gptXIEtubwNZr2PURzkB8dC2YVjCZXbOq60=; b=d3WoA8IOMGYK46imV9ofh0fywDv4vRQWfxTr01ljj2ucw5au9Q8PNJ5xeLLRqQvr5T OnKM+I6QzYq7U2HS6IrWu09jmhNFwQgkgDvun+wRU15i1WUZfCVSZBG8IGqm/NqKAK8O dQ8r6H7mL00GDRdj7PAUi26ScAjeBH0rwc4hjSBu+lZGhMZX0c1rwKsfKVA8/5cXsN5G +LKgS2MBaM/5djLGehgQXVvva3rZ3jKF6mIVSoQ4tGTcukM85R7l6y1M3+uYNvQ+6Ihy E7cm2sG91WcE/u1I2RTuFE7sl9GGaRP8F9Z7cMJnxXnQCfr6IWZu3LNqBPoIdMiOca4y ucqA== 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=Sxtaeza5gptXIEtubwNZr2PURzkB8dC2YVjCZXbOq60=; b=YcyZchdCICoC8ho8GR3dUHGgAoxHyeXgQ+N1ee+2AAllgdD8OIo9C2VS8TvpFnKd5o DL7d2O4xsnVOsyA8++urpgajCnKVk45j60lYquX9o3ggkjsWDe19PsHFG87voLv8Eppg IIUf9q84wRAKuYdnUUbxIhOWK07CD5D7Zdng7j8Tnze/WVFgEa1uFUocdF/H2AFfsASY kVJaZuCAXP4Ne1EVgu7wWuXIqQGeLOX75w01AaLi6utekjM3RxCNLDaNAQkOntO53g4U 0z1cERBX83Gte/bkAoEJUl63nLcF+RQCijXRM0oCw7WYCE48IofWxhBxWeNcPecpqLLT 73hA== X-Gm-Message-State: APjAAAXxRcxaca+fdgsvgnaIyetUyd+wM4S4rTt2XQDzh8l8+3BhH/ZV 4veKCdxipcWXFEWo89WlvYMuXXjx X-Google-Smtp-Source: APXvYqxty13rXRzCb1MSe4c/A52H5Ri9YT+YmzVqU7IsH4w+uammlbwwLU2keAPKy1utV/Tnb87nbw== X-Received: by 2002:a5d:404b:: with SMTP id w11mr4026528wrp.171.1575463057142; Wed, 04 Dec 2019 04:37:37 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08e23.dynamic.kabel-deutschland.de. [188.192.142.35]) by smtp.gmail.com with ESMTPSA id v15sm6295872wmh.24.2019.12.04.04.37.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 04:37:36 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 4 Dec 2019 13:37:13 +0100 Message-Id: <20191204123714.27765-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191204123714.27765-1-andreas.rheinhardt@gmail.com> References: <20191204123714.27765-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] avformat/mpeg: Don't copy or leak string in AVBPrint 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" vobsub_read_header() uses an AVBPrint to write a string and up until now, it collected the string stored in the AVBPrint via av_bprint_finalize(), which might involve an allocation and copy of the string. But this is unnecessary, as the lifetime of the returned string does not exceed the lifetime of the AVBPrint. So use the string in the AVBPrint directly. This also makes it possible to easily fix a memleak: In certain error situations, the string stored in the AVBPrint would not be freed (if it was dynamically allocated). This has been fixed, too. Signed-off-by: Andreas Rheinhardt --- Supersedes https://ffmpeg.org/pipermail/ffmpeg-devel/2019-October/252074.html Resending because of merge conflict. libavformat/mpeg.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c index 80983f8a81..e4fe16c7d2 100644 --- a/libavformat/mpeg.c +++ b/libavformat/mpeg.c @@ -718,7 +718,6 @@ static int vobsub_read_header(AVFormatContext *s) int i, ret = 0, header_parsed = 0, langidx = 0; VobSubDemuxContext *vobsub = s->priv_data; size_t fname_len; - char *header_str = NULL; AVBPrint header; int64_t delay = 0; AVStream *st = NULL; @@ -731,8 +730,7 @@ static int vobsub_read_header(AVFormatContext *s) char *ext; vobsub->sub_name = av_strdup(s->url); if (!vobsub->sub_name) { - ret = AVERROR(ENOMEM); - goto end; + return AVERROR(ENOMEM); } fname_len = strlen(vobsub->sub_name); @@ -740,24 +738,23 @@ static int vobsub_read_header(AVFormatContext *s) if (fname_len < 4 || *(ext - 1) != '.') { av_log(s, AV_LOG_ERROR, "The input index filename is too short " "to guess the associated .SUB file\n"); - ret = AVERROR_INVALIDDATA; - goto end; + return AVERROR_INVALIDDATA; } memcpy(ext, !strncmp(ext, "IDX", 3) ? "SUB" : "sub", 3); av_log(s, AV_LOG_VERBOSE, "IDX/SUB: %s -> %s\n", s->url, vobsub->sub_name); } if (!(iformat = av_find_input_format("mpeg"))) { - ret = AVERROR_DEMUXER_NOT_FOUND; - goto end; + return AVERROR_DEMUXER_NOT_FOUND; } vobsub->sub_ctx = avformat_alloc_context(); if (!vobsub->sub_ctx) { - ret = AVERROR(ENOMEM); - goto end; + return AVERROR(ENOMEM); } + av_bprint_init(&header, 0, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE); + if ((ret = ff_copy_whiteblacklists(vobsub->sub_ctx, s)) < 0) goto end; @@ -767,7 +764,6 @@ static int vobsub_read_header(AVFormatContext *s) goto end; } - av_bprint_init(&header, 0, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE); while (!avio_feof(s->pb)) { char line[MAX_LINE_SIZE]; int len = ff_get_line(s->pb, line, sizeof(line)); @@ -888,22 +884,20 @@ static int vobsub_read_header(AVFormatContext *s) } if (!av_bprint_is_complete(&header)) { - av_bprint_finalize(&header, NULL); ret = AVERROR(ENOMEM); goto end; } - av_bprint_finalize(&header, &header_str); for (i = 0; i < s->nb_streams; i++) { AVCodecParameters *par = s->streams[i]->codecpar; ret = ff_alloc_extradata(par, header.len); if (ret < 0) { goto end; } - memcpy(par->extradata, header_str, header.len); + memcpy(par->extradata, header.str, header.len); } end: - av_free(header_str); + av_bprint_finalize(&header, NULL); return ret; }