From patchwork Sat May 2 17:16:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 19439 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 1567444BB45 for ; Sat, 2 May 2020 20:17:58 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id F3DA968C89B; Sat, 2 May 2020 20:17:57 +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 856D968C882 for ; Sat, 2 May 2020 20:17:50 +0300 (EEST) Received: by mail-wm1-f67.google.com with SMTP id x4so3547072wmj.1 for ; Sat, 02 May 2020 10:17:50 -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=uDjrdozDAZxvu6n3VMag7bylAsHWpKBscBWzCEF2M8A=; b=osUgxZ+hfY/6z2JEJU+2KqIp613TqbmSSRwouqCbJozm4U5FRzs2FNGKONDJ0/Dl3p Ng+ldrazR4V0ZrIkJ2Yj3MiES5H0E84kG032g192egb6P71n6KqbNe04Ef9z9/SZ7fEF 3bT+CE2yaQ4dSSy76ZdkvFhF2ii8CJF83QMrRH9BtiCaBy7p2i6QXKiAXIlwNO+v+hRG IG+PEjkXPVqBzhMD0M6LFlhH4MbNYQ5GTm8O+opUEM3xmPvVuArE8c/7F0Bb8ThOFaqM JipbxY5X6j3+6bCVjbNMO9igIfORmWINUKK3bop2jovIxxx5ml7DCUb9zy+9tEUk3bRU lMGQ== 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=uDjrdozDAZxvu6n3VMag7bylAsHWpKBscBWzCEF2M8A=; b=Hy25O4SiBBEfM3iCl0oM42++HdtLzfJH2z4Gv25nVQhnj370Z1RokA6JIVCacSjWGs vX3rB2si38p9TZIamA1Sy3TC3Lf20jvkIueaf9lmO9BG6qwztKWjbHGsIbKF8hyRattq QbZmn69i2nCw/zGJbJIdFIMyhpHOicXOhl9ZaclqU8O7NzuNQPFpgfbehLsyCclTr1aY TjiW0EdL1Eqpi12tNNUr/Nj5XuI+WXw+70LtTvsDOVloUkHToma6FVfM6xqPNVMMDvnF eGxd0ihzZ7/X1nOqd+5OPFJabJUnX7BHEHLzs9xpaG8q0Cp8NUUnCoc39Tp4vS1HbjQF /6bw== X-Gm-Message-State: AGi0PubTkmUhj7njSWGZp0urgoA3ifHiC+QbPa9Z/bO3r7Ggezzyfm1T QmmzG26ebFtwlYlq+LWacLzk6OSo X-Google-Smtp-Source: APiQypICk+fJ7o/Y0JCewDr//T61yE1CpqRCnP2DrLaT8M2WyVTWRMxbN9uB+0xqNP/rwz9RCBVVLw== X-Received: by 2002:a1c:7c18:: with SMTP id x24mr5056238wmc.146.1588439869643; Sat, 02 May 2020 10:17:49 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id u74sm5241524wmu.13.2020.05.02.10.17.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 May 2020 10:17:49 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 2 May 2020 19:16:57 +0200 Message-Id: <20200502171700.28991-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200429222156.29129-1-andreas.rheinhardt@gmail.com> References: <20200429222156.29129-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/13] avformat/matroskaenc: Only write Cues at the front if space has been reserved 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" If the AVIOContext for output was unseekable when writing the header, no space for Cues would be reserved even if the reserve_index_space option was used (because it is reasonable to expect that one can't seek back to the beginning to write the Cues anyway). But if the AVIOContext was seekable when writing the trailer, it was presumed that space for the Cues had been reserved when the reserve_index_space option indicated so even when it was not. As a result, the beginning of the file would be overwritten. This commit fixes this: If the reserve_index_space option had been used and no space has been reserved in advance because of unseekability when writing the header, then no attempt to write Cues will be performed when writing the trailer; after all, writing them at the front is impossible and writing them at the end is probably undesired. Signed-off-by: Andreas Rheinhardt --- On git master one would get the segfault fixed in 7/13 when one runs into the issue fixed by this commit. libavformat/matroskaenc.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 10b64e2965..e9b2be3d42 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -1916,7 +1916,8 @@ static int mkv_write_header(AVFormatContext *s) if (mkv->reserve_cues_space == 1) mkv->reserve_cues_space++; put_ebml_void(pb, mkv->reserve_cues_space); - } + } else + mkv->reserve_cues_space = -1; } av_init_packet(&mkv->cur_audio_pkt); @@ -2457,7 +2458,7 @@ static int mkv_write_trailer(AVFormatContext *s) MatroskaMuxContext *mkv = s->priv_data; AVIOContext *pb = s->pb; int64_t endpos, ret64; - int ret; + int ret, ret2 = 0; // check if we have an audio packet cached if (mkv->cur_audio_pkt.size > 0) { @@ -2485,7 +2486,7 @@ static int mkv_write_trailer(AVFormatContext *s) endpos = avio_tell(pb); - if (mkv->cues.num_entries) { + if (mkv->cues.num_entries && mkv->reserve_cues_space >= 0) { AVIOContext *cues = NULL; uint64_t size; int length_size = 0; @@ -2510,7 +2511,7 @@ static int mkv_write_trailer(AVFormatContext *s) "Insufficient space reserved for Cues: " "%d < %"PRIu64". No Cues will be output.\n", mkv->reserve_cues_space, size); - mkv->reserve_cues_space = -1; + ret2 = AVERROR(EINVAL); ffio_free_dyn_buf(&cues); goto after_cues; } else { @@ -2600,7 +2601,7 @@ static int mkv_write_trailer(AVFormatContext *s) end_ebml_master(pb, mkv->segment); - return mkv->reserve_cues_space < 0 ? AVERROR(EINVAL) : 0; + return ret2; } static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)