diff mbox

[FFmpeg-devel,03/17] avformat/smoothstreaming: Fix memleaks on errors

Message ID 20191226105342.11175-3-andreas.rheinhardt@gmail.com
State Accepted
Headers show

Commit Message

Andreas Rheinhardt Dec. 26, 2019, 10:53 a.m. UTC
If an AVFormatContext could be allocated, but white-/blacklists couldn't
be copied, the AVFormatContext would leak as it was only accessible
through a local variable that goes out of scope when one goes to fail.

Furthermore, in case writing a header of a submuxer failed, the options
used for said call could leak.

Both of these memleaks have been fixed.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/smoothstreamingenc.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Andreas Rheinhardt March 14, 2020, midnight UTC | #1
Andreas Rheinhardt:
> If an AVFormatContext could be allocated, but white-/blacklists couldn't
> be copied, the AVFormatContext would leak as it was only accessible
> through a local variable that goes out of scope when one goes to fail.
> 
> Furthermore, in case writing a header of a submuxer failed, the options
> used for said call could leak.
> 
> Both of these memleaks have been fixed.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavformat/smoothstreamingenc.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/libavformat/smoothstreamingenc.c b/libavformat/smoothstreamingenc.c
> index 2faf1e7897..0e4f531f90 100644
> --- a/libavformat/smoothstreamingenc.c
> +++ b/libavformat/smoothstreamingenc.c
> @@ -332,12 +332,11 @@ static int ism_write_header(AVFormatContext *s)
>              goto fail;
>          }
>  
> -        ctx = avformat_alloc_context();
> +        os->ctx = ctx = avformat_alloc_context();
>          if (!ctx || ff_copy_whiteblacklists(ctx, s) < 0) {
>              ret = AVERROR(ENOMEM);
>              goto fail;
>          }
> -        os->ctx = ctx;
>          ctx->oformat = oformat;
>          ctx->interrupt_callback = s->interrupt_callback;
>  
> @@ -357,12 +356,13 @@ static int ism_write_header(AVFormatContext *s)
>  
>          av_dict_set_int(&opts, "ism_lookahead", c->lookahead_count, 0);
>          av_dict_set(&opts, "movflags", "frag_custom", 0);
> -        if ((ret = avformat_write_header(ctx, &opts)) < 0) {
> +        ret = avformat_write_header(ctx, &opts);
> +        av_dict_free(&opts);
> +        if (ret < 0) {
>               goto fail;
>          }
>          os->ctx_inited = 1;
>          avio_flush(ctx->pb);
> -        av_dict_free(&opts);
>          s->streams[i]->time_base = st->time_base;
>          if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
>              c->has_video = 1;
> 
Ping.

- Andreas
Michael Niedermayer March 14, 2020, 9:02 p.m. UTC | #2
On Thu, Dec 26, 2019 at 11:53:28AM +0100, Andreas Rheinhardt wrote:
> If an AVFormatContext could be allocated, but white-/blacklists couldn't
> be copied, the AVFormatContext would leak as it was only accessible
> through a local variable that goes out of scope when one goes to fail.
> 
> Furthermore, in case writing a header of a submuxer failed, the options
> used for said call could leak.
> 
> Both of these memleaks have been fixed.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavformat/smoothstreamingenc.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)

will apply

thx

[...]
diff mbox

Patch

diff --git a/libavformat/smoothstreamingenc.c b/libavformat/smoothstreamingenc.c
index 2faf1e7897..0e4f531f90 100644
--- a/libavformat/smoothstreamingenc.c
+++ b/libavformat/smoothstreamingenc.c
@@ -332,12 +332,11 @@  static int ism_write_header(AVFormatContext *s)
             goto fail;
         }
 
-        ctx = avformat_alloc_context();
+        os->ctx = ctx = avformat_alloc_context();
         if (!ctx || ff_copy_whiteblacklists(ctx, s) < 0) {
             ret = AVERROR(ENOMEM);
             goto fail;
         }
-        os->ctx = ctx;
         ctx->oformat = oformat;
         ctx->interrupt_callback = s->interrupt_callback;
 
@@ -357,12 +356,13 @@  static int ism_write_header(AVFormatContext *s)
 
         av_dict_set_int(&opts, "ism_lookahead", c->lookahead_count, 0);
         av_dict_set(&opts, "movflags", "frag_custom", 0);
-        if ((ret = avformat_write_header(ctx, &opts)) < 0) {
+        ret = avformat_write_header(ctx, &opts);
+        av_dict_free(&opts);
+        if (ret < 0) {
              goto fail;
         }
         os->ctx_inited = 1;
         avio_flush(ctx->pb);
-        av_dict_free(&opts);
         s->streams[i]->time_base = st->time_base;
         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
             c->has_video = 1;