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 E3CBE4633B for ; Sat, 15 Jul 2023 10:48:08 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 63CA468C655; Sat, 15 Jul 2023 13:46:45 +0300 (EEST) Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 960EB68C655 for ; Sat, 15 Jul 2023 13:46:38 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 460F52404EE for ; Sat, 15 Jul 2023 12:46:38 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 1rnS5zzQLxj1 for ; Sat, 15 Jul 2023 12:46:35 +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 E6499240D03 for ; Sat, 15 Jul 2023 12:46:26 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 2D2B83A20DF for ; Sat, 15 Jul 2023 12:46:21 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 15 Jul 2023 12:45:50 +0200 Message-Id: <20230715104611.17902-26-anton@khirnov.net> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230715104611.17902-1-anton@khirnov.net> References: <20230715104611.17902-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 26/47] fftools/cmdutils: add error handling to allocate_array_elem() 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: --- fftools/cmdutils.c | 2 +- fftools/cmdutils.h | 3 +-- fftools/ffmpeg_demux.c | 16 ++++++++++++++-- fftools/ffmpeg_filter.c | 28 +++++++++++++++++++++------- fftools/ffmpeg_mux_init.c | 16 ++++++++++++++-- 5 files changed, 51 insertions(+), 14 deletions(-) diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c index e2fa08c116..6c627ee815 100644 --- a/fftools/cmdutils.c +++ b/fftools/cmdutils.c @@ -1002,7 +1002,7 @@ void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems) if (!(new_elem = av_mallocz(elem_size)) || av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0) - report_and_exit(AVERROR(ENOMEM)); + return NULL; return new_elem; } diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h index decd23040f..6b9d7f80ae 100644 --- a/fftools/cmdutils.h +++ b/fftools/cmdutils.h @@ -430,13 +430,12 @@ void *grow_array(void *array, int elem_size, int *size, int new_size); * Atomically add a new element to an array of pointers, i.e. allocate * a new entry, reallocate the array of pointers and make the new last * member of this array point to the newly allocated buffer. - * Calls exit() on failure. * * @param array array of pointers to reallocate * @param elem_size size of the new element to allocate * @param nb_elems pointer to the number of elements of the array array; * *nb_elems will be incremented by one by this function. - * @return pointer to the newly allocated entry + * @return pointer to the newly allocated entry or NULL on failure */ void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems); diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c index a43c39b843..72b94ea44f 100644 --- a/fftools/ffmpeg_demux.c +++ b/fftools/ffmpeg_demux.c @@ -1018,8 +1018,11 @@ static DemuxStream *demux_stream_alloc(Demuxer *d, AVStream *st) { const char *type_str = av_get_media_type_string(st->codecpar->codec_type); InputFile *f = &d->f; - DemuxStream *ds = allocate_array_elem(&f->streams, sizeof(*ds), - &f->nb_streams); + DemuxStream *ds; + + ds = allocate_array_elem(&f->streams, sizeof(*ds), &f->nb_streams); + if (!ds) + return NULL; ds->ist.st = st; ds->ist.file_index = f->index; @@ -1051,6 +1054,9 @@ static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st) int ret; ds = demux_stream_alloc(d, st); + if (!ds) + return AVERROR(ENOMEM); + ist = &ds->ist; ist->discard = 1; @@ -1328,6 +1334,9 @@ static Demuxer *demux_alloc(void) { Demuxer *d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files); + if (!d) + return NULL; + d->f.class = &input_file_class; d->f.index = nb_input_files - 1; @@ -1358,6 +1367,9 @@ int ifile_open(const OptionsContext *o, const char *filename) int64_t recording_time = o->recording_time; d = demux_alloc(); + if (!d) + return AVERROR(ENOMEM); + f = &d->f; if (stop_time != INT64_MAX && recording_time != INT64_MAX) { diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index 26aff9c328..880d883064 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -550,8 +550,10 @@ static OutputFilter *ofilter_alloc(FilterGraph *fg) OutputFilterPriv *ofp; OutputFilter *ofilter; - ofp = allocate_array_elem(&fg->outputs, sizeof(*ofp), - &fg->nb_outputs); + ofp = allocate_array_elem(&fg->outputs, sizeof(*ofp), &fg->nb_outputs); + if (!ofp) + return NULL; + ofilter = &ofp->ofilter; ofilter->graph = fg; ofp->format = -1; @@ -715,10 +717,14 @@ int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost) static InputFilter *ifilter_alloc(FilterGraph *fg) { - InputFilterPriv *ifp = allocate_array_elem(&fg->inputs, sizeof(*ifp), - &fg->nb_inputs); - InputFilter *ifilter = &ifp->ifilter; + InputFilterPriv *ifp; + InputFilter *ifilter; + ifp = allocate_array_elem(&fg->inputs, sizeof(*ifp), &fg->nb_inputs); + if (!ifp) + return NULL; + + ifilter = &ifp->ifilter; ifilter->graph = fg; ifp->frame = av_frame_alloc(); @@ -800,13 +806,18 @@ static const AVClass fg_class = { int fg_create(FilterGraph **pfg, char *graph_desc) { - FilterGraphPriv *fgp = allocate_array_elem(&filtergraphs, sizeof(*fgp), &nb_filtergraphs); - FilterGraph *fg = &fgp->fg; + FilterGraphPriv *fgp; + FilterGraph *fg; AVFilterInOut *inputs, *outputs; AVFilterGraph *graph; int ret = 0; + fgp = allocate_array_elem(&filtergraphs, sizeof(*fgp), &nb_filtergraphs); + if (!fgp) + return AVERROR(ENOMEM); + fg = &fgp->fg; + if (pfg) *pfg = fg; @@ -851,6 +862,9 @@ int fg_create(FilterGraph **pfg, char *graph_desc) for (AVFilterInOut *cur = outputs; cur; cur = cur->next) { OutputFilter *const ofilter = ofilter_alloc(fg); + if (!ofilter) + goto fail; + ofilter->linklabel = cur->name; cur->name = NULL; diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index ac4ef328a6..e714d6cc70 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -414,8 +414,11 @@ static const AVClass output_stream_class = { static MuxStream *mux_stream_alloc(Muxer *mux, enum AVMediaType type) { const char *type_str = av_get_media_type_string(type); - MuxStream *ms = allocate_array_elem(&mux->of.streams, sizeof(*ms), - &mux->of.nb_streams); + MuxStream *ms; + + ms = allocate_array_elem(&mux->of.streams, sizeof(*ms), &mux->of.nb_streams); + if (!ms) + return NULL; ms->ost.file_index = mux->of.index; ms->ost.index = mux->of.nb_streams - 1; @@ -1112,6 +1115,9 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, return AVERROR(ENOMEM); ms = mux_stream_alloc(mux, type); + if (!ms) + return AVERROR(ENOMEM); + ost = &ms->ost; if (o->streamid) { @@ -2568,6 +2574,9 @@ static Muxer *mux_alloc(void) { Muxer *mux = allocate_array_elem(&output_files, sizeof(*mux), &nb_output_files); + if (!mux) + return NULL; + mux->of.class = &output_file_class; mux->of.index = nb_output_files - 1; @@ -2587,6 +2596,9 @@ int of_open(const OptionsContext *o, const char *filename) int64_t stop_time = o->stop_time; mux = mux_alloc(); + if (!mux) + return AVERROR(ENOMEM); + of = &mux->of; if (stop_time != INT64_MAX && recording_time != INT64_MAX) { -- 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".