Browse code

avformat/matroskadec: use av_fast_realloc to reallocate ebml list arrays

Speeds up the process considerably.

Fixes ticket #8109.

Suggested-by: nevcairiel
Suggested-by: cehoyos
Signed-off-by: James Almer <jamrial@gmail.com>
(cherry picked from commit 3b3150c45f1ebb3635e55e76b63439d8d62de85f)

James Almer authored on 2019/09/04 06:45:04
Showing 1 changed files
... ...
@@ -110,6 +110,7 @@ typedef const struct EbmlSyntax {
110 110
 
111 111
 typedef struct EbmlList {
112 112
     int nb_elem;
113
+    unsigned int alloc_elem_size;
113 114
     void *elem;
114 115
 } EbmlList;
115 116
 
... ...
@@ -1236,8 +1237,13 @@ static int ebml_parse(MatroskaDemuxContext *matroska,
1236 1236
         data = (char *) data + syntax->data_offset;
1237 1237
         if (syntax->list_elem_size) {
1238 1238
             EbmlList *list = data;
1239
-            void *newelem = av_realloc_array(list->elem, list->nb_elem + 1,
1240
-                                                   syntax->list_elem_size);
1239
+            void *newelem;
1240
+
1241
+            if ((unsigned)list->nb_elem + 1 >= UINT_MAX / syntax->list_elem_size)
1242
+                return AVERROR(ENOMEM);
1243
+            newelem = av_fast_realloc(list->elem,
1244
+                                      &list->alloc_elem_size,
1245
+                                      (list->nb_elem + 1) * syntax->list_elem_size);
1241 1246
             if (!newelem)
1242 1247
                 return AVERROR(ENOMEM);
1243 1248
             list->elem = newelem;
... ...
@@ -1490,6 +1496,7 @@ static void ebml_free(EbmlSyntax *syntax, void *data)
1490 1490
                     ebml_free(syntax[i].def.n, ptr);
1491 1491
                 av_freep(&list->elem);
1492 1492
                 list->nb_elem = 0;
1493
+                list->alloc_elem_size = 0;
1493 1494
             } else
1494 1495
                 ebml_free(syntax[i].def.n, data_off);
1495 1496
         default: