diff mbox series

[FFmpeg-devel,3/4] fftools/cmdutils: Make allocate_array_elem() return ptr to new element

Message ID AM7PR03MB6660A7ED993535AB964241688F6A9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit 2e7ef008e312bde7c151034628adc2da04313566
Headers show
Series [FFmpeg-devel,1/4] fftools/cmdutils: Atomically add elements to list of pointers, fix crash
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Dec. 3, 2021, 6:28 p.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 fftools/cmdutils.c      |  6 ++++--
 fftools/cmdutils.h      |  4 ++--
 fftools/ffmpeg_filter.c | 13 ++++---------
 fftools/ffmpeg_opt.c    |  9 +++------
 4 files changed, 13 insertions(+), 19 deletions(-)
diff mbox series

Patch

diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index 1464b122df..0b57552e5c 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -2216,8 +2216,9 @@  void *grow_array(void *array, int elem_size, int *size, int new_size)
 
 void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
 {
-    void *new_elem, **array = (void**)ptr;
+    void *new_elem, **array;
 
+    memcpy(&array, ptr, sizeof(array));
     if (*nb_elems == INT_MAX) {
         av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
         exit_program(1);
@@ -2226,8 +2227,9 @@  void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
     if (!new_elem)
         exit_program(1);
     GROW_ARRAY(array, *nb_elems);
+    memcpy(ptr, &array, sizeof(array));
     array[*nb_elems - 1] = new_elem;
-    return array;
+    return new_elem;
 }
 
 double get_rotation(int32_t *displaymatrix)
diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h
index ae78e60f4c..82cda208be 100644
--- a/fftools/cmdutils.h
+++ b/fftools/cmdutils.h
@@ -638,7 +638,7 @@  void *grow_array(void *array, int elem_size, int *size, int new_size);
  * @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 reallocated array
+ * @return pointer to the newly allocated entry
  */
 void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);
 
@@ -648,7 +648,7 @@  void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);
     array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1)
 
 #define ALLOC_ARRAY_ELEM(array, nb_elems)\
-    array = allocate_array_elem(array, sizeof(*array[0]), &nb_elems)
+    allocate_array_elem(&array, sizeof(*array[0]), &nb_elems)
 
 #define GET_PIX_FMT_NAME(pix_fmt)\
     const char *name = av_get_pix_fmt_name(pix_fmt);
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 04253a9b21..bd48098bff 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -166,16 +166,14 @@  int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
         exit_program(1);
     fg->index = nb_filtergraphs;
 
-    ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
-    ofilter = fg->outputs[0];
+    ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
     ofilter->ost    = ost;
     ofilter->graph  = fg;
     ofilter->format = -1;
 
     ost->filter = ofilter;
 
-    ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
-    ifilter = fg->inputs[0];
+    ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
     ifilter->ist    = ist;
     ifilter->graph  = fg;
     ifilter->format = -1;
@@ -281,8 +279,7 @@  static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
     ist->decoding_needed |= DECODING_FOR_FILTER;
     ist->st->discard = AVDISCARD_NONE;
 
-    ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
-    ifilter = fg->inputs[fg->nb_inputs - 1];
+    ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
     ifilter->ist    = ist;
     ifilter->graph  = fg;
     ifilter->format = -1;
@@ -318,9 +315,7 @@  int init_complex_filtergraph(FilterGraph *fg)
         init_input_filter(fg, cur);
 
     for (cur = outputs; cur;) {
-        OutputFilter *ofilter;
-        ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
-        ofilter = fg->outputs[fg->nb_outputs - 1];
+        OutputFilter *const ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
 
         ofilter->graph   = fg;
         ofilter->out_tmp = cur;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 6f971bc922..17e4cdd2d6 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1265,8 +1265,7 @@  static int open_input_file(OptionsContext *o, const char *filename)
     /* dump the file content */
     av_dump_format(ic, nb_input_files, filename, 0);
 
-    ALLOC_ARRAY_ELEM(input_files, nb_input_files);
-    f = input_files[nb_input_files - 1];
+    f = ALLOC_ARRAY_ELEM(input_files, nb_input_files);
 
     f->ctx        = ic;
     f->ist_index  = nb_input_streams - ic->nb_streams;
@@ -2258,8 +2257,7 @@  static int open_output_file(OptionsContext *o, const char *filename)
         }
     }
 
-    ALLOC_ARRAY_ELEM(output_files, nb_output_files);
-    of = output_files[nb_output_files - 1];
+    of = ALLOC_ARRAY_ELEM(output_files, nb_output_files);
 
     of->ost_index      = nb_output_streams;
     of->recording_time = o->recording_time;
@@ -3276,8 +3274,7 @@  static int opt_filter_complex_script(void *optctx, const char *opt, const char *
     if (!graph_desc)
         return AVERROR(EINVAL);
 
-    ALLOC_ARRAY_ELEM(filtergraphs, nb_filtergraphs);
-    fg = filtergraphs[nb_filtergraphs - 1];
+    fg = ALLOC_ARRAY_ELEM(filtergraphs, nb_filtergraphs);
     fg->index      = nb_filtergraphs - 1;
     fg->graph_desc = graph_desc;