]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
math: Fix UB in ldbl-128ibm ilogb
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 7 May 2025 13:37:15 +0000 (13:37 +0000)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Thu, 8 May 2025 12:34:10 +0000 (09:34 -0300)
UBSAN: Undefined behaviour in ../sysdeps/ieee754/ldbl-128ibm/e_ilogbl.c:45:37 left shift of 4611686018427387904 by 1 cannot be represented in type 'long int'

sysdeps/ieee754/ldbl-128ibm/e_ilogbl.c

index 9daa970f84f917000a4f7c40abf25e6513dfde54..b86c841d08dad69454b8ff6d988dcd3597bf2cda 100644 (file)
@@ -27,6 +27,7 @@ static char rcsid[] = "$NetBSD: $";
 #include <math.h>
 #include <math_private.h>
 #include <math_ldbl_opt.h>
+#include <stdbit.h>
 
 int __ieee754_ilogbl(long double x)
 {
@@ -42,7 +43,8 @@ int __ieee754_ilogbl(long double x)
            if(hx==0)
                return FP_ILOGB0;       /* ilogbl(0) = FP_ILOGB0 */
            else                        /* subnormal x */
-               for (ix = -1022, hx<<=11; hx>0; hx<<=1) ix -=1;
+               //for (ix = -1022, hx<<=11; hx>0; hx<<=1) ix -=1;
+               ix = -1022 - stdc_leading_zeros (hx << 11);
            return ix;
        }
        else if (hx < 0x7ff0000000000000LL)