From 9ca0577c5e4cf1e54466282bd56fcad1793b7e09 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michael@niedermayer.cc>
Date: Mon, 10 Jun 2024 15:18:07 +0200
Subject: [PATCH] avfilter/af_amerge: Cleanup on av_channel_layout_copy()
 failure

Fixes: CID1503088 Resource leak

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
(cherry picked from commit 62d4414d54f57612ac444643a92de7d10455b6c6)
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavfilter/af_amerge.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/libavfilter/af_amerge.c b/libavfilter/af_amerge.c
index 5daf639e74..26040e5ed5 100644
--- a/libavfilter/af_amerge.c
+++ b/libavfilter/af_amerge.c
@@ -245,8 +245,11 @@ static int try_push_frame(AVFilterContext *ctx, int nb_samples)
                                     av_make_q(1, outlink->sample_rate),
                                     outlink->time_base);
 
-    if ((ret = av_channel_layout_copy(&outbuf->ch_layout, &outlink->ch_layout)) < 0)
+    if ((ret = av_channel_layout_copy(&outbuf->ch_layout, &outlink->ch_layout)) < 0) {
+        free_frames(s->nb_inputs, inbuf);
+        av_frame_free(&outbuf);
         return ret;
+    }
 
     while (nb_samples) {
         /* Unroll the most common sample formats: speed +~350% for the loop,