From patchwork Thu May 16 22:30:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 13142 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 CDBA644964F for ; Fri, 17 May 2019 01:43:28 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C155468A405; Fri, 17 May 2019 01:43:28 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DF5EA689C5C for ; Fri, 17 May 2019 01:43:24 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id d18so5063481wrs.5 for ; Thu, 16 May 2019 15:43:24 -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=ZggN52FWamKz0W6shDRBaC7daOOjI8OD2Tltp++c2gU=; b=ETMXaMcB6jmXiY5HqEtpAy52k19GK4EE1L5zIzIE1Sld8jm5ERyeTRk9Yscp3E6gsa ZarR42VojEJ/aR3OFZRBx89NdDjRJ7epuaNG0wX2fzwXuveGbaZgrjxjdr6qoq2ikopf +VmiHlfhuddeuFDQVY9W2vl6vGitjfUvdSZy8QJyJKiufSSpNicVAKzqvQpzeeaIsjFu 4EEluYuSsPj+VojQ1urm5jW46epyjWrLVStKfbVhwV9Fb5/qWU5JpPK9f9qQdhQpB2Tr BxinKh1UzXWR2JwzUjDDE3vA/gkMluKorSj4iv9DUBt4iaubspMRArT0XR+kOUvuhMNe HE1Q== 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=ZggN52FWamKz0W6shDRBaC7daOOjI8OD2Tltp++c2gU=; b=lAnlkVQMUSmSwYNesnrU+wdKtVYDHXVR8J/Orcp/Wpx8pwQXQtOLAo8EJpBsZsr5cQ YwhooDyqI4jX5zZZxt04WLDtqB6KCY0IzopYDBB2jnnLAOrVGNQ9zzexuVyP2XOk3qk9 6XnqwZmVVPUAK0qQEXGsWwgQmgLP/4GrqKED0NMiy7yKlHI8RHh3RF/5Jy2pvuMyr6gL /j4U3UVaQ0eNJOwY+HToabKhXwDb6/CgZoSWqTNmU9LPittj2Ul5wn5PjgT0DdBTWDc+ 6oFaNDJPx7e5nc4zMdW7eu1PkAUP3AfhjLeDX1Mfib2vn6Uqf+Bx86+H2Odyhyi8KoSU JAKw== X-Gm-Message-State: APjAAAXj6IW+vonVKlcH0ZD4cmciVhUfmfqT27oupYtB0m4xVIgMUsqg 9nn6odZRff13n4aKTV23Hfiurh3h X-Google-Smtp-Source: APXvYqysdTOyZyMx2mtfRM4aejGbELZP0MCA5MSvKB8liDp9EtNgBvZLED1tvMPEPd/tdm9r01pnBg== X-Received: by 2002:adf:ec0f:: with SMTP id x15mr30967749wrn.120.1558046604320; Thu, 16 May 2019 15:43:24 -0700 (PDT) Received: from localhost.localdomain (ipbcc18715.dynamic.kabel-deutschland.de. [188.193.135.21]) by smtp.gmail.com with ESMTPSA id i185sm11168725wmg.32.2019.05.16.15.43.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 May 2019 15:43:23 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 17 May 2019 00:30:11 +0200 Message-Id: <20190516223018.30827-28-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190516223018.30827-1-andreas.rheinhardt@gmail.com> References: <20190516223018.30827-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 27/37] avformat/matroskadec: Redo EOF handling 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" This commit closes the last hole in the system of checks for a known-length file ending too early: Now an error message is emitted in case the file ends directly after an EBML element. Furthermore, this commit adds a check and a corresponding warning whether there is data beyond the Matroska segment (only reasonable for known-length segments). If everything looks alright, then parsing is stopped as soon as EOF is reached (in contrast, the earlier code would always call matroska_resync at the end). Signed-off-by: Andreas Rheinhardt --- libavformat/matroskadec.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index fdd6c4b86b..f769543b4a 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -1183,10 +1183,15 @@ static int ebml_parse(MatroskaDemuxContext *matroska, if (matroska->is_live) // in live mode, finish parsing if EOF is reached. return 1; - if (level && level->length == EBML_UNKNOWN_LENGTH && pos == avio_tell(pb)) { - // Unknown-length levels automatically end at EOF. - matroska->num_levels--; - return LEVEL_ENDED; + if (level && pos == avio_tell(pb)) { + if (level->length == EBML_UNKNOWN_LENGTH) { + // Unknown-length levels automatically end at EOF. + matroska->num_levels--; + return LEVEL_ENDED; + } else { + av_log(matroska->ctx, AV_LOG_ERROR, "File ended prematurely " + "at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos); + } } } return res; @@ -3614,6 +3619,14 @@ static int matroska_parse_cluster(MatroskaDemuxContext *matroska) ebml_free(matroska_blockgroup, block); memset(block, 0, sizeof(*block)); } else if (!matroska->num_levels) { + if (!avio_feof(matroska->ctx->pb)) { + avio_r8(matroska->ctx->pb); + if (!avio_feof(matroska->ctx->pb)) { + av_log(matroska->ctx, AV_LOG_WARNING, "File extends beyond " + "end of segment.\n"); + return AVERROR_INVALIDDATA; + } + } matroska->done = 1; return AVERROR_EOF; } @@ -3634,7 +3647,7 @@ static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt) while (matroska_deliver_packet(matroska, pkt)) { if (matroska->done) return (ret < 0) ? ret : AVERROR_EOF; - if (matroska_parse_cluster(matroska) < 0) + if (matroska_parse_cluster(matroska) < 0 && !matroska->done) ret = matroska_resync(matroska, matroska->resync_pos); }