[glibc/azanella/ubsan-undef] math: Fix UB in ldbl-96 setayloadl
Adhemerval Zanella
azanella@sourceware.org
Wed May 7 14:18:00 GMT 2025
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=517fb46f9692dca570f35bcb481c636b80a0e39b
commit 517fb46f9692dca570f35bcb481c636b80a0e39b
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date: Fri May 2 15:36:50 2025 -0300
math: Fix UB in ldbl-96 setayloadl
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'
Diff:
---
sysdeps/ieee754/ldbl-96/s_setpayloadl_main.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/sysdeps/ieee754/ldbl-96/s_setpayloadl_main.c b/sysdeps/ieee754/ldbl-96/s_setpayloadl_main.c
index 27b9931201..5105607472 100644
--- a/sysdeps/ieee754/ldbl-96/s_setpayloadl_main.c
+++ b/sysdeps/ieee754/ldbl-96/s_setpayloadl_main.c
@@ -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;
More information about the Glibc-cvs
mailing list