From patchwork Thu Jun 21 16:58:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karsten Otto X-Patchwork-Id: 9465 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:115:0:0:0:0:0 with SMTP id c21-v6csp62876jad; Thu, 21 Jun 2018 09:58:45 -0700 (PDT) X-Google-Smtp-Source: ADUXVKI82N2ptZ4QkKGztisOZnAMbthpM9dh9xLLmzhc2bJEOFUp1Le9Ir+HkjaMHtS8oZm4Becc X-Received: by 2002:a1c:5b4e:: with SMTP id p75-v6mr6436246wmb.3.1529600325740; Thu, 21 Jun 2018 09:58:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529600325; cv=none; d=google.com; s=arc-20160816; b=AqZkCRre4jKgZcnaDbcYumNus94U/swQNNWioBGCBddk2DDCaMpRI4DouXKs6vCS8K ktlMbpyAyl7OEqZIc7/h2+im2fkLQTcSXP+xCPU1entL07vpj46mVXCYc4BT7gtclKir 5/Sca/fZ8M30JG6eZhlKy6jgnjIRjxxw7nWbj3AsOATktBF+1vMgxznTH221yyGNSDa6 N/ye/ZVz3DRlFWQD19GjmxL5HuMzfmzyD2WZH4vWWt8MiPmC0gWUiNRCkzUyQQUuw6fe fUNdZhcpyyvnmxRiXjS4NP/pEJlnUyEOnULKrYSqFnQJ0PTlCFuItdPIGRDVPr45/YDn pXyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=/EI6yxh380WMUb9Bk0dzbJKvwvGMA6nBy3LVgiZYbqI=; b=qi5pSBRbQn5gr26QLcwgdSMK8ELweA5yUzZsQ+JYu/LorRMI6rmBc+8amIhSYaVxpC Dx/sXWrb1xs9iZO19t6sAT+QEiQHfx37MMy6DGKtfn1rXkx9ayWNPfNf1Uj4ooRGqfuo WsUIS8KDBbJKqgGC0kRnmhoxtCzs+vO1KZb1ZJ0iA1DHWhqAaRUu3eMIi20reQ+5CceG GmPFKLmC/qA3OZuH3/1+dJhq1kiwCsVFev8njcoHkmNQeAQ8y4M5kGy+8Vd+Urol8J9r i1zHHiqp6tnpN1TXHGnlYI4rBqsUiazC5AHCic6dKCvrhJGvtBwjHQmQXZ4O3XU85In7 J4Vg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@posteo.de header.s=2017 header.b=b+lQnKJY; 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=NONE dis=NONE) header.from=posteo.de Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id n9-v6si2858386wrf.70.2018.06.21.09.58.43; Thu, 21 Jun 2018 09:58:45 -0700 (PDT) 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=@posteo.de header.s=2017 header.b=b+lQnKJY; 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=NONE dis=NONE) header.from=posteo.de Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B281D68A648; Thu, 21 Jun 2018 19:57:41 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6BB4868A56A for ; Thu, 21 Jun 2018 19:57:34 +0300 (EEST) Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id 93FFC20EC5 for ; Thu, 21 Jun 2018 18:58:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1529600307; bh=oGCkw/YWB4jBOISoBqwXJbFo3o2HHqTEfob+9PdK/xg=; h=From:To:Subject:Date:From; b=b+lQnKJY6lBxritMSO++hebiXj9XGjSY+2D6GGHUxY7ngxRkibPzNRGF0cFOSn9kW PcHeqYuOs2EcoQyykYhKgMHBvbhwzSq+vR+1eh0hXAlhPNXnwT4nU51PoVaVbzsfF8 MJvxPlM01/feacinoUmJIt4/GpQL2cqZZbSC/lbTPPFSsWrApYFBdWo63573KdDfOH BAmi/JOcTA2AKoa0W5J7z4Xai0SjTCQrJwrARIxXVpZaCHddNLvaa6/X8tQBRTIYWa uvG00dOhIhpmCH6oRv1c134+gGZ6QZtX1qKAalJSwRib1K/0XQ0Y52QO/Td3FtYxGs /U76vBTyhmrpA== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 41BSZW17QXz10VY for ; Thu, 21 Jun 2018 18:58:27 +0200 (CEST) From: Karsten Otto To: ffmpeg-devel@ffmpeg.org Date: Thu, 21 Jun 2018 18:58:24 +0200 Message-Id: <20180621165826.1714-2-ottoka@posteo.de> X-Mailer: git-send-email 2.14.3 (Apple Git-98) In-Reply-To: <20180621165826.1714-1-ottoka@posteo.de> References: <20180621165826.1714-1-ottoka@posteo.de> Subject: [FFmpeg-devel] [PATCH v3 1/3] aadec: improve eof detection 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Remember the end position of audio content in the file and check it during read_packet. There always seems to be other data beyond it, which could be misinterpreted as more audio. Also add some extra avio_read error checks, to bail early in case of a broken/truncated file. --- libavformat/aadec.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/libavformat/aadec.c b/libavformat/aadec.c index 8d39b1d9ba..4db71b1939 100644 --- a/libavformat/aadec.c +++ b/libavformat/aadec.c @@ -46,6 +46,7 @@ typedef struct AADemuxContext { struct AVTEA *tea_ctx; uint8_t file_key[16]; int64_t current_chapter_size; + int64_t content_end; } AADemuxContext; static int get_second_size(char *codec_name) @@ -197,6 +198,7 @@ static int aa_read_header(AVFormatContext *s) } start = TOC[largest_idx].offset; avio_seek(pb, start, SEEK_SET); + c->content_end = start + largest_size; c->current_chapter_size = 0; return 0; @@ -214,6 +216,11 @@ static int aa_read_packet(AVFormatContext *s, AVPacket *pkt) int ret; AADemuxContext *c = s->priv_data; + // are we at the end of the audio content? + if (avio_tell(s->pb) >= c->content_end) { + return AVERROR_EOF; + } + // are we at the start of a chapter? if (c->current_chapter_size == 0) { c->current_chapter_size = avio_rb32(s->pb); @@ -234,7 +241,9 @@ static int aa_read_packet(AVFormatContext *s, AVPacket *pkt) // decrypt c->current_codec_second_size bytes blocks = c->current_codec_second_size / TEA_BLOCK_SIZE; for (i = 0; i < blocks; i++) { - avio_read(s->pb, src, TEA_BLOCK_SIZE); + ret = avio_read(s->pb, src, TEA_BLOCK_SIZE); + if (ret != TEA_BLOCK_SIZE) + return (ret < 0) ? ret : AVERROR_EOF; av_tea_init(c->tea_ctx, c->file_key, 16); av_tea_crypt(c->tea_ctx, dst, src, 1, NULL, 1); memcpy(buf + written, dst, TEA_BLOCK_SIZE); @@ -242,7 +251,9 @@ static int aa_read_packet(AVFormatContext *s, AVPacket *pkt) } trailing_bytes = c->current_codec_second_size % TEA_BLOCK_SIZE; if (trailing_bytes != 0) { // trailing bytes are left unencrypted! - avio_read(s->pb, src, trailing_bytes); + ret = avio_read(s->pb, src, trailing_bytes); + if (ret != trailing_bytes) + return (ret < 0) ? ret : AVERROR_EOF; memcpy(buf + written, src, trailing_bytes); written = written + trailing_bytes; }