[FFmpeg-devel] [PATCH 26/47] fftools/cmdutils: add error handling to allocate_array_elem()

Anton Khirnov anton at khirnov.net
Sat Jul 15 13:45:50 EEST 2023


---
 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



More information about the ffmpeg-devel mailing list