[FFmpeg-devel] [PATCH] mfenc: Fix double frees on init errors

Martin Storsjö martin at martin.st
Tue Jan 31 15:16:32 EET 2023


Make mf_close safe to call multiple times; it currently isn't
called more than once, but for clarity, make it safe to call
more than once.

If there are failures in ff_instantiate_mf, that function
calls uninit_com_mf() on its own. Make sure that the second
call to uninit_com_mf from ff_free_mf (from mf_close) is a
no-op in that case.

Signed-off-by: Martin Storsjö <martin at martin.st>
---
 libavcodec/mf_utils.c | 6 ++++--
 libavcodec/mfenc.c    | 2 ++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/libavcodec/mf_utils.c b/libavcodec/mf_utils.c
index 48e3a63efc..50b9fdb2c4 100644
--- a/libavcodec/mf_utils.c
+++ b/libavcodec/mf_utils.c
@@ -642,8 +642,10 @@ error_uninit_mf:
 
 void ff_free_mf(MFFunctions *f, IMFTransform **mft)
 {
-    if (*mft)
-        IMFTransform_Release(*mft);
+    if (!*mft)
+        return;
+
+    IMFTransform_Release(*mft);
     *mft = NULL;
     uninit_com_mf(f);
 }
diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c
index f3415df10b..e3b5fb1600 100644
--- a/libavcodec/mfenc.c
+++ b/libavcodec/mfenc.c
@@ -1184,9 +1184,11 @@ static int mf_close(AVCodecContext *avctx)
 
     if (c->codec_api)
         ICodecAPI_Release(c->codec_api);
+    c->codec_api = NULL;
 
     if (c->async_events)
         IMFMediaEventGenerator_Release(c->async_events);
+    c->async_events = NULL;
 
 #if !HAVE_UWP
     if (c->library)
-- 
2.37.1 (Apple Git-137.1)



More information about the ffmpeg-devel mailing list