Message ID | 20240924101337.90430-1-martin@martin.st |
---|---|
State | New |
Headers | show |
Series | [FFmpeg-devel,v2] compat: Fix the fallback definition of stdc_trailing_zeros | expand |
Context | Check | Description |
---|---|---|
andriy/make_x86 | success | Make finished |
andriy/make_fate_x86 | success | Make fate finished |
Quoting Martin Storsjö (2024-09-24 12:13:37) > While shifting "value" to left, we would iterate through all bits > of an unsigned long long, while we only expect to count through > "size * CHAR_BIT" bits; instead shift bits to the right and just > count the trailing zeros. > > This fixes fate with MSVC. > --- > Fixed the UB by shifting to the right instead of to the left. > --- > compat/stdbit/stdbit.h | 11 +++++++---- > 1 file changed, 7 insertions(+), 4 deletions(-) LGTM
diff --git a/compat/stdbit/stdbit.h b/compat/stdbit/stdbit.h index b434fc2357..53419cf9f9 100644 --- a/compat/stdbit/stdbit.h +++ b/compat/stdbit/stdbit.h @@ -178,11 +178,14 @@ static inline unsigned int stdc_trailing_zeros_uc(unsigned char value) static inline unsigned int __stdc_trailing_zeros(unsigned long long value, unsigned int size) { - unsigned int zeros = size * CHAR_BIT; + unsigned int zeros = 0; - while (value != 0) { - value <<= 1; - zeros--; + if (!value) + return size * CHAR_BIT; + + while ((value & 1) == 0) { + value >>= 1; + zeros++; } return zeros;