PR #21814 opened by Niklas Haas (haasn)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21814
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21814.patch

Restructure the loop slightly to avoid UB in the first loop iteration if
src is 4 bytes, which otherwise computes (0 << 32) | 1. Instead, make
1 the default base case and only shift+add if src < dst.


>From ca0e3c6b9281d559ea52cacdc8ba7260b85d1e41 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Fri, 20 Feb 2026 16:02:32 +0100
Subject: [PATCH] swscale/ops: avoid UB in ff_sws_pixel_expand()

Restructure the loop slightly to avoid UB in the first loop iteration if
src is 4 bytes, which otherwise computes (0 << 32) | 1. Instead, make
1 the default base case and only shift+add if src < dst.
---
 libswscale/ops_internal.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libswscale/ops_internal.h b/libswscale/ops_internal.h
index ba4c9b39da..c2e23f664a 100644
--- a/libswscale/ops_internal.h
+++ b/libswscale/ops_internal.h
@@ -31,9 +31,9 @@ static inline AVRational ff_sws_pixel_expand(SwsPixelType 
from, SwsPixelType to)
 {
     const int src = ff_sws_pixel_type_size(from);
     const int dst = ff_sws_pixel_type_size(to);
-    int scale = 0;
-    for (int i = 0; i < dst / src; i++)
-        scale = scale << src * 8 | 1;
+    int scale = 1;
+    for (int i = 1; i < dst / src; i++)
+        scale = (scale << (src * 8)) | 1;
     return Q(scale);
 }
 
-- 
2.52.0

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

Reply via email to