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.

Add an explicit check to preserve the behavior of returnin 0 if src > dst.
This commit is contained in:
Niklas Haas
2026-02-20 16:02:32 +01:00
committed by Niklas Haas
parent f951aa9ef3
commit 5c661dec61

View File

@@ -31,9 +31,11 @@ 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;
if (src > dst)
return Q(0);
int scale = 1;
for (int i = 1; i < dst / src; i++)
scale = (scale << (src * 8)) | 1;
return Q(scale);
}