From patchwork Sat Mar 23 02:06:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 47351 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:1014:b0:1a3:ac71:9534 with SMTP id nk20csp285741pzb; Fri, 22 Mar 2024 19:06:51 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWfp8spG8ubB0MkMdNKIbWRT5atOhEARGu5MHYRSUJ2p++ZKqPEz028uwZ21Ab28UCs8r9llePa5AqHjN3jFuMHqB8q3zwB4ZM8vA== X-Google-Smtp-Source: AGHT+IEQXniwXWJrwVELgPZCDwVL046N6oZfyLOHNNxkLViZadTpCZIkaeNERk78o5sv0u1Jt740 X-Received: by 2002:a05:6512:456:b0:513:2b35:2520 with SMTP id y22-20020a056512045600b005132b352520mr648871lfk.58.1711159611169; Fri, 22 Mar 2024 19:06:51 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 7-20020a170906328700b00a46e35d488csi376924ejw.306.2024.03.22.19.06.50; Fri, 22 Mar 2024 19:06:51 -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=@outlook.com header.s=selector1 header.b=E6QhOqju; arc=fail (body hash mismatch); 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=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 462BD68D5A8; Sat, 23 Mar 2024 04:06:45 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04olkn2050.outbound.protection.outlook.com [40.92.74.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F241368D022 for ; Sat, 23 Mar 2024 04:06:37 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=d9ao4BqhnOk8trazMQzciM+rieptkR9ziPwo0d8U4xGOegLQUH4yKlyl1JfI/2Tv+UBecAs9mpjEvIof7t9P23gpp9XMFViLY47puTJvlPl1lXgEaYr8AK1336hne/Me5CcOLN1/60ef41fZ/yFVPeTDzFdqWk6iX1FpfyrjMd1B9E0WxHSartslo9+Vv7JO9WY//YBURiui6Yu5TeLXjZMlpdAwFfK1uEs+SCao0UGS+Lg3+mmzdqAS0Lir0mXhnJtrwr4TpRC0+nHhhNS9I4SfOfTP7ipa/rwqj38Yk9vMp9IeSR+In4/HQoRqwVQN/cELxR/HaNgsa4ougUajJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=quB8mw2Ha/rVF/is7Rk96NNaKcMJsPrX/2QGnSV9tSA=; b=HyiGSshxO0n5GDBmTsWyte/BaCKqb0xElLm33v04CCVWLIr5qjpY8PIS1Nu2oJIF6z2cweCkiCb4ZRSd3Jcg8GO844xriNLLcRTznkpREGMMK+8nfSV4pb9d7ndSkYFM6ptsS89tK5+w1EgN+bAgZBN/P4WAHsahRZCuIZTudOESNX5QkSXn4roqQC7TRpTrjITt3cFp4H5k5AvpQ+5kkv/cVTqxnZVDwMUmweWFhsNpac+U03CWGeAoXomuSB2MADXZ3bHqTB/nWRMD9JQBNuf2Ohp8H7cdM+8pX5Lf0GLMBBEKnj/52bz5/sAjMeJZhgYiH7cakLxW9reFior3wg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=quB8mw2Ha/rVF/is7Rk96NNaKcMJsPrX/2QGnSV9tSA=; b=E6QhOqjusSTqtp32Y44MZSah9MEsX2gyIwhuzQrxg1MvrrcL9U+XB28TlpwHALBeDQ1cX64KgAi00kqiamXVNeB/pcZiqvOykkHeFj95SgSLQwEEJlw5V7+GSQ73syJceuLuCFNgPs8fYYUAR4lnvo3V4R4bKq9sJnxRosbv5WB8AiA3z43PAWwdTN4/hHPSBrUsDcdJoXHUn8Q/6pemc39K5vbPUAO6N451O6FqRD4In/KSxeV5vWedWsUDYgaJcDs5H5rT2NzV5o2zuB39d/O/B57LqktE/qTUb7PJ4Q7AtExIJsNbhs1yM+weSV4KAeqtt9Iuw6u2PaOYKQ0SaQ== Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) by AM8P250MB0137.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:320::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.24; Sat, 23 Mar 2024 02:06:33 +0000 Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::4a3b:cf76:cddc:c68d]) by GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::4a3b:cf76:cddc:c68d%4]) with mapi id 15.20.7386.025; Sat, 23 Mar 2024 02:06:33 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 23 Mar 2024 03:06:11 +0100 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: X-TMN: [Yn6bD57ir7Tq7RdGAXJgg14k5UeAJAVEZlQ1rWhYPEA=] X-ClientProxiedBy: ZR0P278CA0005.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:16::15) To GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) X-Microsoft-Original-Message-ID: <20240323020623.1570961-2-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV1P250MB0737:EE_|AM8P250MB0137:EE_ X-MS-Office365-Filtering-Correlation-Id: fe6e3c97-1f1f-4aa2-ddd3-08dc4adddc8a X-MS-Exchange-SLBlob-MailProps: Cq7lScuPrnqkjmXb1vlddM5aSa/Epk3Avcf2FPioCve9ssXv7eqtGJv4lSyo9jxOADghzn+eXjdYg9vJ9YWdTLHB1Au4oIu7OqoM0ZOVZwcXvmLuqaYVIiN84k6H+ZaITT7qcOWe71r+zNIej1V0pylPu+ZVtgF6Ked8CpCOaa7b7ceSUG5CuFyumJjnlocW2gb8Q5TxKBOVK1icM2JB5mWvPRWeLa6YujF8737wXsatXpvdueKu5paxvgT+0VvJwQdjLQ99QwW4qWUBGUZ5Ha6L77LVn7buWT7HgjwY2amFzfVLiCD0e+buBHnuFgIoSH3JO0J7dzwUDoW1wOKcbQPD9a6IUwD7EqcGM45f/yXgQja4m4/aB3UfXQojy2BeqIAvVPs25zQwTXpL+MxpypP+LbylyX7m5ryS4v9V4JqRf3EUa60S9mzISt1anzLN3bYhAp5FSUb6eG+UNIbyH2M32oy4Dy28MaB0377frL+uCHUsKPhRKbsFCYRv1WSVlUQxHPXPj8IIoRUmc2Ovq43IuhP1tZtVZhPjA6rc7hevv7Gvfq1BzVG5tFwuGQUWQrcb2TodNZALcDbA/mTXkbXPv3eUT60aPD28rP0Yg7I8e8kLpnONHpUbh0sX7cHjgVKKeNwlQSx9YSaDbmYcEQrVh1VRIFoCTPG0hqyJtTmXqL6QB54pY3di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ook.com X-MS-Exchange-CrossTenant-Network-Message-Id: fe6e3c97-1f1f-4aa2-ddd3-08dc4adddc8a X-MS-Exchange-CrossTenant-AuthSource: GV1P250MB0737.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Mar 2024 02:06:33.0271 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8P250MB0137 Subject: [FFmpeg-devel] [PATCH 03/15] avformat/fitsdec: Don't use AVBPrint for temporary storage X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: KtQej2VeHguJ Most of the data in the temporary storage ends up being returned to the user as AVPacket.data, so it makes sense to avoid using the AVBPrint for temporary storage altogether (in particular in light of the fact that the blocks read here are too big for the small-string optimization anyway) and read the data directly into AVPacket.data. This also avoids another memcpy() from a stack buffer to the AVBPrint in ts_image() (that could always have been avoided with av_bprint_get_buffer()). These changes also allow to use av_append_packet(), which greatly simplifies the code; furthermore, one can avoid cleanup code on error as the packet is already unreferenced generically on error. There are two user-visible changes from this patch: 1. Truncated packets are now marked as corrupt. 2. AVPacket.pos is set (it corresponds to the discarded header line, 80 bytes before the position corresponding to the actual packet data). Furthermore, this patch also removes code that triggered a -Wtautological-constant-out-of-range-compare warning from Clang (namely a comparison of an unsigned and INT64_MAX in an assert). Signed-off-by: Andreas Rheinhardt --- libavformat/fitsdec.c | 80 ++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 55 deletions(-) diff --git a/libavformat/fitsdec.c b/libavformat/fitsdec.c index fe2dd5ad5d..6771dda327 100644 --- a/libavformat/fitsdec.c +++ b/libavformat/fitsdec.c @@ -24,13 +24,10 @@ * FITS demuxer. */ -#include "libavutil/avassert.h" -#include "libavutil/intreadwrite.h" #include "demux.h" #include "internal.h" #include "libavutil/opt.h" #include "libavcodec/fits.h" -#include "libavutil/bprint.h" #define FITS_BLOCK_SIZE 2880 @@ -71,31 +68,31 @@ static int fits_read_header(AVFormatContext *s) * @param s pointer to AVFormat Context * @param fits pointer to FITSContext * @param header pointer to FITSHeader - * @param avbuf pointer to AVBPrint to store the header + * @param pkt pointer to AVPacket to store the header * @param data_size to store the size of data part - * @return 1 if image found, 0 if any other extension and AVERROR_INVALIDDATA otherwise + * @return 1 if image found, 0 if any other extension and AVERROR code otherwise */ -static int64_t is_image(AVFormatContext *s, FITSContext *fits, FITSHeader *header, - AVBPrint *avbuf, uint64_t *data_size) +static int is_image(AVFormatContext *s, FITSContext *fits, FITSHeader *header, + AVPacket *pkt, uint64_t *data_size) { int i, ret, image = 0; - char buf[FITS_BLOCK_SIZE] = { 0 }; - int64_t buf_size = 0, size = 0, t; + int64_t size = 0, t; do { - ret = avio_read(s->pb, buf, FITS_BLOCK_SIZE); + const uint8_t *buf, *buf_end; + ret = av_append_packet(s->pb, pkt, FITS_BLOCK_SIZE); if (ret < 0) { return ret; } else if (ret < FITS_BLOCK_SIZE) { return AVERROR_INVALIDDATA; } - av_bprint_append_data(avbuf, buf, FITS_BLOCK_SIZE); ret = 0; - buf_size = 0; - while(!ret && buf_size < FITS_BLOCK_SIZE) { - ret = avpriv_fits_header_parse_line(s, header, buf + buf_size, NULL); - buf_size += 80; + buf_end = pkt->data + pkt->size; + buf = buf_end - FITS_BLOCK_SIZE; + while(!ret && buf < buf_end) { + ret = avpriv_fits_header_parse_line(s, header, buf, NULL); + buf += 80; } } while (!ret); if (ret < 0) @@ -142,12 +139,10 @@ static int64_t is_image(AVFormatContext *s, FITSContext *fits, FITSHeader *heade static int fits_read_packet(AVFormatContext *s, AVPacket *pkt) { - int64_t pos, ret; uint64_t size; FITSContext *fits = s->priv_data; FITSHeader header; - AVBPrint avbuf; - char *buf; + int ret; if (fits->first_image) { avpriv_fits_header_init(&header, STATE_SIMPLE); @@ -155,57 +150,32 @@ static int fits_read_packet(AVFormatContext *s, AVPacket *pkt) avpriv_fits_header_init(&header, STATE_XTENSION); } - av_bprint_init(&avbuf, FITS_BLOCK_SIZE, AV_BPRINT_SIZE_UNLIMITED); - while ((ret = is_image(s, fits, &header, &avbuf, &size)) == 0) { - av_bprint_finalize(&avbuf, NULL); - pos = avio_skip(s->pb, size); + while ((ret = is_image(s, fits, &header, pkt, &size)) == 0) { + int64_t pos = avio_skip(s->pb, size); if (pos < 0) return pos; - av_bprint_init(&avbuf, FITS_BLOCK_SIZE, AV_BPRINT_SIZE_UNLIMITED); avpriv_fits_header_init(&header, STATE_XTENSION); + av_packet_unref(pkt); } if (ret < 0) - goto fail; - - if (!av_bprint_is_complete(&avbuf)) { - ret = AVERROR(ENOMEM); - goto fail; - } - - av_assert0(avbuf.len <= INT64_MAX && size <= INT64_MAX); - if (avbuf.len + size > INT_MAX - 80) { - ret = AVERROR_INVALIDDATA; - goto fail; - } - // Header is sent with the first line removed... - ret = av_new_packet(pkt, avbuf.len - 80 + size); - if (ret < 0) - goto fail; + return ret; pkt->stream_index = 0; - pkt->flags |= AV_PKT_FLAG_KEY; + pkt->flags |= AV_PKT_FLAG_KEY; + pkt->duration = 1; + // Header is sent with the first line removed... + pkt->data += 80; + pkt->size -= 80; - ret = av_bprint_finalize(&avbuf, &buf); - if (ret < 0) { - return ret; - } + if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - pkt->size) + return AVERROR(ERANGE); - memcpy(pkt->data, buf + 80, avbuf.len - 80); - pkt->size = avbuf.len - 80; - av_freep(&buf); - ret = avio_read(s->pb, pkt->data + pkt->size, size); + ret = av_append_packet(s->pb, pkt, size); if (ret < 0) return ret; - pkt->size += ret; - pkt->duration = 1; - return 0; - -fail: - av_bprint_finalize(&avbuf, NULL); - return ret; } static const AVOption fits_options[] = {