diff mbox series

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

Message ID AS8P250MB074427DD3617E0BF31C0C2138FC8A@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit c06d3d24047206f9c11bfc5849544b960e5d68eb
Headers show
Series [FFmpeg-devel,1/7] avcodec/wmv2dec: Parse extradata during init | expand

Commit Message

Andreas Rheinhardt Oct. 7, 2023, 12:40 a.m. UTC
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@outlook.com>
---
 fftools/ffmpeg_demux.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Comments

Anton Khirnov Oct. 10, 2023, 11:51 a.m. UTC | #1
Quoting Andreas Rheinhardt (2023-10-07 02:40:30)
> 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@outlook.com>
> ---
>  fftools/ffmpeg_demux.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)

Ok
diff mbox series

Patch

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;