aboutsummaryrefslogtreecommitdiffstats
path: root/fftools/cmdutils.h
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/cmdutils.h
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/cmdutils.h')
-rw-r--r--fftools/cmdutils.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h
index 64dd7266bc..ae78e60f4c 100644
--- a/fftools/cmdutils.h
+++ b/fftools/cmdutils.h
@@ -628,11 +628,28 @@ FILE *get_preset_file(char *filename, size_t filename_size,
*/
void *grow_array(void *array, int elem_size, int *size, int new_size);
+/**
+ * Atomically add a new element to an array of pointers, i.e. allocate
+ * a new entry, reallocate the array of pointers and make the new last
+ * member of this array point to the newly allocated buffer.
+ * Calls exit() on failure.
+ *
+ * @param array array of pointers to reallocate
+ * @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
+ */
+void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);
+
#define media_type_string av_get_media_type_string
#define GROW_ARRAY(array, 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)
+
#define GET_PIX_FMT_NAME(pix_fmt)\
const char *name = av_get_pix_fmt_name(pix_fmt);