From 8b6336b91dae4e4706cc5a7537d774b0408e1736 Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Sun, 5 Feb 2023 17:11:01 +0100 Subject: [PATCH] [xcode] Protect against memleak if future ffmpeg doesn't free options Can happen if ffmpeg doesn't recognize all the options --- src/transcode.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/transcode.c b/src/transcode.c index dc7d2d0b..9d3820dc 100644 --- a/src/transcode.c +++ b/src/transcode.c @@ -522,8 +522,7 @@ stream_add(struct encode_ctx *ctx, struct stream_ctx *s, enum AVCodecID codec_id if (ret < 0) { DPRINTF(E_LOG, L_XCODE, "Cannot open encoder (%s): %s\n", codec_desc->name, err2str(ret)); - avcodec_free_context(&s->codec); - return -1; + goto error; } // Copy the codec parameters we just set to the stream, so the muxer knows them @@ -531,11 +530,24 @@ stream_add(struct encode_ctx *ctx, struct stream_ctx *s, enum AVCodecID codec_id if (ret < 0) { DPRINTF(E_LOG, L_XCODE, "Cannot copy stream parameters (%s): %s\n", codec_desc->name, err2str(ret)); - avcodec_free_context(&s->codec); - return -1; + goto error; + } + + if (options) + { + DPRINTF(E_WARN, L_XCODE, "Encoder %s didn't recognize all options given to avcodec_open2\n", codec_desc->name); + av_dict_free(&options); } return 0; + + error: + if (s->codec) + avcodec_free_context(&s->codec); + if (options) + av_dict_free(&options); + + return -1; } /*