Commit 3cfb702389 for ffmpeg

commit 3cfb7023896e01d34d426b8130080d1d943eaff6
Author: Qingzheng Li <s@qwqlog.com>
Date:   Thu Aug 6 01:22:57 2026 +0800

    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 <roosevelthonaker@gmail.com>

diff --git a/libavfilter/vf_swaprect.c b/libavfilter/vf_swaprect.c
index c56cf6bca0..599ba2d440 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);