PR #22522 opened by Huihui_Huang
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22522
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22522.patch

### Description:

`adapt_colors()` allocates a `SwsLut3D` object using `ff_sws_lut3d_alloc()` 
before performing an optional format conversion via `add_convert_pass()`:
``` c
lut = ff_sws_lut3d_alloc();
```
If `add_convert_pass()` fails, the function returns immediately without freeing 
the previously allocated `lut`. Since `lut` is allocated locally and has not 
yet been transferred to `ff_sws_graph_add_pass()` on this path, this may result 
in a memory leak.
``` c
if (fmt_in != src.format) {
    SwsFormat tmp = src;
    tmp.format = fmt_in;
    ret = add_convert_pass(graph, &src, &tmp, input, &input);
    if (ret < 0)
        return ret; // leak here
}
```
### Fix:
Free `lut` before returning when `add_convert_pass()` fails. The fix is 
included in this commit.


>From 6e37545d6be7fb3bd74a4766eb0712ed443603bb Mon Sep 17 00:00:00 2001
From: Huihui_Huang <[email protected]>
Date: Tue, 17 Mar 2026 15:56:15 +0800
Subject: [PATCH] swscale: fix possible lut leak in adapt_colors()

adapt_colors() allocates a SwsLut3D before calling add_convert_pass(). If 
add_convert_pass() fails, the function returns without freeing the previously 
allocated lut. Free lut on that error path.

Signed-off-by: Huihui_Huang <[email protected]>
---
 libswscale/graph.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libswscale/graph.c b/libswscale/graph.c
index db11591862..3b16037767 100644
--- a/libswscale/graph.c
+++ b/libswscale/graph.c
@@ -702,8 +702,10 @@ static int adapt_colors(SwsGraph *graph, SwsFormat src, 
SwsFormat dst,
         SwsFormat tmp = src;
         tmp.format = fmt_in;
         ret = add_convert_pass(graph, &src, &tmp, input, &input);
-        if (ret < 0)
+        if (ret < 0) {
+            ff_sws_lut3d_free(&lut);
             return ret;
+        }
     }
 
     ret = ff_sws_lut3d_generate(lut, fmt_in, fmt_out, &map);
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to