diff mbox series

[FFmpeg-devel,1/7] avformat/utils: Check allocations for failure

Message ID HE1PR0301MB2154050DED74FF1F43F61FE58F7B9@HE1PR0301MB2154.eurprd03.prod.outlook.com
State Accepted
Commit 543e4a194252050cf1abcded7c75e4b889e3db4f
Headers show
Series [FFmpeg-devel,1/7] avformat/utils: Check allocations for failure | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt April 1, 2021, 9:24 p.m. UTC
There would be leaks in case of failure.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
There is unfortunately more of this dynarray_add.

 libavformat/utils.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

Comments

Andreas Rheinhardt April 2, 2021, 10:20 a.m. UTC | #1
Andreas Rheinhardt:
> There would be leaks in case of failure.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
> There is unfortunately more of this dynarray_add.
> 
>  libavformat/utils.c | 16 ++++++++++++----
>  1 file changed, 12 insertions(+), 4 deletions(-)
> 
> diff --git a/libavformat/utils.c b/libavformat/utils.c
> index 0834c80f4e..13b1bc7c78 100644
> --- a/libavformat/utils.c
> +++ b/libavformat/utils.c
> @@ -4625,7 +4625,7 @@ fail:
>  AVProgram *av_new_program(AVFormatContext *ac, int id)
>  {
>      AVProgram *program = NULL;
> -    int i;
> +    int i, ret;
>  
>      av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id);
>  
> @@ -4637,7 +4637,11 @@ AVProgram *av_new_program(AVFormatContext *ac, int id)
>          program = av_mallocz(sizeof(AVProgram));
>          if (!program)
>              return NULL;
> -        dynarray_add(&ac->programs, &ac->nb_programs, program);
> +        ret = av_dynarray_add_nofree(&ac->programs, &ac->nb_programs, program);
> +        if (ret < 0) {
> +            av_free(program);
> +            return NULL;
> +        }
>          program->discard = AVDISCARD_NONE;
>          program->pmt_version = -1;
>          program->id = id;
> @@ -4657,7 +4661,7 @@ AVChapter *avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_ba
>                                int64_t start, int64_t end, const char *title)
>  {
>      AVChapter *chapter = NULL;
> -    int i;
> +    int i, ret;
>  
>      if (end != AV_NOPTS_VALUE && start > end) {
>          av_log(s, AV_LOG_ERROR, "Chapter end time %"PRId64" before start %"PRId64"\n", end, start);
> @@ -4677,7 +4681,11 @@ AVChapter *avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_ba
>          chapter = av_mallocz(sizeof(AVChapter));
>          if (!chapter)
>              return NULL;
> -        dynarray_add(&s->chapters, &s->nb_chapters, chapter);
> +        ret = av_dynarray_add_nofree(&s->chapters, &s->nb_chapters, chapter);
> +        if (ret < 0) {
> +            av_free(chapter);
> +            return NULL;
> +        }
>      }
>      av_dict_set(&chapter->metadata, "title", title, 0);
>      chapter->id        = id;
> 
Will apply the first two patches of this patchset.

- Andreas
diff mbox series

Patch

diff --git a/libavformat/utils.c b/libavformat/utils.c
index 0834c80f4e..13b1bc7c78 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -4625,7 +4625,7 @@  fail:
 AVProgram *av_new_program(AVFormatContext *ac, int id)
 {
     AVProgram *program = NULL;
-    int i;
+    int i, ret;
 
     av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id);
 
@@ -4637,7 +4637,11 @@  AVProgram *av_new_program(AVFormatContext *ac, int id)
         program = av_mallocz(sizeof(AVProgram));
         if (!program)
             return NULL;
-        dynarray_add(&ac->programs, &ac->nb_programs, program);
+        ret = av_dynarray_add_nofree(&ac->programs, &ac->nb_programs, program);
+        if (ret < 0) {
+            av_free(program);
+            return NULL;
+        }
         program->discard = AVDISCARD_NONE;
         program->pmt_version = -1;
         program->id = id;
@@ -4657,7 +4661,7 @@  AVChapter *avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_ba
                               int64_t start, int64_t end, const char *title)
 {
     AVChapter *chapter = NULL;
-    int i;
+    int i, ret;
 
     if (end != AV_NOPTS_VALUE && start > end) {
         av_log(s, AV_LOG_ERROR, "Chapter end time %"PRId64" before start %"PRId64"\n", end, start);
@@ -4677,7 +4681,11 @@  AVChapter *avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_ba
         chapter = av_mallocz(sizeof(AVChapter));
         if (!chapter)
             return NULL;
-        dynarray_add(&s->chapters, &s->nb_chapters, chapter);
+        ret = av_dynarray_add_nofree(&s->chapters, &s->nb_chapters, chapter);
+        if (ret < 0) {
+            av_free(chapter);
+            return NULL;
+        }
     }
     av_dict_set(&chapter->metadata, "title", title, 0);
     chapter->id        = id;