summaryrefslogtreecommitdiff
path: root/fftools/ffmpeg_opt.c
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2021-12-03 13:12:39 +0100
committerAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2021-12-05 13:27:37 +0100
commit3ca1e31e63085ab84bc2d031c9ba06ea161188d3 (patch)
tree98075d431dea6617d68248d17675eb84990fae02 /fftools/ffmpeg_opt.c
parent3a9861e22c636d843c10e23f5585196d1f3400dd (diff)
downloadffmpeg-3ca1e31e63085ab84bc2d031c9ba06ea161188d3.tar.gz
fftools/cmdutils: Atomically add elements to list of pointers, fix crash
Currently, adding a (separately allocated) element to a list of pointers works by first reallocating the array of pointers and (on success) incrementing its size and only then allocating the new element. If the latter allocation fails, the size is inconsistent, i.e. array[nb_array_elems - 1] is NULL. Our cleanup code crashes in such scenarios. Fix this by adding an auxiliary function that atomically allocates and adds a new element to a list of pointers. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Diffstat (limited to 'fftools/ffmpeg_opt.c')
-rw-r--r--fftools/ffmpeg_opt.c22
1 files changed, 6 insertions, 16 deletions
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 6c2eb53290..848b817e9c 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1265,11 +1265,8 @@ static int open_input_file(OptionsContext *o, const char *filename)
/* dump the file content */
av_dump_format(ic, nb_input_files, filename, 0);
- GROW_ARRAY(input_files, nb_input_files);
- f = av_mallocz(sizeof(*f));
- if (!f)
- exit_program(1);
- input_files[nb_input_files - 1] = f;
+ ALLOC_ARRAY_ELEM(input_files, nb_input_files);
+ f = input_files[nb_input_files - 1];
f->ctx = ic;
f->ist_index = nb_input_streams - ic->nb_streams;
@@ -2263,11 +2260,8 @@ static int open_output_file(OptionsContext *o, const char *filename)
}
}
- GROW_ARRAY(output_files, nb_output_files);
- of = av_mallocz(sizeof(*of));
- if (!of)
- exit_program(1);
- output_files[nb_output_files - 1] = of;
+ ALLOC_ARRAY_ELEM(output_files, nb_output_files);
+ of = output_files[nb_output_files - 1];
of->ost_index = nb_output_streams;
of->recording_time = o->recording_time;
@@ -3264,9 +3258,7 @@ static int opt_audio_qscale(void *optctx, const char *opt, const char *arg)
static int opt_filter_complex(void *optctx, const char *opt, const char *arg)
{
- GROW_ARRAY(filtergraphs, nb_filtergraphs);
- if (!(filtergraphs[nb_filtergraphs - 1] = av_mallocz(sizeof(*filtergraphs[0]))))
- return AVERROR(ENOMEM);
+ ALLOC_ARRAY_ELEM(filtergraphs, nb_filtergraphs);
filtergraphs[nb_filtergraphs - 1]->index = nb_filtergraphs - 1;
filtergraphs[nb_filtergraphs - 1]->graph_desc = av_strdup(arg);
if (!filtergraphs[nb_filtergraphs - 1]->graph_desc)
@@ -3283,9 +3275,7 @@ static int opt_filter_complex_script(void *optctx, const char *opt, const char *
if (!graph_desc)
return AVERROR(EINVAL);
- GROW_ARRAY(filtergraphs, nb_filtergraphs);
- if (!(filtergraphs[nb_filtergraphs - 1] = av_mallocz(sizeof(*filtergraphs[0]))))
- return AVERROR(ENOMEM);
+ ALLOC_ARRAY_ELEM(filtergraphs, nb_filtergraphs);
filtergraphs[nb_filtergraphs - 1]->index = nb_filtergraphs - 1;
filtergraphs[nb_filtergraphs - 1]->graph_desc = graph_desc;