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

This contains Qingzheng Li's three swaprect commits split as suggested by 
marvin in !24023


>From e9fcc874a11d97222a195928e7a00e7be4cb3ec8 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:19:10 +0100
Subject: [PATCH 01/18] avfilter/vf_feedback: clamp the position without
 integer overflow

Fixes: out of array read
Fixes: signed integer overflow
Fixes: hrmKDBP9CvuA

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

Out of array read Replicated through UnModified FFmpeg with ASAN
Signed integer overflow Replicated through UnModified FFmpeg with UBSAN
---
 libavfilter/vf_feedback.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/libavfilter/vf_feedback.c b/libavfilter/vf_feedback.c
index e3633d7d374..0665bde2217 100644
--- a/libavfilter/vf_feedback.c
+++ b/libavfilter/vf_feedback.c
@@ -46,10 +46,8 @@ typedef struct FeedbackContext {
 
 static void adjust_pos(AVFilterContext *ctx, FeedbackContext *s)
 {
-    if (s->x + s->w > ctx->inputs[0]->w)
-        s->x = ctx->inputs[0]->w - s->w;
-    if (s->y + s->h > ctx->inputs[0]->h)
-        s->y = ctx->inputs[0]->h - s->h;
+    s->x = FFMIN(s->x, ctx->inputs[0]->w - s->w);
+    s->y = FFMIN(s->y, ctx->inputs[0]->h - s->h);
 }
 
 static void adjust_parameters(AVFilterContext *ctx, FeedbackContext *s)
-- 
2.54.0


>From 96242c04be71dd83706ece67dc1e6b32c1bd4deb Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:19:37 +0100
Subject: [PATCH 02/18] avfilter/vf_feedback: clamp the position after queued
 commands ran

Fixes: out of array read
Fixes: V4WgPHrcMhhG

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

Out of array read Replicated through UnModified FFmpeg with ASAN
---
 libavfilter/vf_feedback.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavfilter/vf_feedback.c b/libavfilter/vf_feedback.c
index 0665bde2217..db0ebd12ee6 100644
--- a/libavfilter/vf_feedback.c
+++ b/libavfilter/vf_feedback.c
@@ -120,8 +120,6 @@ static int activate(AVFilterContext *ctx)
     int status, ret;
     int64_t pts;
 
-    adjust_pos(ctx, s);
-
     for (int i = 0; i < ctx->nb_outputs; i++)
         FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[i], ctx);
 
@@ -135,6 +133,7 @@ static int activate(AVFilterContext *ctx)
         AVFrame *src = s->feed;
         AVFrame *dst = NULL;
 
+        adjust_pos(ctx, s);
         av_fifo_read(s->fifo, &dst, 1);
         if (!dst)
             return AVERROR_BUG;
