From patchwork Sat Jul 15 10:45:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 42701 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp4239517pzb; Sat, 15 Jul 2023 03:48:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlGYDHt/INFgTrqLX+jT/nw/y5an9kp2wOUURzuKSgoVQS+VHIhYVreQbYLcdIeDrUNXbWi4 X-Received: by 2002:a05:6402:2037:b0:51d:9ddf:f0f3 with SMTP id ay23-20020a056402203700b0051d9ddff0f3mr6078358edb.31.1689418091058; Sat, 15 Jul 2023 03:48:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689418091; cv=none; d=google.com; s=arc-20160816; b=SArwFfN6jfYxqDp8Dqx37U2wbXo+4nOxP+a4EH03h8lyaJCK+cPliJtvId7gACoqPl uSkvuqJwMSiKNdTYGAMgDbhvbBt1FoLoZgV/7T7o0X6Qa5vqR7DuRvVF5sgdnp/d+ikY xOSo0rCSQbZxxQuE5lPA1zbO1SV/no+mYP6iHQSdT26aQ/+lSW2Kb+UNGq0VCmqJS4JJ i//SnoNZLb2pA3sNcY1JX3/OAO5LpM4v/InpBx+g+fKVyZrvSbyyoThTQWZdJmcRhsjP RH0E/SZ6PIlbPnZO2GWmA5LLLp4EVskTapsWzb8b2wI+ysOelvDZWYBlJ/rqyCOQ2pCf GN3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=Tb8bYR2o532dud7M2uaTcsrHSvGdPLGh75xld/htkq8=; fh=8ZF2QtiKOjBtyn9wuO2cMeZaLsE4aq02Bq77gGK61E8=; b=YGIOK0ASkFBxPxlzD+PLPqkigELSoYqUYvt/mthr0iEjwueBu/o8c+7M8RTgc9uDML vMgRH3hY1qN1PLaNlnOh25NPOBVsvwUBRmWr+ziXzcyUfqy0RlC+i9YBY9bInGsyBQUu 3i1k7rp21qvfEKsJiv/cF7/1AyYGyzmQ/x9SDrCSHMoP82/Qi9XRULDAKUpJnhIDNMev +y8ycKj/C0vtd/29UwyC1UwfHceEhaIj9RHe1hRYivC10m7bAlBULQjIJFRxzWVyfySa 8jdAExk5RmZHXjNyNxT9fyPWBXxIJUAqkGqPJ5XAx/hy+7Hs+eti4+0L+MUVyukz5QOF HxoQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id b21-20020aa7c6d5000000b0051e04e2dbd9si10248767eds.258.2023.07.15.03.48.10; Sat, 15 Jul 2023 03:48:11 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 96FBD68C6C6; Sat, 15 Jul 2023 13:46:45 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: l4f/g91cCvKJ --- 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) {