From patchwork Wed Jan 1 00:58:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17102 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a05:6214:1865:0:0:0:0 with SMTP id eh5csp218808qvb; Tue, 31 Dec 2019 17:01:00 -0800 (PST) X-Google-Smtp-Source: APXvYqwTf3jkJp13Qh6kRkHYKUpqblrkuwr6VUnuIoQI73iK+2inMiGP+N41EOJnb62A+yC9emEE X-Received: by 2002:a17:906:aec7:: with SMTP id me7mr77452367ejb.81.1577840460737; Tue, 31 Dec 2019 17:01:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577840460; cv=none; d=google.com; s=arc-20160816; b=xjFEr2E29ouIX22YHdgwdAeJKledzoQkcD4CVzQl6uK2+L15H+71dO1rzMRH3T69Qd UJEWwsCtKs/acdo7rFsmPXIy8mNpjEoUfjoCauJHTFJupj5f82zemlYNq2M3rjR+M8ks MlXufkhXPeiaOd2RRo0bH66UlDL7eV512jI3CXjI0fFMdO6OmDRj5zjkru8ymDmgdnTO V0kSexcA9RpXcUwan4mN7/033xeJzmurxi7RJ1gMOxito+169GSjMQ1dVP+yRtxYpmgH hCojaxMRi5d8RGZu+zT3zwZpMOHZDPjJrEYyLH9QA5rCDBYF12Q6ePskpbJwhiFGbqzn iEHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=OBhHu+MWxAx8JD/RRTrLKkWfnsOwS05zO3C7Nm/Stp0=; b=IprhJHCLKdex+E61QIhtlilSvpQY/8fm2DkkiRkBvJ3WmI75tUqjORNhJM9W7RlJrU MTXwL2kRoALhQ43WvMjbvIdDaebHt1SLikxak4mHvpfLdupt49Ic9Hwq6VEhaLJUaIBO RDGXTdR4/6HqfPxxJ0PMT18XO3YzA2OdJdmteaCPKjGOs7tm72b4r8XsmoUKhAVSCroA NP9MkF76Z0XmtgCXrHiQAbe4rh69msG82600jsbOE5JH43Tw8vJVBa6cKJEgQfQztOLu rZsxKhl79UX7QogIIIYr6Tz6r6bR6xKfO3g2Zf2N8NVCVxbfq2MOXZ8u7+TfmLdZnuM5 BWoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=KTKqlQwK; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id w26si33789850edl.301.2019.12.31.17.01.00; Tue, 31 Dec 2019 17:01:00 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=KTKqlQwK; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9DE5A68ADF6; Wed, 1 Jan 2020 02:59:21 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 061A968AD24 for ; Wed, 1 Jan 2020 02:59:09 +0200 (EET) Received: by mail-wm1-f68.google.com with SMTP id p9so2833351wmc.2 for ; Tue, 31 Dec 2019 16:59:09 -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=zAjtu36LanZAVtelwcacU+QkpCRtYLcrwTgoYItYXF8=; b=KTKqlQwK/6s4il515xumylPkph+oZNhtVqMDxF7JKAPPH2hejg3Mtk6nrHT0vUjhq6 eHMLXg5ooQe3qdb1L3R02aG3pHUqbV6u/FgLkK3TKEeoXJMRnpC4SdkdRQIlZYkcLxtE 0av9MgnsaD26SJRYaOvLRlAF7uvTQmpgjcbq75fepIXfXCNaLakBAZ76NSL3rVkbQ+cu FhvNz2mR1INj+NurrYH/7wyFqXaooJqxEJOeXjhq6ewQ+fmBiHYl77HigqkZHVvwl8dZ 71VTBQ9kFP+Juk6msdNTcblXkHEPh6wjzORrDgVV82TfVoRDlpTRJ63qwXSJjly/I/WP /Psw== 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=zAjtu36LanZAVtelwcacU+QkpCRtYLcrwTgoYItYXF8=; b=GQBjyvMPSLsFXRKPInIHQJFL23vrcG/yFRanSot37xws9yFcAN0Oa2ytjK4ZeMwpou 8Mr616/IdJIGbD0S2PUBPSTnMa0/h+JH/JWJ9KXIelWEjWO5ucMWW2L/TG16f97NPDFm WvzfBWzq3VGoQ+SuGGiNoMakroeHLNBsEITtWVbpBcR2eIRGpvEjl0L+QFTMHZLZhfUK ummhyaAvcFnlogoOA0gMIKuTqNPVQERJ/vC1o5NTt7xgONDF7J6+qWXk8W1Lj0/uqcmI zHEZcf1nfC2jhQCfiWg5U4qrVFC+Tal4NAxoL3IsM+76eBtn/TLU+bvJ57R27ETjUMOg Fawg== X-Gm-Message-State: APjAAAUoVJFy6l0sUXYzww+1xukv+avI3CvYA9mv5lmIegJuFadACmw3 JhfCch7S/zoqz9+4OCa9AS4rSq6P X-Received: by 2002:a1c:16:: with SMTP id 22mr6660992wma.8.1577840348307; Tue, 31 Dec 2019 16:59:08 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id w17sm52431655wrt.89.2019.12.31.16.59.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Dec 2019 16:59:07 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 1 Jan 2020 01:58:26 +0100 Message-Id: <20200101005837.11356-10-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200101005837.11356-1-andreas.rheinhardt@gmail.com> References: <20200101005837.11356-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/20] avformat/matroskaenc: Check for failure when writing SeekHead 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" X-TUID: Z4Ih6ImAubPM Content-Length: 5881 mkv_write_seekhead() would up until now try to seek to the position where the SeekHead ought to be written, write the SeekHead and seek back. The first of these seeks was checked as was writing, yet the seek back was unchecked. Moreover the return value of mkv_write_seekhead() was unchecked (the ordinary return value was the position where the SeekHead was written). This commit changes this: Everything is checked. In the unseekable case (where the first seek may nevertheless work when it happens in the buffer) a failure at the first seek is not considered an error. In any case, failure to seek back is an error. Signed-off-by: Andreas Rheinhardt --- libavformat/matroskaenc.c | 52 ++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index cecf7589e5..c7948fb643 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -436,31 +436,35 @@ static void mkv_add_seekhead_entry(MatroskaMuxContext *mkv, uint32_t elementid, } /** - * Write the SeekHead to the file at the location reserved for it. + * Write the SeekHead to the file at the location reserved for it + * and seek back to the initial position. When error_on_seek_failure + * is not set, failure to seek to the position designated for the + * SeekHead is not considered an error; failure to seek back afterwards + * is always an error. * - * @return The file offset where the seekhead was written, - * -1 if an error occurred. + * @return 0 on success, < 0 on error. */ -static int64_t mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv) +static int mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv, + int error_on_seek_failure) { AVIOContext *dyn_cp; mkv_seekhead *seekhead = &mkv->seekhead; ebml_master seekentry; - int64_t currentpos, remaining; - int i; + int64_t currentpos, remaining, ret64; + int i, ret; currentpos = avio_tell(pb); - if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) { - currentpos = -1; - goto fail; - } - - if (start_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_SEEKHEAD) < 0) { - currentpos = -1; - goto fail; + if ((ret64 = avio_seek(pb, seekhead->filepos, SEEK_SET)) < 0) { + if (error_on_seek_failure) + return ret64; + goto seek_back; } + ret = start_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_SEEKHEAD); + if (ret < 0) + return ret; + for (i = 0; i < seekhead->num_entries; i++) { mkv_seekhead_entry *entry = &seekhead->entries[i]; @@ -477,13 +481,12 @@ static int64_t mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv) remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb); put_ebml_void(pb, remaining); - avio_seek(pb, currentpos, SEEK_SET); - currentpos = seekhead->filepos; +seek_back: + if ((ret64 = avio_seek(pb, currentpos, SEEK_SET)) < 0) + return ret64; -fail: - - return currentpos; + return 0; } static mkv_cues *mkv_start_cues(int64_t segment_offset) @@ -1925,8 +1928,11 @@ static int mkv_write_header(AVFormatContext *s) if (ret < 0) return ret; - if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live) - mkv_write_seekhead(pb, mkv); + if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live) { + ret = mkv_write_seekhead(pb, mkv, 0); + if (ret < 0) + return ret; + } mkv->cues = mkv_start_cues(mkv->segment_offset); if (!mkv->cues) { @@ -2527,7 +2533,9 @@ static int mkv_write_trailer(AVFormatContext *s) mkv_add_seekhead_entry(mkv, MATROSKA_ID_CUES, cuespos); } - mkv_write_seekhead(pb, mkv); + ret = mkv_write_seekhead(pb, mkv, 1); + if (ret < 0) + return ret; // update the duration av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);