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


>From 4940f4ffc2a5591a33db804cad3e3f424703641c Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 11 Nov 2026 21:17:27 +0100
Subject: [PATCH 1/5] swscale/x86/yuv2rgb: do not write past the width in the
 SSSE3 converters

Fixes: out of array write
Fixes: v6YVGm0pHn7P
Out of array write Replicated through API with Valgrind

Found during triage of the security report bKTdZBAokzK8
---
 libswscale/x86/yuv2rgb.c     | 11 ++++-------
 libswscale/x86/yuv_2_rgb.asm | 15 ++++++++++++++-
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/libswscale/x86/yuv2rgb.c b/libswscale/x86/yuv2rgb.c
index 2291c84e53b..31487385211 100644
--- a/libswscale/x86/yuv2rgb.c
+++ b/libswscale/x86/yuv2rgb.c
@@ -41,9 +41,7 @@
 #if HAVE_X86ASM
 
 #define YUV2RGB_LOOP(depth)                                          \
-    h_size = (c->opts.dst_w + 7) & ~7;                               \
-    if (h_size * depth > FFABS(dstStride[0]))                        \
-        h_size -= 8;                                                 \
+    h_size = c->opts.dst_w;                                          \
                                                                      \
     vshift = c->opts.src_format != AV_PIX_FMT_YUV422P;               \
                                                                      \
