[FFmpeg-cvslog] avconv: simplify memory allocation in copy_chapters

Janne Grunau git at videolan.org
Wed Oct 10 13:23:58 CEST 2012


ffmpeg | branch: master | Janne Grunau <janne-libav at jannau.net> | Tue Oct  9 15:20:15 2012 +0200| [18ff4d20201ae69fdeb2da2c90bdcbd33f7ac025] | committer: Janne Grunau

avconv: simplify memory allocation in copy_chapters

Make just a single reallocation per call instead of one reallocation
per copied chapters. This fixes possible memory leaks on realloc
failures. Also correct the allocation since it needs multiples of
sizeof(AVChapter*) and not sizeof(AVChapter).

Fixes CID700633 and CID700719.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=18ff4d20201ae69fdeb2da2c90bdcbd33f7ac025
---

 avconv_opt.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/avconv_opt.c b/avconv_opt.c
index 108e510..f8a76c0 100644
--- a/avconv_opt.c
+++ b/avconv_opt.c
@@ -1085,8 +1085,14 @@ static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata)
 {
     AVFormatContext *is = ifile->ctx;
     AVFormatContext *os = ofile->ctx;
+    AVChapter **tmp;
     int i;
 
+    tmp = av_realloc(os->chapters, sizeof(*os->chapters) * (is->nb_chapters + os->nb_chapters));
+    if (!tmp)
+        return AVERROR(ENOMEM);
+    os->chapters = tmp;
+
     for (i = 0; i < is->nb_chapters; i++) {
         AVChapter *in_ch = is->chapters[i], *out_ch;
         int64_t ts_off   = av_rescale_q(ofile->start_time - ifile->ts_offset,
@@ -1112,11 +1118,7 @@ static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata)
         if (copy_metadata)
             av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
 
-        os->nb_chapters++;
-        os->chapters = av_realloc(os->chapters, sizeof(AVChapter) * os->nb_chapters);
-        if (!os->chapters)
-            return AVERROR(ENOMEM);
-        os->chapters[os->nb_chapters - 1] = out_ch;
+        os->chapters[os->nb_chapters++] = out_ch;
     }
     return 0;
 }



More information about the ffmpeg-cvslog mailing list