diff mbox series

[FFmpeg-devel,4/4] fftools/cmdutils: Use av_dynarray_add_nofree()

Message ID AM7PR03MB6660F9927A77E2C0FE6512A48F6A9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit 2d0bfbd0fafe5e869919120758903801f91530fa
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
Simplifies code and reduces the number of allocations a bit
by overallocating.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 fftools/cmdutils.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index 0b57552e5c..3c8e5a82cd 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -2216,19 +2216,13 @@  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 *new_elem;
 
-    memcpy(&array, ptr, sizeof(array));
-    if (*nb_elems == INT_MAX) {
-        av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
+    if (!(new_elem = av_mallocz(elem_size)) ||
+        av_dynarray_add_nofree(ptr, nb_elems, new_elem) < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Could not alloc buffer.\n");
         exit_program(1);
     }
-    new_elem = av_mallocz(elem_size);
-    if (!new_elem)
-        exit_program(1);
-    GROW_ARRAY(array, *nb_elems);
-    memcpy(ptr, &array, sizeof(array));
-    array[*nb_elems - 1] = new_elem;
     return new_elem;
 }