@@ -198,6 +197,7 @@ static int activate(AVFilterContext *ctx)
         if (ret > 0) {
             AVFrame *frame;
 
+            adjust_pos(ctx, s);
             ret = av_fifo_write(s->fifo, &in, 1);
             if (ret < 0) {
                 av_frame_free(&in);
-- 
2.54.0


>From 7f61d9426b9127f0bfec84e8a85db8f334693329 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:20:32 +0100
Subject: [PATCH 03/18] avfilter/f_streamselect: reject a map with more entries
 than outputs

Fixes: out of array read
Fixes: wShaWIYvxHRE

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

Out of array read Replicated through UnModified FFmpeg with ASAN
---
 libavfilter/f_streamselect.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavfilter/f_streamselect.c b/libavfilter/f_streamselect.c
index cce74b6dde9..f9702b81ce7 100644
--- a/libavfilter/f_streamselect.c
+++ b/libavfilter/f_streamselect.c
@@ -225,7 +225,7 @@ static int parse_mapping(AVFilterContext *ctx, const char 
*map)
         new_map[new_nb_map++] = n;
     }
 
-    if (!new_nb_map) {
+    if (!new_nb_map || new_nb_map > ctx->nb_outputs) {
         av_log(ctx, AV_LOG_ERROR, "invalid mapping\n");
         av_free(new_map);
         return AVERROR(EINVAL);
-- 
2.54.0


>From 926dca9cabdfa27b21b694035ea1853194e6e474 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:22:04 +0100
Subject: [PATCH 04/18] avfilter/vf_convolution: set the filter function for
 identity matrices too

Fixes: NULL pointer dereference
Fixes: ORvkcJq0tqFe
Regression since: fe0fdc51b5

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

NULL pointer dereference Replicated through UnModified FFmpeg with ASAN
---
 libavfilter/vf_convolution.c | 25 ++++++-------------------
 1 file changed, 6 insertions(+), 19 deletions(-)

diff --git a/libavfilter/vf_convolution.c b/libavfilter/vf_convolution.c
index cc55beb0be1..b222b46e2db 100644
--- a/libavfilter/vf_convolution.c
+++ b/libavfilter/vf_convolution.c
@@ -694,31 +694,18 @@ static int param_init(AVFilterContext *ctx)
                 s->size[i] = s->matrix_length[i];
             } else if (s->matrix_length[i] == 9) {
                 s->size[i] = 3;
-
-                if (!memcmp(matrix, same3x3, sizeof(same3x3))) {
-                    s->copy[i] = 1;
-                } else {
-                    s->filter[i] = filter_3x3;
-                    s->copy[i] = 0;
-                }
+                s->filter[i] = filter_3x3;
+                s->copy[i] = !memcmp(matrix, same3x3, sizeof(same3x3));
                 s->setup[i] = setup_3x3;
             } else if (s->matrix_length[i] == 25) {
                 s->size[i] = 5;
-                if (!memcmp(matrix, same5x5, sizeof(same5x5))) {
-                    s->copy[i] = 1;
-                } else {
-                    s->filter[i] = filter_5x5;
-                    s->copy[i] = 0;
-                }
+                s->filter[i] = filter_5x5;
+                s->copy[i] = !memcmp(matrix, same5x5, sizeof(same5x5));
                 s->setup[i] = setup_5x5;
             } else if (s->matrix_length[i] == 49) {
                 s->size[i] = 7;
-                if (!memcmp(matrix, same7x7, sizeof(same7x7))) {
-                    s->copy[i] = 1;
-                } else {
-                    s->filter[i] = filter_7x7;
-                    s->copy[i] = 0;
-                }
+                s->filter[i] = filter_7x7;
+                s->copy[i] = !memcmp(matrix, same7x7, sizeof(same7x7));
                 s->setup[i] = setup_7x7;
             } else {
                 return AVERROR(EINVAL);
-- 
2.54.0


>From eef3d3f688a3e1b09f1bdbb72e64e801e6fb0135 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 20:06:12 +0100
Subject: [PATCH 05/18] avfilter/vf_convolution: filter with the mode the
 functions were selected for

Fixes: out of array write
Fixes: JDGDab47U8W3

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

Out of array write Replicated through UnModified FFmpeg with ASAN
no further issues found with 3000 iterations of a random sendcmd command fuzzer
---
 libavfilter/convolution.h    | 1 +
 libavfilter/vf_convolution.c | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/libavfilter/convolution.h b/libavfilter/convolution.h
index 71ec3b6804e..f26140e29c9 100644
--- a/libavfilter/convolution.h
+++ b/libavfilter/convolution.h
@@ -38,6 +38,7 @@ typedef struct ConvolutionContext {
     float user_rdiv[4];
     float bias[4];
     int mode[4];
+    int active_mode[4];
     float scale;
     float delta;
     int planes;
diff --git a/libavfilter/vf_convolution.c b/libavfilter/vf_convolution.c
index b222b46e2db..6b3291b7979 100644
--- a/libavfilter/vf_convolution.c
+++ b/libavfilter/vf_convolution.c
@@ -570,7 +570,7 @@ static int filter_slice(AVFilterContext *ctx, void *arg, 
int jobnr, int nb_jobs)
     int plane;
 
     for (plane = 0; plane < s->nb_planes; plane++) {
-        const int mode = s->mode[plane];
+        const int mode = s->active_mode[plane];
         const int bpc = s->bpc;
         const int radius = s->size[plane] / 2;
         const int height = s->planeheight[plane];
@@ -710,6 +710,7 @@ static int param_init(AVFilterContext *ctx)
             } else {
                 return AVERROR(EINVAL);
             }
+            s->active_mode[i] = s->mode[i];
 
             if (sum == 0)
                 sum = 1;
-- 
2.54.0


>From 30b74f78f040c15f1964dca9999bfddd99e77511 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:23:06 +0100
Subject: [PATCH 06/18] avfilter/vf_convolution: do not keep the copy shortcut
 for row and column matrices

A plane whose identity square matrix was replaced by a row or column
matrix through commands stayed unfiltered when rdiv was 1 and bias 0.
---
 libavfilter/vf_convolution.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libavfilter/vf_convolution.c b/libavfilter/vf_convolution.c
index 6b3291b7979..a449461b5d1 100644
--- a/libavfilter/vf_convolution.c
+++ b/libavfilter/vf_convolution.c
@@ -684,6 +684,7 @@ static int param_init(AVFilterContext *ctx)
                 }
             }
 
+            s->copy[i] = 0;
             if (s->mode[i] == MATRIX_ROW) {
                 s->filter[i] = filter_row;
                 s->setup[i] = setup_row;
-- 
2.54.0


>From 5d644724f54026560b0a2962a891be58717b34fa Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:25:28 +0100
Subject: [PATCH 07/18] avfilter/vf_drawtext: keep the parsed expressions when
 the new ones fail to parse

Fixes: NULL pointer dereference
Fixes: h6dDBZrc7mdt

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

NULL pointer dereference Replicated through UnModified FFmpeg with ASAN
---
 libavfilter/vf_drawtext.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
index 2ccb03600e9..f17ce664949 100644
--- a/libavfilter/vf_drawtext.c
+++ b/libavfilter/vf_drawtext.c
@@ -1150,6 +1150,7 @@ static int config_input(AVFilterLink *inlink)
 {
     AVFilterContext *ctx = inlink->dst;
     DrawTextContext *s = ctx->priv;
+    AVExpr *x_pexpr = NULL, *y_pexpr = NULL, *a_pexpr = NULL;
     char *expr;
     int ret;
 
@@ -1175,20 +1176,24 @@ static int config_input(AVFilterLink *inlink)
 
     av_lfg_init(&s->prng, av_get_random_seed());
 
+    if ((ret = av_expr_parse(&x_pexpr, expr = s->x_expr, var_names,
+                             NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
+        (ret = av_expr_parse(&y_pexpr, expr = s->y_expr, var_names,
+                             NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
+        (ret = av_expr_parse(&a_pexpr, expr = s->a_expr, var_names,
+                             NULL, NULL, fun2_names, fun2, 0, ctx)) < 0) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to parse expression: %s \n", expr);
+        av_expr_free(x_pexpr);
+        av_expr_free(y_pexpr);
+        return AVERROR(EINVAL);
+    }
+
     av_expr_free(s->x_pexpr);
     av_expr_free(s->y_pexpr);
     av_expr_free(s->a_pexpr);
-    s->x_pexpr = s->y_pexpr = s->a_pexpr = NULL;
-
-    if ((ret = av_expr_parse(&s->x_pexpr, expr = s->x_expr, var_names,
-                             NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
-        (ret = av_expr_parse(&s->y_pexpr, expr = s->y_expr, var_names,
-                             NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
-        (ret = av_expr_parse(&s->a_pexpr, expr = s->a_expr, var_names,
-                             NULL, NULL, fun2_names, fun2, 0, ctx)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to parse expression: %s \n", expr);
-        return AVERROR(EINVAL);
-    }
+    s->x_pexpr = x_pexpr;
+    s->y_pexpr = y_pexpr;
+    s->a_pexpr = a_pexpr;
 
     return 0;
 }
-- 
2.54.0


>From c6c4db9f7b7b3553745b15c38e152169c9e43bab Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:24:33 +0100
Subject: [PATCH 08/18] avfilter/vf_pseudocolor: restore the index when a
 command fails

Fixes: out of array read
Fixes: NULL pointer dereference
Fixes: leN4LX7HLBxe

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

Out of array read and NULL pointer dereference Replicated through UnModified 
FFmpeg with ASAN
---
 libavfilter/vf_pseudocolor.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/libavfilter/vf_pseudocolor.c b/libavfilter/vf_pseudocolor.c
index 793278ce89f..e2ffa7b2570 100644
--- a/libavfilter/vf_pseudocolor.c
+++ b/libavfilter/vf_pseudocolor.c
@@ -1025,12 +1025,18 @@ static int filter_frame(AVFilterLink *inlink, AVFrame 
*in)
 static int process_command(AVFilterContext *ctx, const char *cmd, const char 
*args,
                            char *res, int res_len, int flags)
 {
+    PseudoColorContext *s = ctx->priv;
+    int index = s->index;
     int ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
 
     if (ret < 0)
         return ret;
 
-    return config_input(ctx->inputs[0]);
+    ret = config_input(ctx->inputs[0]);
+    if (ret < 0)
+        s->index = index;
+
+    return ret;
 }
 
 static const AVFilterPad inputs[] = {
-- 
2.54.0


>From 50ec1983279983c04611858a15bfc5af10a870b8 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:25:37 +0100
Subject: [PATCH 09/18] avfilter/vf_drawtext: configure the reinit context
 before releasing the old one

Fixes: NULL pointer dereference
Fixes: bFIbSGRFo5ag

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

NULL pointer dereference Replicated through UnModified FFmpeg with ASAN
---
 libavfilter/vf_drawtext.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
index f17ce664949..761d725c424 100644
--- a/libavfilter/vf_drawtext.c
+++ b/libavfilter/vf_drawtext.c
@@ -1222,6 +1222,8 @@ static int command(AVFilterContext *ctx, const char *cmd, 
const char *arg, char
         }
 
         ret = init(ctx);
+        if (ret >= 0)
+            ret = config_input(ctx->inputs[0]);
         if (ret < 0) {
             uninit(ctx);
             ctx->priv = old;
@@ -1236,7 +1238,7 @@ static int command(AVFilterContext *ctx, const char *cmd, 
const char *arg, char
         av_freep(&old);
 
         ctx->priv = new;
-        return config_input(ctx->inputs[0]);
+        return 0;
     } else {
         int old_borderw = old->borderw;
         if ((ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, 
flags)) < 0) {
-- 
2.54.0


>From 302040f0ae030ed2bf83d95e1f94a2c418e1fdc5 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:26:22 +0100
Subject: [PATCH 10/18] avfilter/vf_colorcorrect: select the analyze function
 from the current mode

Fixes: NULL pointer dereference
Fixes: OnakM8KVAots

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

NULL pointer dereference Replicated through UnModified FFmpeg with ASAN
---
 libavfilter/vf_colorcorrect.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)

diff --git a/libavfilter/vf_colorcorrect.c b/libavfilter/vf_colorcorrect.c
index b6c3d043313..5688b8860cb 100644
--- a/libavfilter/vf_colorcorrect.c
+++ b/libavfilter/vf_colorcorrect.c
@@ -55,8 +55,6 @@ typedef struct ColorCorrectContext {
 
     float (*analyzeret)[4];
 
-    int (*do_analyze)(AVFilterContext *s, void *arg,
-                      int jobnr, int nb_jobs);
     int (*do_slice)(AVFilterContext *s, void *arg,
                     int jobnr, int nb_jobs);
 } ColorCorrectContext;
@@ -393,6 +391,13 @@ static int colorcorrect_slice16(AVFilterContext *ctx, void 
*arg, int jobnr, int
     return 0;
 }
 
+static int (*const analyze_slice[NB_ANALYZE][2])(AVFilterContext *s, void *arg,
+                                                 int jobnr, int nb_jobs) = {
+    [AVERAGE] = { average_slice8, average_slice16 },
+    [MINMAX]  = { minmax_slice8,  minmax_slice16  },
+    [MEDIAN]  = { median_8,       median_16       },
+};
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     AVFilterContext *ctx = inlink->dst;
@@ -403,7 +408,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame 
*frame)
         const int nb_athreads = s->analyze == MEDIAN ? 1 : nb_threads;
         float bl = 0.f, rl = 0.f, bh = 0.f, rh = 0.f;
 
-        ff_filter_execute(ctx, s->do_analyze, frame, NULL, nb_athreads);
+        ff_filter_execute(ctx, analyze_slice[s->analyze][s->depth > 8], frame, 
NULL, nb_athreads);
 
         for (int i = 0; i < nb_athreads; i++) {
             bl += s->analyzeret[i][0];
@@ -467,22 +472,6 @@ static av_cold int config_input(AVFilterLink *inlink)
     if (!s->analyzeret)
         return AVERROR(ENOMEM);
 
-    switch (s->analyze) {
-    case MANUAL:
-        break;
-    case AVERAGE:
-        s->do_analyze = s->depth <= 8 ? average_slice8 : average_slice16;
-        break;
-    case MINMAX:
-        s->do_analyze = s->depth <= 8 ? minmax_slice8  : minmax_slice16;
-        break;
-    case MEDIAN:
-        s->do_analyze = s->depth <= 8 ? median_8       : median_16;
-        break;
-    default:
-        return AVERROR_BUG;
-    }
-
     s->chroma_w = 1 << desc->log2_chroma_w;
     s->chroma_h = 1 << desc->log2_chroma_h;
     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, 
desc->log2_chroma_h);
-- 
2.54.0


>From 2a4c397047327a3b728f772269466839c8c823ff Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:27:14 +0100
Subject: [PATCH 11/18] avfilter/vf_median: limit the number of jobs by the
 current radiusV

Fixes: out of array read
Fixes: NidYeIfje6hU

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

Out of array read Replicated through UnModified FFmpeg with ASAN
---
 libavfilter/vf_median.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libavfilter/vf_median.c b/libavfilter/vf_median.c
index 53989324362..aeb5d09e3a4 100644
--- a/libavfilter/vf_median.c
+++ b/libavfilter/vf_median.c
@@ -230,7 +230,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     av_frame_copy_props(out, in);
 
     td.in = in; td.out = out;
-    ff_filter_execute(ctx, filter_slice, &td, NULL, s->nb_threads);
+    ff_filter_execute(ctx, filter_slice, &td, NULL,
+                      av_clip(s->planeheight[1] / (s->radiusV + 1), 1, 
s->nb_threads));
 
     av_frame_free(&in);
     return ff_filter_frame(outlink, out);
-- 
2.54.0


>From 3403417cfe5b29e15cf7003cb4370bd42d5ea256 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:30:41 +0100
Subject: [PATCH 12/18] avfilter/af_adelay: rebuild the delay line when the
 delay is changed

Fixes: out of array write
Fixes: negative size memmove
Fixes: juZLhjrRs7X8

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

Out of array write and negative size memmove Replicated through UnModified 
FFmpeg with ASAN
---
 libavfilter/af_adelay.c | 44 ++++++++++++++++-------------------------
 1 file changed, 17 insertions(+), 27 deletions(-)

diff --git a/libavfilter/af_adelay.c b/libavfilter/af_adelay.c
index 0ce65e7b9b6..ae86d5191b8 100644
--- a/libavfilter/af_adelay.c
+++ b/libavfilter/af_adelay.c
@@ -30,7 +30,6 @@ typedef struct ChanDelay {
     int64_t delay;
     size_t delay_index;
     size_t index;
-    unsigned int samples_size;
     uint8_t *samples;
 } ChanDelay;
 
@@ -103,6 +102,10 @@ DELAY(dbl, double,  0)
 #define CHANGE_DELAY(name, type, fill)                                         
                         \
 static int resize_samples_## name ##p(ChanDelay *d, int64_t new_delay)         
                         \
 {                                                                              
                         \
+    const size_t nb_stored = FFMIN(d->delay_index, d->delay);                  
                         \
+    const size_t nb_kept = FFMIN(nb_stored, new_delay);                        
                         \
+    const size_t first_kept = d->index + nb_stored - nb_kept;                  
                         \
+    const type *old_samples = (const type *)d->samples;                        
                         \
     type *samples;                                                             
                         \
                                                                                
                         \
     if (new_delay == d->delay) {                                               
                         \
@@ -111,42 +114,30 @@ static int resize_samples_## name ##p(ChanDelay *d, 
int64_t new_delay)
                                                                                
                         \
     if (new_delay == 0) {                                                      
                         \
         av_freep(&d->samples);                                                 
                         \
-        d->samples_size = 0;                                                   
                         \
         d->delay = 0;                                                          
                         \
         d->index = 0;                                                          
                         \
         d->delay_index = 0;                                                    
                         \
         return 0;                                                              
                         \
     }                                                                          
                         \
                                                                                
                         \
-    samples = (type *) av_fast_realloc(d->samples, &d->samples_size, new_delay 
* sizeof(type));         \
+    if (new_delay > SIZE_MAX) {                                                
                         \
+        return AVERROR(ENOMEM);                                                
                         \
+    }                                                                          
                         \
+                                                                               
                         \
+    samples = av_malloc_array(new_delay, sizeof(type));                        
                         \
     if (!samples) {                                                            
                         \
         return AVERROR(ENOMEM);                                                
                         \
     }                                                                          
                         \
                                                                                
                         \
-    if (new_delay < d->delay) {                                                
                         \
-        if (d->index > new_delay) {                                            
                         \
-            d->index -= new_delay;                                             
                         \
-            memmove(samples, &samples[new_delay], d->index * sizeof(type));    
                         \
-            d->delay_index = new_delay;                                        
                         \
-        } else if (d->delay_index > d->index) {                                
                         \
-            memmove(&samples[d->index], 
&samples[d->index+(d->delay-new_delay)],                        \
-                    (new_delay - d->index) * sizeof(type));                    
                         \
-            d->delay_index -= d->delay - new_delay;                            
                         \
-        }                                                                      
                         \
-    } else {                                                                   
                         \
-        size_t block_size;                                                     
                         \
-        if (d->delay_index >= d->delay) {                                      
                         \
-            block_size = (d->delay - d->index) * sizeof(type);                 
                         \
-            memmove(&samples[d->index+(new_delay - d->delay)], 
&samples[d->index], block_size);         \
-            d->delay_index = new_delay;                                        
                         \
-        } else {                                                               
                         \
-            d->delay_index += new_delay - d->delay;                            
                         \
-        }                                                                      
                         \
-        block_size = (new_delay - d->delay) * sizeof(type);                    
                         \
-        memset(&samples[d->index], fill, block_size);                          
                         \
-    }                                                                          
                         \
-    d->delay = new_delay;                                                      
                         \
+    memset(samples, fill, (new_delay - nb_kept) * sizeof(type));               
                         \
+    for (size_t i = 0; i < nb_kept; i++)                                       
                         \
+        samples[new_delay - nb_kept + i] = old_samples[(first_kept + i) % 
nb_stored];                   \
+                                                                               
                         \
+    av_free(d->samples);                                                       
                         \
     d->samples = (void *) samples;                                             
                         \
+    d->delay = new_delay;                                                      
                         \
+    d->delay_index = new_delay;                                                
                         \
+    d->index = 0;                                                              
                         \
     return 0;                                                                  
                         \
 }
 
@@ -260,7 +251,6 @@ static int config_input(AVFilterLink *inlink)
         d->samples = av_malloc_array(d->delay, s->block_align);
         if (!d->samples)
             return AVERROR(ENOMEM);
-        d->samples_size = d->delay * s->block_align;
 
         s->max_delay = FFMAX(s->max_delay, d->delay);
     }
-- 
2.54.0


>From 73dca4e4a5a8caba7752348307c27becd6b6a686 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:31:17 +0100
Subject: [PATCH 13/18] avfilter/vf_yaepblur: clamp the radius of each frame

Fixes: out of array read
Fixes: signed integer overflow
Fixes: tdbi5LrAh1Pq

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

Out of array read Replicated through UnModified FFmpeg with ASAN
Signed integer overflow Replicated through UnModified FFmpeg with UBSAN
---
 libavfilter/vf_yaepblur.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavfilter/vf_yaepblur.c b/libavfilter/vf_yaepblur.c
index 59f3501f35e..748a2f7a581 100644
--- a/libavfilter/vf_yaepblur.c
+++ b/libavfilter/vf_yaepblur.c
@@ -224,6 +224,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     const int nb_threads = ff_filter_get_nb_threads(ctx);
     ThreadData td;
 
+    s->radius = FFMIN(s->radius, AV_CEIL_RSHIFT(FFMIN(inlink->w, inlink->h), 
1));
+
     if (av_frame_is_writable(in)) {
         out = in;
     } else {
@@ -279,8 +281,6 @@ static int config_input(AVFilterLink *inlink)
     s->planeheight[0] = s->planeheight[3] = inlink->h;
     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
 
-    s->radius = FFMIN(s->radius, AV_CEIL_RSHIFT(FFMIN(inlink->w, inlink->h), 
1));
-
     if (s->depth <= 8) {
         s->pre_calculate_row = pre_calculate_row_byte;
         s->filter_slice      = filter_slice_byte;
-- 
2.54.0


>From 96fae05b77337643ade8cae2e85555ff23a11684 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 01:31:47 +0100
Subject: [PATCH 14/18] avfilter/af_ladspa: check the control index before
 using it

Fixes: out of array read
Fixes: okPC857hfRXh

Found during the triage of a finding from the review of 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25800

Out of array read Replicated through UnModified FFmpeg with ASAN
---
 libavfilter/af_ladspa.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libavfilter/af_ladspa.c b/libavfilter/af_ladspa.c
index ea5eb1a2efe..cf51c8bea59 100644
--- a/libavfilter/af_ladspa.c
+++ b/libavfilter/af_ladspa.c
@@ -452,8 +452,7 @@ static int set_control(AVFilterContext *ctx, unsigned long 
port, LADSPA_Data val
 {
     LADSPAContext *s = ctx->priv;
     const char *label = s->desc->Label;
-    LADSPA_PortRangeHint *h = (LADSPA_PortRangeHint *)s->desc->PortRangeHints +
-                              s->icmap[port];
+    LADSPA_PortRangeHint *h;
 
     if (port >= s->nb_inputcontrols) {
         av_log(ctx, AV_LOG_ERROR, "Control c%ld is out of range [0 - %lu].\n",
@@ -461,6 +460,8 @@ static int set_control(AVFilterContext *ctx, unsigned long 
port, LADSPA_Data val
         return AVERROR(EINVAL);
     }
 
+    h = (LADSPA_PortRangeHint *)s->desc->PortRangeHints + s->icmap[port];
+
     if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) &&
             value < h->LowerBound) {
         av_log(ctx, AV_LOG_ERROR,
-- 
2.54.0


>From 7b3a9a01495b755ae912649a6b68328825dc9279 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 02:00:43 +0100
Subject: [PATCH 15/18] avfilter/vf_drawtext: free the options of the new
 context when reinit fails

Fixes: memory leak
---
 libavfilter/vf_drawtext.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
index 761d725c424..ff3eefa3ae1 100644
--- a/libavfilter/vf_drawtext.c
+++ b/libavfilter/vf_drawtext.c
@@ -1259,6 +1259,7 @@ static int command(AVFilterContext *ctx, const char *cmd, 
const char *arg, char
 
 fail:
     av_log(ctx, AV_LOG_ERROR, "Failed to process command. Continuing with 
existing parameters.\n");
+    av_opt_free(new);
     av_freep(&new);
     return ret;
 }
-- 
2.54.0


>From 0bbbad6638cb88b6e5ebfed2cc824da5d29362ae Mon Sep 17 00:00:00 2001
From: Qingzheng Li <[email protected]>
Date: Thu, 6 Aug 2026 01:22:57 +0800
Subject: [PATCH 16/18] avfilter/swaprect: free the input frame when an
 expression fails

Route expression failures through a common cleanup path,
since filter_frame() owns the input frame until it is forwarded.

Fixes: memory leak
Signed-off-by: Qingzheng Li <[email protected]>
---
 libavfilter/vf_swaprect.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/libavfilter/vf_swaprect.c b/libavfilter/vf_swaprect.c
index fe007ee5e77..c56cf6bca08 100644
--- a/libavfilter/vf_swaprect.c
+++ b/libavfilter/vf_swaprect.c
@@ -101,42 +101,42 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
                                  NULL, NULL, NULL, NULL,
                                  0, 0, ctx);
     if (ret < 0)
-        return ret;
+        goto fail;
 
     ret = av_expr_parse_and_eval(&dh, s->h,
                                  var_names, &var_values[0],
                                  NULL, NULL, NULL, NULL,
                                  0, 0, ctx);
     if (ret < 0)
-        return ret;
+        goto fail;
 
     ret = av_expr_parse_and_eval(&dx1, s->x1,
                                  var_names, &var_values[0],
                                  NULL, NULL, NULL, NULL,
                                  0, 0, ctx);
     if (ret < 0)
-        return ret;
+        goto fail;
 
     ret = av_expr_parse_and_eval(&dy1, s->y1,
                                  var_names, &var_values[0],
                                  NULL, NULL, NULL, NULL,
                                  0, 0, ctx);
     if (ret < 0)
-        return ret;
+        goto fail;
 
     ret = av_expr_parse_and_eval(&dx2, s->x2,
                                  var_names, &var_values[0],
                                  NULL, NULL, NULL, NULL,
                                  0, 0, ctx);
     if (ret < 0)
-        return ret;
+        goto fail;
 
     ret = av_expr_parse_and_eval(&dy2, s->y2,
                                  var_names, &var_values[0],
                                  NULL, NULL, NULL, NULL,
                                  0, 0, ctx);
     if (ret < 0)
-        return ret;
+        goto fail;
 
     w = dw; h = dh; x1[0] = dx1; y1[0] = dy1; x2[0] = dx2; y2[0] = dy2;
 
@@ -194,6 +194,10 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     }
 
     return ff_filter_frame(outlink, in);
+
+fail:
+    av_frame_free(&in);
+    return ret;
 }
 
 static int config_input(AVFilterLink *inlink)
-- 
2.54.0


>From e40a08c1961e601c728b826f133f2f3d07481c55 Mon Sep 17 00:00:00 2001
From: Qingzheng Li <[email protected]>
Date: Thu, 6 Aug 2026 01:22:57 +0800
Subject: [PATCH 17/18] avfilter/swaprect: validate rectangle expression
 results

swaprect evaluates its dimensions and coordinates as doubles before
converting them to int. Non-finite or out-of-range results make that
conversion undefined, and negative dimensions can later become invalid
copy sizes.

Truncate each of the six results before checking the int range, preserving
the filter's existing conversion semantics. Reject non-finite and
unrepresentable results, then validate the converted dimensions so values
such as 0.5 cannot become zero-sized rectangles.

Negative size memcpy Replicated through UnModified FFmpeg with ASAN

Fixes: #22866
Fixes: negative size memcpy
Fixes: 2eqsjEd0TfU4
Reported-by: cxxz16
Based-on-patch-by: un-tag <[email protected]>
---
 libavfilter/vf_swaprect.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/libavfilter/vf_swaprect.c b/libavfilter/vf_swaprect.c
index c56cf6bca08..599ba2d440d 100644
--- a/libavfilter/vf_swaprect.c
+++ b/libavfilter/vf_swaprect.c
@@ -18,6 +18,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <math.h>
+
 #include "libavutil/avassert.h"
 #include "libavutil/eval.h"
 #include "libavutil/imgutils.h"
@@ -71,6 +73,19 @@ static int query_formats(const AVFilterContext *ctx,
 static const char *const var_names[] = {   "w",   "h",   "a",   "n",   "t",   
"sar",   "dar",        NULL };
 enum                                   { VAR_W, VAR_H, VAR_A, VAR_N, VAR_T, 
VAR_SAR, VAR_DAR, VAR_VARS_NB };
 
+static int trunc_double_to_int(int *dst, double value)
+{
+    if (!isfinite(value))
+        return AVERROR(EINVAL);
+
+    value = trunc(value);
+    if (value < INT_MIN || value > INT_MAX)
+        return AVERROR(EINVAL);
+
+    *dst = value;
+    return 0;
+}
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     FilterLink *inl = ff_filter_link(inlink);
@@ -138,7 +153,23 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     if (ret < 0)
         goto fail;
 
-    w = dw; h = dh; x1[0] = dx1; y1[0] = dy1; x2[0] = dx2; y2[0] = dy2;
+    if (trunc_double_to_int(&w, dw)       < 0 ||
+        trunc_double_to_int(&h, dh)       < 0 ||
+        trunc_double_to_int(&x1[0], dx1)  < 0 ||
+        trunc_double_to_int(&y1[0], dy1)  < 0 ||
+        trunc_double_to_int(&x2[0], dx2)  < 0 ||
+        trunc_double_to_int(&y2[0], dy2)  < 0) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Rectangle expression result is not representable as an 
integer.\n");
+        ret = AVERROR(EINVAL);
+        goto fail;
+    }
+
+    if (w <= 0 || h <= 0) {
+        av_log(ctx, AV_LOG_ERROR, "Rectangle dimensions must be positive.\n");
+        ret = AVERROR(EINVAL);
+        goto fail;
+    }
 
     x1[0] = av_clip(x1[0], 0, inlink->w - 1);
     y1[0] = av_clip(y1[0], 0, inlink->h - 1);
-- 
2.54.0


>From 72c0a6c98212fed508d7e585faac7dc8b72e08a0 Mon Sep 17 00:00:00 2001
From: Qingzheng Li <[email protected]>
Date: Thu, 6 Aug 2026 01:23:33 +0800
Subject: [PATCH 18/18] fate/filter-video: test invalid swaprect expressions

Cover negative and sub-unit dimensions, as well as finite out-of-range
and infinite coordinates.

Require the expected EINVAL process status and diagnostic so a crash does
not satisfy the rejection tests.

Based-on-patch-by: un-tag <[email protected]>
---
 tests/fate/filter-video.mak | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
index 82ab48dc68d..ba52075a89f 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -393,6 +393,25 @@ fate-filter-swaprect: CMD = framecrc -c:v pgmyuv -i $(SRC) 
-vf swaprect
 
 FATE_FILTER_VSYNTH_PGMYUV-$(CONFIG_SWAPRECT_FILTER) += $(FATE_SWAPRECT)
 
+FATE_FILTER-$(call ALLYES, COLOR_FILTER LAVFI_INDEV SWAPRECT_FILTER \
+                              WRAPPED_AVFRAME_DECODER WRAPPED_AVFRAME_ENCODER \
+                              NULL_MUXER) += fate-filter-swaprect-invalid-size 
\
+                                             
fate-filter-swaprect-invalid-fractional-size \
+                                             
fate-filter-swaprect-invalid-coordinate \
+                                             
fate-filter-swaprect-nonfinite-coordinate
+fate-filter-swaprect-invalid-size: CMD = run $(FFMPEG) -nostdin -hide_banner 
-f lavfi -i "color=c=black:s=16x16:r=1" -vf 
"swaprect=w=-1:h=1:x1=0:y1=0:x2=1:y2=0" -frames:v 1 -f null none ; ret=$$?; 
test $$ret -eq 234
+fate-filter-swaprect-invalid-size: CMP = grep
+fate-filter-swaprect-invalid-size: REF = Rectangle dimensions must be positive
+fate-filter-swaprect-invalid-fractional-size: CMD = run $(FFMPEG) -nostdin 
-hide_banner -f lavfi -i "color=c=black:s=16x16:r=1" -vf 
"swaprect=w=0.5:h=1:x1=0:y1=0:x2=1:y2=0" -frames:v 1 -f null none ; ret=$$?; 
test $$ret -eq 234
+fate-filter-swaprect-invalid-fractional-size: CMP = grep
+fate-filter-swaprect-invalid-fractional-size: REF = Rectangle dimensions must 
be positive
+fate-filter-swaprect-invalid-coordinate: CMD = run $(FFMPEG) -nostdin 
-hide_banner -f lavfi -i "color=c=black:s=16x16:r=1" -vf 
"swaprect=w=1:h=1:x1=1e100:y1=0:x2=1:y2=0" -frames:v 1 -f null none ; ret=$$?; 
test $$ret -eq 234
+fate-filter-swaprect-invalid-coordinate: CMP = grep
+fate-filter-swaprect-invalid-coordinate: REF = Rectangle expression result is 
not representable as an integer
+fate-filter-swaprect-nonfinite-coordinate: CMD = run $(FFMPEG) -nostdin 
-hide_banner -f lavfi -i "color=c=black:s=16x16:r=1" -vf 
"swaprect=w=1:h=1:x1=1/0:y1=0:x2=1:y2=0" -frames:v 1 -f null none ; ret=$$?; 
test $$ret -eq 234
+fate-filter-swaprect-nonfinite-coordinate: CMP = grep
+fate-filter-swaprect-nonfinite-coordinate: REF = Rectangle expression result 
is not representable as an integer
+
 FATE_FILTER_VSYNTH_PGMYUV-$(CONFIG_TBLEND_FILTER) += fate-filter-tblend
 fate-filter-tblend: CMD = framecrc -c:v pgmyuv -i $(SRC) -vf 
tblend=all_mode=difference128
 
-- 
2.54.0

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

Reply via email to