[FFmpeg-devel] [PATCH] compat: Fix the fallback definition of stdc_trailing_zeros

Anton Khirnov anton at khirnov.net
Tue Sep 24 12:27:48 EEST 2024


Quoting Martin Storsjö (2024-09-24 10:53:38)
> 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.
> 
> This fixes fate with MSVC.
> ---
>  compat/stdbit/stdbit.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/compat/stdbit/stdbit.h b/compat/stdbit/stdbit.h
> index b434fc2357..3197a24938 100644
> --- a/compat/stdbit/stdbit.h
> +++ b/compat/stdbit/stdbit.h
> @@ -179,9 +179,10 @@ static inline unsigned int __stdc_trailing_zeros(unsigned long long value,
>                                                   unsigned int size)
>  {
>      unsigned int zeros = size * CHAR_BIT;
> +    unsigned long long mask = (1ULL << (size * CHAR_BIT)) - 1;

This is UB for size == sizeof(unsigned long long).

>  
>      while (value != 0) {
> -        value <<= 1;
> +        value = (value << 1) & mask;

Otherwise looks ok.

-- 
Anton Khirnov


More information about the ffmpeg-devel mailing list