From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 9EB1246DEC for ; Thu, 13 Jul 2023 10:58:42 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4342C68C6B5; Thu, 13 Jul 2023 13:57:55 +0300 (EEST) Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CEBDE68C646 for ; Thu, 13 Jul 2023 13:57:49 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 8F904240591 for ; Thu, 13 Jul 2023 12:57:49 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id Jpj2K7RrDXdg for ; Thu, 13 Jul 2023 12:57:48 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id A90AF240705 for ; Thu, 13 Jul 2023 12:57:41 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 574EC3A12EC for ; Thu, 13 Jul 2023 12:57:35 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 13 Jul 2023 12:55:29 +0200 Message-Id: <20230713105553.21052-9-anton@khirnov.net> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230713105553.21052-1-anton@khirnov.net> References: <20230713105553.21052-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/33] fftools/ffmpeg_mux_init: improve error handling in of_add_attachments() 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: * return error codes instead of aborting * avoid leaking the AVIOContext on failure * check the return code of avio_read() --- fftools/ffmpeg_mux_init.c | 44 +++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index dbc58abea8..47b6f826b6 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -1514,7 +1514,7 @@ loop_end: } } -static void of_add_attachments(Muxer *mux, const OptionsContext *o) +static int of_add_attachments(Muxer *mux, const OptionsContext *o) { OutputStream *ost; int err; @@ -1528,20 +1528,42 @@ static void of_add_attachments(Muxer *mux, const OptionsContext *o) if ((err = avio_open2(&pb, o->attachments[i], AVIO_FLAG_READ, &int_cb, NULL)) < 0) { av_log(mux, AV_LOG_FATAL, "Could not open attachment file %s.\n", o->attachments[i]); - exit_program(1); + return err; } if ((len = avio_size(pb)) <= 0) { av_log(mux, AV_LOG_FATAL, "Could not get size of the attachment %s.\n", o->attachments[i]); - exit_program(1); + err = len ? len : AVERROR_INVALIDDATA; + goto read_fail; } - if (len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE || - !(attachment = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) { + if (len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) { av_log(mux, AV_LOG_FATAL, "Attachment %s too large.\n", o->attachments[i]); - exit_program(1); + err = AVERROR(ERANGE); + goto read_fail; } - avio_read(pb, attachment, len); + + attachment = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE); + if (!attachment) { + err = AVERROR(ENOMEM); + goto read_fail; + } + + err = avio_read(pb, attachment, len); + if (err < 0) + av_log(mux, AV_LOG_FATAL, "Error reading attachment file %s: %s\n", + o->attachments[i], av_err2str(err)); + else if (err != len) { + av_log(mux, AV_LOG_FATAL, "Could not read all %"PRId64" bytes for " + "attachment file %s\n", len, o->attachments[i]); + err = AVERROR(EIO); + } + +read_fail: + avio_closep(&pb); + if (err < 0) + return err; + memset(attachment + len, 0, AV_INPUT_BUFFER_PADDING_SIZE); av_log(mux, AV_LOG_VERBOSE, "Creating attachment stream from file %s\n", @@ -1554,8 +1576,9 @@ static void of_add_attachments(Muxer *mux, const OptionsContext *o) p = strrchr(o->attachments[i], '/'); av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE); - avio_closep(&pb); } + + return 0; } static int create_streams(Muxer *mux, const OptionsContext *o) @@ -1565,6 +1588,7 @@ static int create_streams(Muxer *mux, const OptionsContext *o) int auto_disable_a = o->audio_disable; int auto_disable_s = o->subtitle_disable; int auto_disable_d = o->data_disable; + int ret; /* create streams for all unlabeled output pads */ for (int i = 0; i < nb_filtergraphs; i++) { @@ -1611,7 +1635,9 @@ static int create_streams(Muxer *mux, const OptionsContext *o) map_manual(mux, o, &o->stream_maps[i]); } - of_add_attachments(mux, o); + ret = of_add_attachments(mux, o); + if (ret < 0) + return ret; if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) { av_dump_format(oc, nb_output_files - 1, oc->url, 1); -- 2.40.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".