From fc38459e50d35d046f2b5ce03b02527b28842c59 Mon Sep 17 00:00:00 2001
From: Mengkejiergeli Ba <mengkejiergeli.ba@intel.com>
Date: Mon, 19 Feb 2024 12:50:53 +0800
Subject: [PATCH] msdk: Fix possible memory leaks

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/6161>
---
 .../sys/msdk/gstmsdkallocator_libva.c         | 13 +++++++++---
 .../gst-plugins-bad/sys/msdk/gstmsdkvpp.c     | 20 ++++++++++---------
 2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkallocator_libva.c b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkallocator_libva.c
index 395097147c..eab8001602 100644
--- a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkallocator_libva.c
+++ b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkallocator_libva.c
@@ -118,13 +118,16 @@ gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req,
 
     pool = gst_msdk_context_get_alloc_pool (context);
     if (!pool) {
+      status = MFX_ERR_MEMORY_ALLOC;
       goto error_alloc;
     }
 
     config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
     if (!gst_buffer_pool_config_get_params (config, &caps, NULL, &min_buffers,
-            &max_buffers))
+            &max_buffers)) {
+      status = MFX_ERR_MEMORY_ALLOC;
       goto error_alloc;
+    }
 
     max_buffers = MAX (max_buffers, surfaces_num);
     gst_buffer_pool_config_set_params (config, caps,
@@ -138,12 +141,14 @@ gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req,
     if (!gst_buffer_pool_set_config (pool, config)) {
       GST_ERROR ("Failed to set pool config");
       gst_object_unref (pool);
+      status = MFX_ERR_MEMORY_ALLOC;
       goto error_alloc;
     }
 
     if (!gst_buffer_pool_set_active (pool, TRUE)) {
       GST_ERROR ("Failed to activate pool");
       gst_object_unref (pool);
+      status = MFX_ERR_MEMORY_ALLOC;
       goto error_alloc;
     }
 
@@ -154,6 +159,7 @@ gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req,
         GST_ERROR ("Failed to allocate buffer");
         gst_buffer_pool_set_active (pool, FALSE);
         gst_object_unref (pool);
+        status = MFX_ERR_MEMORY_ALLOC;
         goto error_alloc;
       }
 
@@ -163,6 +169,7 @@ gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req,
         GST_ERROR ("Failed to get GstMsdkSurface");
         gst_buffer_pool_set_active (pool, FALSE);
         gst_object_unref (pool);
+        status = MFX_ERR_MEMORY_ALLOC;
         goto error_alloc;
       }
 
@@ -192,7 +199,7 @@ gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req,
       status = gst_msdk_get_mfx_status_from_va_status (va_status);
       if (status < MFX_ERR_NONE) {
         GST_ERROR ("failed to create buffer");
-        return status;
+        goto error_alloc;
       }
 
       msdk_mid.surface = coded_buf;
@@ -224,7 +231,7 @@ error_alloc:
   g_slice_free (GstMsdkAllocResponse, msdk_resp);
   if (tmp_list)
     g_list_free_full (tmp_list, (GDestroyNotify) gst_msdk_surface_list_free);
-  return MFX_ERR_MEMORY_ALLOC;
+  return status;
 }
 
 mfxStatus
diff --git a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkvpp.c b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkvpp.c
index edb3173b23..be97f07ced 100644
--- a/subprojects/gst-plugins-bad/sys/msdk/gstmsdkvpp.c
+++ b/subprojects/gst-plugins-bad/sys/msdk/gstmsdkvpp.c
@@ -865,12 +865,13 @@ gst_msdkvpp_transform (GstBaseTransform * trans, GstBuffer * inbuf,
     in_surface->surface->Data.TimeStamp =
         gst_util_uint64_scale_round (inbuf->pts, 90000, GST_SECOND);
 
-  out_surface = gst_msdk_import_to_msdk_surface (outbuf, thiz->context,
-      &thiz->srcpad_info, GST_MAP_WRITE);
-
-  if (!thiz->use_video_memory)
+  if (thiz->use_video_memory) {
+    out_surface = gst_msdk_import_to_msdk_surface (outbuf, thiz->context,
+        &thiz->srcpad_info, GST_MAP_WRITE);
+  } else {
     out_surface =
         gst_msdk_import_sys_mem_to_msdk_surface (outbuf, &thiz->srcpad_info);
+  }
 
   if (out_surface) {
     out_surface->buf = gst_buffer_ref (outbuf);
@@ -951,14 +952,15 @@ gst_msdkvpp_transform (GstBaseTransform * trans, GstBuffer * inbuf,
       GST_BUFFER_DURATION (outbuf_new) = thiz->buffer_duration;
 
       release_out_surface (thiz, out_surface);
-      out_surface =
-          gst_msdk_import_to_msdk_surface (outbuf_new, thiz->context,
-          &thiz->srcpad_buffer_pool_info, GST_MAP_WRITE);
-
-      if (!thiz->use_video_memory)
+      if (thiz->use_video_memory) {
+        out_surface =
+            gst_msdk_import_to_msdk_surface (outbuf_new, thiz->context,
+            &thiz->srcpad_buffer_pool_info, GST_MAP_WRITE);
+      } else {
         out_surface =
             gst_msdk_import_sys_mem_to_msdk_surface (outbuf_new,
             &thiz->srcpad_buffer_pool_info);
+      }
 
       if (out_surface) {
         out_surface->buf = gst_buffer_ref (outbuf_new);