fftools/cmdutils: add error handling to GROW_ARRAY()

This commit is contained in:
Anton Khirnov 2023-07-14 12:28:18 +02:00
parent b23abb7c48
commit 2e6afa799e
6 changed files with 78 additions and 26 deletions

View File

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

View File

@ -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);

View File

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

View File

@ -208,7 +208,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];
@ -320,7 +322,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;
@ -351,7 +357,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++) {

View File

@ -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 */

View File

@ -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;
}