diff mbox series

[FFmpeg-devel,28/47] fftools/cmdutils: add error handling to GROW_ARRAY()

Message ID 20230715104611.17902-28-anton@khirnov.net
State Accepted
Commit 2e6afa799ef693b94f993f54ed41a84f6d9f1685
Headers show
Series [FFmpeg-devel,01/47] fftools/ffmpeg_mux_init: handle pixel format endianness | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Anton Khirnov July 15, 2023, 10:45 a.m. UTC
---
 fftools/cmdutils.c        | 44 ++++++++++++++++++++++++++++++---------
 fftools/cmdutils.h        |  6 +-----
 fftools/ffmpeg_demux.c    | 10 +++++++--
 fftools/ffmpeg_mux_init.c | 15 ++++++++++---
 fftools/ffmpeg_opt.c      | 24 ++++++++++++++++-----
 fftools/ffplay.c          |  5 ++++-
 6 files changed, 78 insertions(+), 26 deletions(-)
diff mbox series

Patch

diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index 63b29c7a3a..fb35245f0a 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -609,13 +609,17 @@  static int match_group_separator(const OptionGroupDef *groups, int nb_groups,
  * @param group_idx which group definition should this group belong to
  * @param arg argument of the group delimiting option
  */
-static void finish_group(OptionParseContext *octx, int group_idx,
-                         const char *arg)
+static int finish_group(OptionParseContext *octx, int group_idx,
+                        const char *arg)
 {
     OptionGroupList *l = &octx->groups[group_idx];
     OptionGroup *g;
+    int ret;
+
+    ret = GROW_ARRAY(l->groups, l->nb_groups);
+    if (ret < 0)
+        return ret;
 
-    GROW_ARRAY(l->groups, l->nb_groups);
     g = &l->groups[l->nb_groups - 1];
 
     *g             = octx->cur_group;
@@ -632,21 +636,29 @@  static void finish_group(OptionParseContext *octx, int group_idx,
     swr_opts    = NULL;
 
     memset(&octx->cur_group, 0, sizeof(octx->cur_group));
+
+    return ret;
 }
 
 /*
  * Add an option instance to currently parsed group.
  */
-static void add_opt(OptionParseContext *octx, const OptionDef *opt,
-                    const char *key, const char *val)
+static int add_opt(OptionParseContext *octx, const OptionDef *opt,
+                   const char *key, const char *val)
 {
     int global = !(opt->flags & (OPT_PERFILE | OPT_SPEC | OPT_OFFSET));
     OptionGroup *g = global ? &octx->global_opts : &octx->cur_group;
+    int ret;
+
+    ret = GROW_ARRAY(g->opts, g->nb_opts);
+    if (ret < 0)
+        return ret;
 
-    GROW_ARRAY(g->opts, g->nb_opts);
     g->opts[g->nb_opts - 1].opt = opt;
     g->opts[g->nb_opts - 1].key = key;
     g->opts[g->nb_opts - 1].val = val;
+
+    return 0;
 }
 
 static int init_parse_context(OptionParseContext *octx,
@@ -726,7 +738,10 @@  int split_commandline(OptionParseContext *octx, int argc, char *argv[],
         }
         /* unnamed group separators, e.g. output filename */
         if (opt[0] != '-' || !opt[1] || dashdash+1 == optindex) {
-            finish_group(octx, 0, opt);
+            ret = finish_group(octx, 0, opt);
+            if (ret < 0)
+                return ret;
+
             av_log(NULL, AV_LOG_DEBUG, " matched as %s.\n", groups[0].name);
             continue;
         }
@@ -744,7 +759,10 @@  do {                                                                           \
         /* named group separators, e.g. -i */
         if ((ret = match_group_separator(groups, nb_groups, opt)) >= 0) {
             GET_ARG(arg);
-            finish_group(octx, ret, arg);
+            ret = finish_group(octx, ret, arg);
+            if (ret < 0)
+                return ret;
+
             av_log(NULL, AV_LOG_DEBUG, " matched as %s with argument '%s'.\n",
                    groups[ret].name, arg);
             continue;
@@ -762,7 +780,10 @@  do {                                                                           \
                 arg = "1";
             }
 
-            add_opt(octx, po, opt, arg);
+            ret = add_opt(octx, po, opt, arg);
+            if (ret < 0)
+                return ret;
+
             av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
                    "argument '%s'.\n", po->name, po->help, arg);
             continue;
@@ -787,7 +808,10 @@  do {                                                                           \
         if (opt[0] == 'n' && opt[1] == 'o' &&
             (po = find_option(options, opt + 2)) &&
             po->name && po->flags & OPT_BOOL) {
-            add_opt(octx, po, opt, "0");
+            ret = add_opt(octx, po, opt, "0");
+            if (ret < 0)
+                return ret;
+
             av_log(NULL, AV_LOG_DEBUG, " matched as option '%s' (%s) with "
                    "argument 0.\n", po->name, po->help);
             continue;
diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h
index 0dfe8b313c..69c2123804 100644
--- a/fftools/cmdutils.h
+++ b/fftools/cmdutils.h
@@ -440,11 +440,7 @@  int grow_array(void **array, int elem_size, int *size, int new_size);
 void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);
 
 #define GROW_ARRAY(array, nb_elems)\
-do {                                                                        \
-    int _ret = grow_array((void**)&array, sizeof(*array), &nb_elems, nb_elems + 1); \
-    if (_ret < 0)                                                           \
-        report_and_exit(_ret);                                              \
-} while (0)
+    grow_array((void**)&array, sizeof(*array), &nb_elems, nb_elems + 1)
 
 #define GET_PIX_FMT_NAME(pix_fmt)\
     const char *name = av_get_pix_fmt_name(pix_fmt);
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 72b94ea44f..5a41db9b21 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -880,7 +880,10 @@  int ist_output_add(InputStream *ist, OutputStream *ost)
     if (ret < 0)
         return ret;
 
-    GROW_ARRAY(ist->outputs, ist->nb_outputs);
+    ret = GROW_ARRAY(ist->outputs, ist->nb_outputs);
+    if (ret < 0)
+        return ret;
+
     ist->outputs[ist->nb_outputs - 1] = ost;
 
     return 0;
@@ -894,7 +897,10 @@  int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
     if (ret < 0)
         return ret;
 
-    GROW_ARRAY(ist->filters, ist->nb_filters);
+    ret = GROW_ARRAY(ist->filters, ist->nb_filters);
+    if (ret < 0)
+        return ret;
+
     ist->filters[ist->nb_filters - 1] = ifilter;
 
     // initialize fallback parameters for filtering
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index e714d6cc70..cb943ac408 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -209,7 +209,9 @@  static int enc_stats_get_file(AVIOContext **io, const char *path)
             return 0;
         }
 
-    GROW_ARRAY(enc_stats_files, nb_enc_stats_files);
+    ret = GROW_ARRAY(enc_stats_files, nb_enc_stats_files);
+    if (ret < 0)
+        return ret;
 
     esf = &enc_stats_files[nb_enc_stats_files - 1];
 
@@ -321,7 +323,11 @@  static int enc_stats_init(OutputStream *ost, EncStats *es, int pre,
             return ret;
 
         if (val) {
-            GROW_ARRAY(es->components, es->nb_components);
+            ret = GROW_ARRAY(es->components, es->nb_components);
+            if (ret < 0) {
+                av_freep(&val);
+                return ret;
+            }
 
             c          = &es->components[es->nb_components - 1];
             c->type    = ENC_STATS_LITERAL;
@@ -352,7 +358,10 @@  static int enc_stats_init(OutputStream *ost, EncStats *es, int pre,
         }
         next++;
 
-        GROW_ARRAY(es->components, es->nb_components);
+        ret = GROW_ARRAY(es->components, es->nb_components);
+        if (ret < 0)
+            return ret;
+
         c = &es->components[es->nb_components - 1];
 
         for (size_t i = 0; i < FF_ARRAY_ELEMS(fmt_specs); i++) {
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 7002986369..eef6d5e749 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -361,6 +361,7 @@  static int opt_map(void *optctx, const char *opt, const char *arg)
     OptionsContext *o = optctx;
     StreamMap *m = NULL;
     int i, negative = 0, file_idx, disabled = 0;
+    int ret;
 #if FFMPEG_OPT_MAP_SYNC
     char *sync;
 #endif
@@ -387,7 +388,11 @@  static int opt_map(void *optctx, const char *opt, const char *arg)
     if (map[0] == '[') {
         /* this mapping refers to lavfi output */
         const char *c = map + 1;
-        GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
+
+        ret = GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
+        if (ret < 0)
+            return ret;
+
         m = &o->stream_maps[o->nb_stream_maps - 1];
         m->linklabel = av_get_token(&c, "]");
         if (!m->linklabel) {
@@ -421,7 +426,10 @@  static int opt_map(void *optctx, const char *opt, const char *arg)
                     disabled = 1;
                     continue;
                 }
-                GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
+                ret = GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
+                if (ret < 0)
+                    return ret;
+
                 m = &o->stream_maps[o->nb_stream_maps - 1];
 
                 m->file_index   = file_idx;
@@ -450,7 +458,10 @@  static int opt_map(void *optctx, const char *opt, const char *arg)
 static int opt_attach(void *optctx, const char *opt, const char *arg)
 {
     OptionsContext *o = optctx;
-    GROW_ARRAY(o->attachments, o->nb_attachments);
+    int ret = GROW_ARRAY(o->attachments, o->nb_attachments);
+    if (ret < 0)
+        return ret;
+
     o->attachments[o->nb_attachments - 1] = arg;
     return 0;
 }
@@ -459,7 +470,7 @@  static int opt_attach(void *optctx, const char *opt, const char *arg)
 static int opt_map_channel(void *optctx, const char *opt, const char *arg)
 {
     OptionsContext *o = optctx;
-    int n;
+    int n, ret;
     AVStream *st;
     AudioChannelMap *m;
     char *allow_unused;
@@ -474,7 +485,10 @@  static int opt_map_channel(void *optctx, const char *opt, const char *arg)
     if (!mapchan)
         return AVERROR(ENOMEM);
 
-    GROW_ARRAY(o->audio_channel_maps, o->nb_audio_channel_maps);
+    ret = GROW_ARRAY(o->audio_channel_maps, o->nb_audio_channel_maps);
+    if (ret < 0)
+        return ret;
+
     m = &o->audio_channel_maps[o->nb_audio_channel_maps - 1];
 
     /* muted channel syntax */
diff --git a/fftools/ffplay.c b/fftools/ffplay.c
index 99700dc6f2..1f9757d607 100644
--- a/fftools/ffplay.c
+++ b/fftools/ffplay.c
@@ -388,7 +388,10 @@  static const struct TextureFormatEntry {
 
 static int opt_add_vfilter(void *optctx, const char *opt, const char *arg)
 {
-    GROW_ARRAY(vfilters_list, nb_vfilters);
+    int ret = GROW_ARRAY(vfilters_list, nb_vfilters);
+    if (ret < 0)
+        return ret;
+
     vfilters_list[nb_vfilters - 1] = arg;
     return 0;
 }