[FFmpeg-devel] [PATCH 6/7] fftools/ffmpeg_demux: Fix leak on error

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Sat Oct 7 03:40:30 EEST 2023


An AVFormatContext leaks on errors that happen before it is attached
to its permanent place (an InputFile). Fix this by attaching
it earlier.

Given that it is not documented that avformat_close_input() is usable
with an AVFormatContext that has only been allocated with
avformat_alloc_context() and not opened with avformat_open_input(),
one error path before avformat_open_input() had to be treated
specially: It uses avformat_free_context().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 fftools/ffmpeg_demux.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 41fcb678c6..350f233ab7 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -1462,8 +1462,10 @@ int ifile_open(const OptionsContext *o, const char *filename)
     if (data_codec_name)
         ret = err_merge(ret, find_codec(NULL, data_codec_name    , AVMEDIA_TYPE_DATA,     0,
                                         &ic->data_codec));
-    if (ret < 0)
+    if (ret < 0) {
+        avformat_free_context(ic);
         return ret;
+    }
 
     ic->video_codec_id     = video_codec_name    ? ic->video_codec->id    : AV_CODEC_ID_NONE;
     ic->audio_codec_id     = audio_codec_name    ? ic->audio_codec->id    : AV_CODEC_ID_NONE;
@@ -1488,6 +1490,7 @@ int ifile_open(const OptionsContext *o, const char *filename)
             av_log(d, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
         return err;
     }
+    f->ctx = ic;
 
     av_strlcat(d->log_name, "/",               sizeof(d->log_name));
     av_strlcat(d->log_name, ic->iformat->name, sizeof(d->log_name));
@@ -1527,10 +1530,8 @@ int ifile_open(const OptionsContext *o, const char *filename)
 
         if (ret < 0) {
             av_log(d, AV_LOG_FATAL, "could not find codec parameters\n");
-            if (ic->nb_streams == 0) {
-                avformat_close_input(&ic);
+            if (ic->nb_streams == 0)
                 return ret;
-            }
         }
     }
 
@@ -1582,7 +1583,6 @@ int ifile_open(const OptionsContext *o, const char *filename)
         }
     }
 
-    f->ctx        = ic;
     f->start_time = start_time;
     f->recording_time = recording_time;
     f->input_sync_ref = o->input_sync_ref;
-- 
2.34.1



More information about the ffmpeg-devel mailing list