@@ -215,9 +213,7 @@ static inline int yuv420_gbrp_ssse3(SwsInternal *c, const 
uint8_t *const src[],
 {
     int y, h_size, vshift;
 
-    h_size = (c->opts.dst_w + 7) & ~7;
-    if (h_size > FFABS(dstStride[0]))
-        h_size -= 8;
+    h_size = c->opts.dst_w;
 
     vshift = c->opts.src_format != AV_PIX_FMT_YUV422P;
 
@@ -243,7 +239,8 @@ av_cold SwsFunc ff_yuv2rgb_init_x86(SwsInternal *c)
 #if HAVE_X86ASM
     int cpu_flags = av_get_cpu_flags();
 
-    if (EXTERNAL_SSSE3(cpu_flags)) {
+    /* the converters need at least one block of 16 pixels and an even width */
+    if (EXTERNAL_SSSE3(cpu_flags) && c->opts.dst_w >= 16 && !(c->opts.dst_w & 
1)) {
         switch (c->opts.dst_format) {
         case AV_PIX_FMT_RGB32:
             if (c->opts.src_format == AV_PIX_FMT_YUVA420P) {
diff --git a/libswscale/x86/yuv_2_rgb.asm b/libswscale/x86/yuv_2_rgb.asm
index eeb1d259420..353a2e21ca5 100644
--- a/libswscale/x86/yuv_2_rgb.asm
+++ b/libswscale/x86/yuv_2_rgb.asm
@@ -296,8 +296,21 @@ cglobal %1_420_%2%3, GPR_num, GPR_num, reg_num, parameters
 
 add imageq, 8 * depth * time_num
 add indexq, 4 * time_num
-js .loop0
+jns .end
+cmp indexq, -4 * time_num
+jle .loop0
+; less than a block is left, convert the last block of the row
+add indexq, 4 * time_num
+imul indexq, 2 * depth
+sub imageq, indexq
+%ifidn %2, gbrp
+sub dst_bq, indexq
+sub dst_rq, indexq
+%endif
+mov indexq, -4 * time_num
+jmp .loop0
 
+.end:
 RET
 
 %endmacro
-- 
2.54.0


>From b71ffeec9e081b0acc3cb056a45e65c3f1bb6cb2 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 23:08:23 +0100
Subject: [PATCH 2/5] swscale: do nothing for an output slice of height 0

Fixes: out of array write
Fixes: k8Ars6WKgFA6
Out of array write Replicated through API with Valgrind

Found during review of the fixes for the security report bKTdZBAokzK8
---
 libswscale/swscale.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libswscale/swscale.c b/libswscale/swscale.c
index 90a9a40d786..712389c27bb 100644
--- a/libswscale/swscale.c
+++ b/libswscale/swscale.c
@@ -1096,7 +1096,7 @@ static int scale_internal(SwsContext *sws,
     }
 
     // do not mess up sliceDir if we have a "trailing" 0-size slice
-    if (srcSliceH == 0)
+    if (srcSliceH == 0 || dstSliceH == 0)
         return 0;
 
     if (sws->gamma_flag && c->cascaded_context[0])
-- 
2.54.0


>From 26fe4b4fcf8eb51950f39f4c92a8adac71f6ad70 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 19:48:47 +0100
Subject: [PATCH 3/5] swscale/swscale_unscaled: call the RGB converter from one
 place in the row loop

---
 libswscale/swscale_unscaled.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/libswscale/swscale_unscaled.c b/libswscale/swscale_unscaled.c
index ccd96fb204e..7a7b2bb1ee6 100644
--- a/libswscale/swscale_unscaled.c
+++ b/libswscale/swscale_unscaled.c
@@ -2042,12 +2042,14 @@ static int rgbToRgbWrapper(SwsInternal *c, const 
uint8_t *const src[], const int
             dstPtr += dstStride[0] * srcSliceY;
 
             for (i = 0; i < srcSliceH; i++) {
+                const uint8_t *row = srcPtr;
+
                 if(src_bswap) {
                     for(j=0; j<c->opts.src_w; j++)
                         ((uint16_t*)c->formatConvBuffer)[j] = 
av_bswap16(((uint16_t*)srcPtr)[j]);
-                    conv(c->formatConvBuffer, dstPtr, c->opts.src_w * srcBpp);
-                }else
-                    conv(srcPtr, dstPtr, c->opts.src_w * srcBpp);
+                    row = c->formatConvBuffer;
+                }
+                conv(row, dstPtr, c->opts.src_w * srcBpp);
                 if(dst_bswap)
                     for(j=0; j<c->opts.src_w; j++)
                         ((uint16_t*)dstPtr)[j] = 
av_bswap16(((uint16_t*)dstPtr)[j]);
-- 
2.54.0


>From 83035b9c9c75bd2f9fbed2873f62b237fdb558d5 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 11 Nov 2026 19:19:57 +0100
Subject: [PATCH 4/5] swscale/swscale_unscaled: keep RGB to ARGB/ABGR
 conversion inside the plane

Fixes: out of array write
Fixes: cstOiKPYOhNO
Out of array write Replicated through API with Valgrind

Found during triage of the security report bKTdZBAokzK8
---
 libswscale/swscale_unscaled.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/libswscale/swscale_unscaled.c b/libswscale/swscale_unscaled.c
index 7a7b2bb1ee6..f137bcb5c43 100644
--- a/libswscale/swscale_unscaled.c
+++ b/libswscale/swscale_unscaled.c
@@ -1997,6 +1997,13 @@ static rgbConvFn findRgbConvFn(SwsInternal *c)
     return conv;
 }
 
+static inline void rgb_pixel_to_alt32(rgbConvFn conv, const uint8_t *src, 
uint8_t *dst, int src_bpp)
+{
+    uint32_t pixel;
+    conv(src, (uint8_t *)&pixel, src_bpp);
+    AV_WL32(dst, AV_RL32(&pixel) << 8 | 0xFF);
+}
+
 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
 static int rgbToRgbWrapper(SwsInternal *c, const uint8_t *const src[], const 
int srcStride[],
                            int srcSliceY, int srcSliceH, uint8_t *const dst[],
@@ -2019,6 +2026,7 @@ static int rgbToRgbWrapper(SwsInternal *c, const uint8_t 
*const src[], const int
               uint8_t *dstPtr = dst[0];
         int src_bswap = IS_NOT_NE(srcBpp, desc_src);
         int dst_bswap = IS_NOT_NE(dstBpp, desc_dst);
+        int row_ending_plane = -1;
 
         if ((srcFormat == AV_PIX_FMT_RGB32_1 || srcFormat == 
AV_PIX_FMT_BGR32_1) &&
             !isRGBA32(dstFormat))
@@ -2031,25 +2039,35 @@ static int rgbToRgbWrapper(SwsInternal *c, const 
uint8_t *const src[], const int
             for (i = 0; i < srcSliceH; i++)
                 dstPtr[dstStride[0] * (srcSliceY + i)] = 255;
             dstPtr += ALT32_CORR;
+            row_ending_plane = dstStride[0] > 0 ? c->opts.src_h - 1 : 0;
         }
 
         if (dstStride[0] * srcBpp == srcStride[0] * dstBpp && srcStride[0] > 0 
&&
-            !(srcStride[0] % srcBpp) && !dst_bswap && !src_bswap)
-            conv(srcPtr, dstPtr + dstStride[0] * srcSliceY,
-                 (srcSliceH - 1) * srcStride[0] + c->opts.src_w * srcBpp);
-        else {
+            !(srcStride[0] % srcBpp) && !dst_bswap && !src_bswap) {
+            const int last_row = srcSliceY + srcSliceH - 1;
+            const int src_size = (srcSliceH - 1) * srcStride[0] + 
c->opts.src_w * srcBpp;
+            const int cut      = last_row == row_ending_plane ? srcBpp : 0;
+
+            conv(srcPtr, dstPtr + dstStride[0] * srcSliceY, src_size - cut);
+            if (cut)
+                rgb_pixel_to_alt32(conv, srcPtr + src_size - cut,
+                                   dstPtr + dstStride[0] * last_row + 
(c->opts.src_w - 1) * 4 - ALT32_CORR, srcBpp);
+        } else {
             int i, j;
             dstPtr += dstStride[0] * srcSliceY;
 
             for (i = 0; i < srcSliceH; i++) {
                 const uint8_t *row = srcPtr;
+                const int conv_w   = c->opts.src_w - (srcSliceY + i == 
row_ending_plane);
 
                 if(src_bswap) {
                     for(j=0; j<c->opts.src_w; j++)
                         ((uint16_t*)c->formatConvBuffer)[j] = 
av_bswap16(((uint16_t*)srcPtr)[j]);
                     row = c->formatConvBuffer;
                 }
-                conv(row, dstPtr, c->opts.src_w * srcBpp);
+                conv(row, dstPtr, conv_w * srcBpp);
+                if (conv_w < c->opts.src_w)
+                    rgb_pixel_to_alt32(conv, row + conv_w * srcBpp, dstPtr + 
conv_w * 4 - ALT32_CORR, srcBpp);
                 if(dst_bswap)
                     for(j=0; j<c->opts.src_w; j++)
                         ((uint16_t*)dstPtr)[j] = 
av_bswap16(((uint16_t*)dstPtr)[j]);
-- 
2.54.0


>From 947265d7c5aa24fd0ab3aedc57d33711acc0d85e Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 12 Nov 2026 05:17:25 +0100
Subject: [PATCH 5/5] swscale/output: do not store past an odd width in the 16
 bit RGB C functions

Fixes: out of array write
Fixes: bKTdZBAokzK8
Fixes: sws_565_exact.c
Fixes: sws_full_sweep.c
Out of array write Replicated through API with Valgrind

Found-by: Kim Jeongsu with StealthMole
---
 libswscale/output.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/libswscale/output.c b/libswscale/output.c
index 3e9086a3106..8e8ed28d668 100644
--- a/libswscale/output.c
+++ b/libswscale/output.c
@@ -1671,7 +1671,7 @@ YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgbxf32le_full, 
AV_PIX_FMT_RGBAF32, LE, 0
  * correct RGB values into the destination buffer.
  */
 static av_always_inline void
-yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
+yuv2rgb_write(uint8_t *_dest, int i, int dstW, int Y1, int Y2,
               unsigned A1, unsigned A2,
               const void *_r, const void *_g, const void *_b, int y,
               enum AVPixelFormat target, int hasAlpha)
@@ -1755,7 +1755,10 @@ yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
         }
 
         dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
-        dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
+        /* these formats have no full chroma interpolation and so are
+         * output in pairs for odd widths too */
+        if (i * 2 + 1 < dstW)
+            dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
     } else if (target == AV_PIX_FMT_X2RGB10 || target == AV_PIX_FMT_X2BGR10) {
         uint32_t *dest = (uint32_t *) _dest;
         const uint32_t *r = (const uint32_t *) _r;
@@ -1845,7 +1848,7 @@ yuv2rgb_X_c_template(SwsInternal *c, const int16_t 
*lumFilter,
         g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + 
YUVRGB_TABLE_HEADROOM]);
         b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
 
-        yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
+        yuv2rgb_write(dest, i, dstW, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 
: 0,
                       r, g, b, y, target, hasAlpha);
     }
 }
@@ -1885,7 +1888,7 @@ yuv2rgb_2_c_template(SwsInternal *c, const int16_t 
*buf[2],
             A2 = av_clip_uint8(A2);
         }
 
-        yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
+        yuv2rgb_write(dest, i, dstW, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 
: 0,
                       r, g, b, y, target, hasAlpha);
     }
 }
@@ -1918,7 +1921,7 @@ yuv2rgb_1_c_template(SwsInternal *c, const int16_t *buf0,
                 A2 = av_clip_uint8(A2);
             }
 
-            yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 
0,
+            yuv2rgb_write(dest, i, dstW, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? 
A2 : 0,
                           r, g, b, y, target, hasAlpha);
         }
     } else {
@@ -1943,7 +1946,7 @@ yuv2rgb_1_c_template(SwsInternal *c, const int16_t *buf0,
                 A2 = av_clip_uint8(A2);
             }
 
-            yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 
0,
+            yuv2rgb_write(dest, i, dstW, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? 
A2 : 0,
                           r, g, b, y, target, hasAlpha);
         }
     }
-- 
2.54.0

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

Reply via email to