From dba249abeea40ee07edcec5508e872f767dc8c0f Mon Sep 17 00:00:00 2001
From: Aurelien Jacobs <aurel@gnuage.org>
Date: Fri, 1 Oct 2010 21:29:37 +0000
Subject: [PATCH] ffmpeg: add a grow_array() helper function

Originally committed as revision 25297 to svn://svn.ffmpeg.org/ffmpeg/trunk
---
 ffmpeg.c             | 20 ++++++++++++++++++++
 libavformat/cutils.c |  1 +
 2 files changed, 21 insertions(+)

diff --git a/ffmpeg.c b/ffmpeg.c
index 88ef7985a3a..cfd3cb8c65b 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -647,6 +647,26 @@ static int ffmpeg_exit(int ret)
     return ret;
 }
 
+/* similar to ff_dynarray_add() and av_fast_realloc() */
+static void *grow_array(void *array, int elem_size, int *size, int new_size)
+{
+    if (new_size >= INT_MAX / elem_size) {
+        fprintf(stderr, "Array too big.\n");
+        ffmpeg_exit(1);
+    }
+    if (*size < new_size) {
+        uint8_t *tmp = av_realloc(array, new_size*elem_size);
+        if (!tmp) {
+            fprintf(stderr, "Could not alloc buffer.\n");
+            ffmpeg_exit(1);
+        }
+        memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
+        *size = new_size;
+        return tmp;
+    }
+    return array;
+}
+
 static void choose_sample_fmt(AVStream *st, AVCodec *codec)
 {
     if(codec && codec->sample_fmts){
diff --git a/libavformat/cutils.c b/libavformat/cutils.c
index 5092d99f6a7..c1b56139c0f 100644
--- a/libavformat/cutils.c
+++ b/libavformat/cutils.c
@@ -24,6 +24,7 @@
 /* add one element to a dynamic array */
 void ff_dynarray_add(intptr_t **tab_ptr, int *nb_ptr, intptr_t elem)
 {
+    /* see similar ffmpeg.c:grow_array() */
     int nb, nb_alloc;
     intptr_t *tab;
 
-- 
GitLab