]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
math: Fix UB in ldbl-96 setayloadl
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 2 May 2025 18:36:50 +0000 (15:36 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Thu, 8 May 2025 12:25:49 +0000 (09:25 -0300)
The code can shift the 1ULL for value larger than 32 depending of
the exponent value.  Building with ubsan triggers:

$ math/test-ldouble-setpayload
testing long double (without inline functions)
UBSAN: Undefined behaviour in ../sysdeps/ieee754/ldbl-96/s_setpayloadl_main.c:48:32 shift exponent 16414 is too large for 32-bit type 'unsigned int'

sysdeps/ieee754/ldbl-96/s_setpayloadl_main.c

index 27b9931201524fc0690315d63801b0452b53fef1..51056074720943e517ea4b113b6cad9116fa1872 100644 (file)
@@ -45,7 +45,8 @@ FUNC (long double *x, long double payload)
   int shift = BIAS + EXPLICIT_MANT_DIG - exponent;
   if (shift < 32
       ? (lx & ((1U << shift) - 1)) != 0
-      : (lx != 0 || (hx & ((1U << (shift - 32)) - 1)) != 0))
+      : shift < 64 ? (lx != 0 || (hx & ((1U << (shift - 32)) - 1)) != 0)
+                  : 0)
     {
       SET_LDOUBLE_WORDS (*x, 0, 0, 0);
       return 1;