[FFmpeg-cvslog] avformat/libmodplug: Fix memleaks on error

Andreas Rheinhardt git at videolan.org
Sat Apr 3 01:18:12 EEST 2021


ffmpeg | branch: release/4.4 | Andreas Rheinhardt <andreas.rheinhardt at gmail.com> | Wed Mar 24 06:31:16 2021 +0100| [955be73bc5fc9edf42581f67e441378577161b2b] | committer: Andreas Rheinhardt

avformat/libmodplug: Fix memleaks on error

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
(cherry picked from commit df6dc331dd54c779c779eb8b950ad83c81799de4)

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

 libavformat/libmodplug.c | 37 ++++++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 13 deletions(-)

diff --git a/libavformat/libmodplug.c b/libavformat/libmodplug.c
index 6e567f5f98..b85269341b 100644
--- a/libavformat/libmodplug.c
+++ b/libavformat/libmodplug.c
@@ -99,6 +99,14 @@ static const AVOption options[] = {
     {NULL},
 };
 
+static int modplug_read_close(AVFormatContext *s)
+{
+    ModPlugContext *modplug = s->priv_data;
+    ModPlug_Unload(modplug->f);
+    av_freep(&modplug->buf);
+    return 0;
+}
+
 #define SET_OPT_IF_REQUESTED(libopt, opt, flag) do {        \
     if (modplug->opt) {                                     \
         settings.libopt  = modplug->opt;                    \
@@ -168,6 +176,7 @@ static int modplug_read_header(AVFormatContext *s)
     ModPlug_Settings settings;
     ModPlugContext *modplug = s->priv_data;
     int64_t sz = avio_size(pb);
+    int ret;
 
     if (sz < 0) {
         av_log(s, AV_LOG_WARNING, "Could not determine file size\n");
@@ -221,8 +230,10 @@ static int modplug_read_header(AVFormatContext *s)
         return AVERROR_INVALIDDATA;
     }
     st = avformat_new_stream(s, NULL);
-    if (!st)
-        return AVERROR(ENOMEM);
+    if (!st) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
     avpriv_set_pts_info(st, 64, 1, 1000);
     st->duration = ModPlug_GetLength(modplug->f);
     st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
@@ -235,8 +246,10 @@ static int modplug_read_header(AVFormatContext *s)
 
     if (modplug->video_stream) {
         AVStream *vst = avformat_new_stream(s, NULL);
-        if (!vst)
-            return AVERROR(ENOMEM);
+        if (!vst) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
         avpriv_set_pts_info(vst, 64, 1, 1000);
         vst->duration = st->duration;
         vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
@@ -247,7 +260,13 @@ static int modplug_read_header(AVFormatContext *s)
         modplug->fsize    = modplug->linesize * modplug->h;
     }
 
-    return modplug_load_metadata(s);
+    ret = modplug_load_metadata(s);
+    if (ret < 0)
+        goto fail;
+    return 0;
+fail:
+    modplug_read_close(s);
+    return ret;
 }
 
 static void write_text(uint8_t *dst, const char *s, int linesize, int x, int y)
@@ -332,14 +351,6 @@ static int modplug_read_packet(AVFormatContext *s, AVPacket *pkt)
     return 0;
 }
 
-static int modplug_read_close(AVFormatContext *s)
-{
-    ModPlugContext *modplug = s->priv_data;
-    ModPlug_Unload(modplug->f);
-    av_freep(&modplug->buf);
-    return 0;
-}
-
 static int modplug_read_seek(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
 {
     ModPlugContext *modplug = s->priv_data;



More information about the ffmpeg-cvslog mailing list